이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
import java.util.*;
import java.io.*;
public class tracks {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer in = new StringTokenizer(br.readLine());
int h = Integer.parseInt(in.nextToken()), w = Integer.parseInt(in.nextToken());
int[][] grid = new int[h][w];
for (int i=0; i< h; i++) {
String s = br.readLine();
for (int j =0; j< w; j++) grid[i][j] = s.charAt(j);
}
ArrayDeque<Integer> nextRow = new ArrayDeque<>();
ArrayDeque<Integer> nextHeight = new ArrayDeque<>();
ArrayDeque<Integer> nextValue = new ArrayDeque<>();
nextRow.addLast(0);
nextHeight.addLast(0);
nextValue.addLast(1);
int maxV = 1;
while (!nextRow.isEmpty()) {
int r = nextRow.removeFirst(), c = nextHeight.removeFirst(), v = nextValue.removeFirst();
maxV = Math.max(maxV, v);
if (r < h-1 && grid[r+1][c] != '.') {
if (grid[r+1][c]==grid[r][c]) {
nextRow.addFirst(r+1); nextHeight.addFirst(c); nextValue.addFirst(v);
} else {
nextRow.addLast(r+1); nextHeight.addLast(c); nextValue.addLast(v+1);
}
}
if (r > 0 && grid[r-1][c] != '.') {
if (grid[r-1][c]==grid[r][c]) {
nextRow.addFirst(r-1); nextHeight.addFirst(c); nextValue.addFirst(v);
} else {
nextRow.addLast(r-1); nextHeight.addLast(c); nextValue.addLast(v+1);
}
}
if (c < w-1 && grid[r][c+1] != '.') {
if (grid[r][c+1]==grid[r][c]) {
nextRow.addFirst(r); nextHeight.addFirst(c+1); nextValue.addFirst(v);
} else {
nextRow.addLast(r); nextHeight.addLast(c+1); nextValue.addLast(v+1);
}
}
if (c > 0 && grid[r][c-1] != '.') {
if (grid[r][c-1]==grid[r][c]) {
nextRow.addFirst(r); nextHeight.addFirst(c-1); nextValue.addFirst(v);
} else {
nextRow.addLast(r); nextHeight.addLast(c-1); nextValue.addLast(v+1);
}
}
grid[r][c] = '.';
}
out.println(maxV);
br.close();
out.close();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |