Submission #1160161

#TimeUsernameProblemLanguageResultExecution timeMemory
1160161APersonTracks in the Snow (BOI13_tracks)Java
86.88 / 100
2108 ms741404 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] == '.' || grid[cur.x][cur.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[cur.x][cur.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...