제출 #1039179

#제출 시각아이디문제언어결과실행 시간메모리
1039179biserailievaTracks in the Snow (BOI13_tracks)C++14
47.50 / 100
2105 ms94452 KiB
#include <bits/stdc++.h>
using namespace std;
 
int h, w;
char m[4000][4000];
queue<pair<int, int> > q;
int vis[4000][4000];
vector<pair<int, int> > dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
 
int main() {
    cin>>h>>w;
    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++) {
            cin>>m[i][j];
        }
    }
    int res = 1;
    char animal = m[0][0];
    bool finish = false;
    while(!finish) {
        q.push({0, 0});
        finish = true;
        while(q.size()) {
            int y = q.front().first;
            int x = q.front().second;
            q.pop();
            if(vis[y][x] == res || (animal != m[y][x] && m[y][x] != 'A'))
                continue;
            if(m[y][x] != 'A')
                finish = false;
            vis[y][x] = res;
            m[y][x] = 'A';
            for(auto d : dir) {
                int newY = y + d.first;
                int newX = x + d.second;
                if(newY >= 0 && newY < h && newX >= 0 && newX < w && vis[newY][newX] != res)
                    if(m[newY][newX] == animal || m[newY][newX] == 'A')
                        q.push({newY, newX});
            }
        }
        res++;
        if(animal == 'R')
            animal = 'F';
        else
            animal = 'R';
    }
    cout<<res - 2<<endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...