제출 #1342410

#제출 시각아이디문제언어결과실행 시간메모리
1342410nathlol2Tracks in the Snow (BOI13_tracks)C++20
12.08 / 100
770 ms169436 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 4e3 + 5;
int n, m, ans, vis[N][N], dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
char tb[N][N];
queue<pair<int, int>> q, cur, nxt;
vector<pair<int, int>> v;

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

    cin >> n >> m;
    for(int i = 1;i<=n;i++) for(int j = 1;j<=m;j++) cin >> tb[i][j];
    ++ans;
    q.push({n, m});
    vis[n][m] = 1;
    while(q.size()){
        auto [x, y] = q.front();
        q.pop();
        for(int i = 0;i<4;i++){
            int nx = x + dx[i], ny = y + dy[i];
            if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && tb[nx][ny] == tb[n][m] && !vis[nx][ny]){
                vis[nx][ny] = 1;
                q.push({nx, ny});
            }
        }
    }
    for(int i = 1;i<=n;i++) for(int j = 1;j<=m;j++) if(vis[i][j]) cur.push({i, j});
    while(cur.size()){
        while(cur.size()){
            auto [x, y] = cur.front();
            cur.pop();
            for(int i = 0;i<4;i++){
                int nx = x + dx[i], ny = y + dy[i];
                if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && tb[nx][ny] != '.' && !vis[nx][ny]){
                    vis[nx][ny] = 1;
                    nxt.push({nx, ny});
                }
            }
        }
        if(nxt.size()) ++ans;
        swap(cur, nxt);
    }
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...