제출 #546495

#제출 시각아이디문제언어결과실행 시간메모리
546495_martynasTracks in the Snow (BOI13_tracks)C++11
100 / 100
986 ms75000 KiB
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second

using pii = pair<int, int>;

const int MXN = 4005;

int n, m;
int A[MXN][MXN];
int dirI[] = {0, 0, -1, 1};
int dirJ[] = {-1, 1, 0, 0};

bool inbounds(int i, int j)
{
    return i >= 0 && i < n && j >= 0 && j < m;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            char c; cin >> c;
            if(c == '.') A[i][j] = -1;
            else if(c == 'F') A[i][j] = 0;
            else A[i][j] = 1;
        }
    }

    queue<pii> Q[2];
    int which = A[0][0];
    A[0][0] = -1;
    Q[which].push({0, 0});
    int cnt = 0;
    do {
//        for(int i = 0; i < n; i++) {
//            for(int j = 0; j < m; j++) {
//                cerr << setw(2) << A[i][j] << " ";
//            }
//            cerr << "\n";
//        }
//        cerr << "\n";
        while(!Q[which].empty()) {
            int i = Q[which].front().F, j = Q[which].front().S;
            Q[which].pop();
            for(int k = 0; k < 4; k++) {
                int ii = i + dirI[k], jj = j + dirJ[k];
                if(inbounds(ii, jj)) {
                    if(A[ii][jj] == which) {
                        A[ii][jj] = -1;
                        Q[which].push({ii, jj});
                    }
                    else if(A[ii][jj] == 1-which) {
                        A[ii][jj] = -1;
                        Q[1-which].push({ii, jj});
                    }
                }
            }
        }
        which = 1-which;
        cnt++;
    } while(!Q[which].empty());

    cout << cnt;

    return 0;
}
/*
5 8
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...