제출 #1160154

#제출 시각아이디문제언어결과실행 시간메모리
1160154APersonTracks in the Snow (BOI13_tracks)Java
0 / 100
1196 ms209264 KiB
import java.util.*;
public class tracks {
    static int n, m, ans = 1;
    static  int[] dx = {0, 0, -1, 1}, dy = {-1, 1, 0, 0};
    static char[][] grid;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        grid = new char[n][m];
        for(int i = 0; i < n; i++) grid[i] = sc.next().toCharArray();
        LinkedList<State> q = new LinkedList<>();
        q.add(new State(0, 0, 1));
        while(!q.isEmpty()) {
            State cur = q.poll();
            ans = Math.max(ans, cur.d);
            for(int i = 0; i < 4; i++) {
                int x = cur.x+dx[i];
                int y = cur.y+dy[i];
                if(x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == '*') continue;
                if (grid[x][y] != grid[cur.x][cur.y]) q.addLast(new State(x, y, cur.d+1));
                else q.addFirst(new State(x, y, cur.d));
                grid[x][y] = '*';
            }
        }
        System.out.println(ans);
        sc.close();
    }
    public static class State {
        int x, y, d;
        public State(int x, int y, int d) {
            this.x = x;
            this.y = y;
            this.d = d;
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...