|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.io.OutputStreamWriter; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + static BufferedReader br; |
| 10 | + static BufferedWriter bw; |
| 11 | + public static void main(String[] args) throws Exception { |
| 12 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 14 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 15 | + int N = Integer.parseInt(st.nextToken()); |
| 16 | + int M = Integer.parseInt(st.nextToken()); |
| 17 | + int T = Integer.parseInt(st.nextToken()); |
| 18 | + int D = Integer.parseInt(st.nextToken()); |
| 19 | + |
| 20 | + int[][] map = new int[N][M]; |
| 21 | + for(int n=0;n<N;n++){ |
| 22 | + int temp=0; |
| 23 | + for(char c : br.readLine().toCharArray()){ |
| 24 | + if(c>=97 && c<=122){ |
| 25 | + map[n][temp++] = c-'a' + 26; |
| 26 | + } |
| 27 | + else{ |
| 28 | + map[n][temp++] += c-'A'; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + long[][] shortpath = new long[N*M][N*M]; |
| 34 | + for(int i=0;i<N*M;i++){ |
| 35 | + Arrays.fill(shortpath[i],Integer.MAX_VALUE); |
| 36 | + shortpath[i][i] = 0; |
| 37 | + } |
| 38 | + //상하좌우 순서서 |
| 39 | + int[] dx = {0,0,-1,1}; |
| 40 | + int[] dy = {-1,1,0,0}; |
| 41 | + |
| 42 | + for(int i=0;i<N;i++){ |
| 43 | + for(int j=0;j<M;j++){ |
| 44 | + for(int m=0;m<4;m++){ |
| 45 | + int newi = i + dy[m]; |
| 46 | + int newj = j + dx[m]; |
| 47 | + if(newi >= 0 && newi < N && newj >= 0 && newj < M){ |
| 48 | + int height = map[newi][newj] - map[i][j]; |
| 49 | + if(Math.abs((double)height) > T){continue;} |
| 50 | + if(height >= 0){//높은 곳에서 아래로 이동 |
| 51 | + shortpath[newi*M+newj][i*M+j] = 1; |
| 52 | + shortpath[i*M+j][newi*M+newj] = (int)Math.pow(height,2); |
| 53 | + } |
| 54 | + else{ |
| 55 | + shortpath[newi*M+newj][i*M+j] = (int)Math.pow(height,2); |
| 56 | + shortpath[i*M+j][newi*M+newj] = 1; |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + for(int x=0;x<N;x++){ |
| 64 | + for(int y=0;y<M;y++){ |
| 65 | + for(int i=0;i<N;i++){ |
| 66 | + for(int j=0;j<M;j++){ |
| 67 | + for(int k=0;k<N;k++){ |
| 68 | + for(int l=0;l<M;l++){ |
| 69 | + shortpath[i*M+j][k*M+l] = Math.min(shortpath[i*M+j][k*M+l],shortpath[i*M+j][x*M+y] + shortpath[x*M+y][k*M+l]); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + int max = map[0][0]; |
| 78 | + for(int i=0;i<N;i++){ |
| 79 | + for(int j=0;j<M;j++){ |
| 80 | + if(shortpath[0][i*M+j] + shortpath[i*M+j][0] <= D){ |
| 81 | + max = Math.max(max,map[i][j]); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + bw.write(max+""); |
| 86 | + bw.flush(); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +``` |
0 commit comments