제출 #1291820

#제출 시각아이디문제언어결과실행 시간메모리
1291820ariukTracks in the Snow (BOI13_tracks)C++20
100 / 100
775 ms119596 KiB
#include <bits/stdc++.h>
using namespace std;

int n, m, depth[4001][4001];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
char grid[4001][4001];

bool inside(int i, int j){
    if(i < 0 || i >= n || j < 0 || j >= m || grid[i][j] == '.') return false;
    else return true;
}



int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;

    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            cin >> grid[i][j];
        }
    }

    depth[0][0] = 1;
    deque<pair<int, int>> q;
    q.push_back({0, 0});

    int ans = 0;

    while(!q.empty()){
        auto s = q.front();
        q.pop_front();
        int x = s.first;
        int y = s.second;
        ans = max(ans, depth[x][y]);

        for(int i = 0; i < 4; i++){
            int sx = x + dx[i];
            int sy = y + dy[i];

            if(inside(sx, sy) && depth[sx][sy] == 0){
                if(grid[x][y] == grid[sx][sy]){
                    depth[sx][sy] = depth[x][y];
                    q.push_front({sx, sy});
                }else{
                    depth[sx][sy] = depth[x][y] + 1;
                    q.push_back({sx, sy});
                }
            }

        }


    }

    cout <<  ans << endl;

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...