이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
import java.util.*;
import java.io.*;
public class tracks {
private static int[] dx = {-1, 0, 1, 0};
private static int[] dy = {0, -1, 0, 1};
public static void main(String[] args) throws IOException{
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(r.readLine());
int h = Integer.parseInt(st.nextToken()); int w = Integer.parseInt(st.nextToken());
int[][] grid = new int[h][w];
int[][] dist = new int[h][w];
for (int i = 0; i < h; i++) {
String s = r.readLine();
for (int j = 0; j < w; j++) {
dist[i][j] = Integer.MAX_VALUE;
if(s.charAt(j) == 'F'){
grid[i][j] = 0;
}
else if(s.charAt(j) == 'R'){
grid[i][j] = 1;
}
else{
grid[i][j] = 2;
}
}
}
dist[0][0] = 1;
LinkedList<int[]> cur = new LinkedList<>();
cur.add(new int[]{0, 0});
while(!cur.isEmpty()){
int[] x = cur.poll();
for (int i = 0; i < 4; i++) {
int fx = x[0] + dx[i];
int fy = x[1] + dy[i];
if(0 <= fx && fx < h && 0 <= fy && fy < w && grid[fx][fy] != 2){
if(grid[fx][fy] == grid[x[0]][x[1]]){
if(dist[x[0]][x[1]] < dist[fx][fy]){
dist[fx][fy] = dist[x[0]][x[1]];
cur.addFirst(new int[]{fx, fy});
}
}
else{
if(dist[x[0]][x[1]] + 1 < dist[fx][fy]){
dist[fx][fy] = dist[x[0]][x[1]] + 1;
cur.addLast(new int[]{fx, fy});
}
}
}
}
}
int max = Integer.MIN_VALUE;
for(int[] e1 : dist){
for(int e2 : e1){
if(e2 != Integer.MAX_VALUE){
max = Math.max(max, e2);
}
}
}
pw.println(max);
pw.close();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |