Submission #394172

#TimeUsernameProblemLanguageResultExecution timeMemory
394172SirCovidThe19thTracks in the Snow (BOI13_tracks)C++14
100 / 100
1482 ms122428 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
	
    int h, w;
    cin >> h >> w;

    char grid[h][w];
    for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
            cin >> grid[i][j];
    
    int ans = 0;
    int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1};
    int depth[h][w];
    deque<pair<int, int>> trav;

    memset(depth, 0, sizeof(depth));
    depth[0][0] = 1; trav.push_back({0, 0});

    while (!trav.empty()){
        pair<int, int> curr = trav.front(); trav.pop_front();
        ans = max(ans, depth[curr.first][curr.second]);
        for (int i = 0; i < 4; i++){
            int x = curr.first+dx[i]; int y = curr.second+dy[i];
            if (x < 0 or x >= h or y < 0 or y >= w or grid[x][y] == '.' or depth[x][y] != 0) continue;
            if (grid[x][y] == grid[curr.first][curr.second]){
                depth[x][y] = depth[curr.first][curr.second];
                trav.push_front({x, y});
            }
            else{
                depth[x][y] = depth[curr.first][curr.second]+1;
                trav.push_back({x, y});
            }
        }
    }
    cout<<ans<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...