답안 #211107

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
211107 2020-03-19T08:45:00 Z Aldas25 Zoo (COCI19_zoo) C++14
0 / 110
5 ms 512 KB
#include <bits/stdc++.h>

using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef vector<int> vi;
typedef long long ll;
typedef pair<short, short> pii;
typedef vector<pii> vii;

const int MAXN = 16000100;

int n, m;
char grid[4100][4100];
bool checked[4100][4100];
deque<pii> q;
int d[4100][4100];
int curD = 0;

int main()
{
    FAST_IO;

    //ifstream in ("in.txt");

    cin >> n >> m;
    q.push_front({1,1});
    FOR(i, 1, n) FOR(j, 1, m) cin >> grid[i][j];

    //int cnt = 0;
    while (!q.empty()) {
        //int v = 0;
        while (!q.empty() && (checked[q.front().f][q.front().s] || grid[q.front().f][q.front().s]=='.')) {q.pop_front();}
        if (q.empty()) break;
        //cnt++; //curT++;c
        pii v = q.front(); q.pop_front();
        checked[v.f][v.s] = true;
        curD = d[v.f][v.s];

        if (v.f > 1 && !checked[v.f-1][v.s] && grid[v.f-1][v.s] == grid[v.f][v.s]) {
            d[v.f-1][v.s] = d[v.f][v.s];
            q.push_front({v.f-1, v.s});
        }
        if (v.s > 1 && !checked[v.f][v.s-1] && grid[v.f][v.s-1] == grid[v.f][v.s]) {
            d[v.f][v.s-1] = d[v.f][v.s];
            q.push_front({v.f, v.s-1});
        }
        if (v.f < n && !checked[v.f+1][v.s] && grid[v.f+1][v.s] == grid[v.f][v.s]) {
            d[v.f+1][v.s] = d[v.f][v.s];
            q.push_front({v.f+1, v.s});
        }
        if (v.s < m && !checked[v.f][v.s+1] && grid[v.f][v.s+1] == grid[v.f][v.s]) {
            d[v.f][v.s+1] = d[v.f][v.s];
            q.push_front({v.f, v.s+1});
        }


        if (v.f > 1 && !checked[v.f-1][v.s] && grid[v.f-1][v.s] != grid[v.f][v.s] && grid[v.f-1][v.s] != '.') {
            d[v.f-1][v.s] = d[v.f][v.s]+1;
            q.push_back({v.f-1, v.s});
        }
        if (v.s > 1 && !checked[v.f][v.s-1] && grid[v.f][v.s-1] != grid[v.f][v.s] && grid[v.f][v.s-1] != '.') {
            d[v.f][v.s-1] = d[v.f][v.s]+1;
            q.push_back({v.f, v.s-1});
        }
        if (v.f < n && !checked[v.f+1][v.s] && grid[v.f+1][v.s] != grid[v.f][v.s] && grid[v.f+1][v.s] != '.') {
            d[v.f+1][v.s] = d[v.f][v.s]+1;
            q.push_back({v.f+1, v.s});
        }
        if (v.s < m && !checked[v.f][v.s+1] && grid[v.f][v.s+1] != grid[v.f][v.s] && grid[v.f][v.s+1] != '.') {
            d[v.f][v.s+1] = d[v.f][v.s]+1;
            q.push_back({v.f, v.s+1});
        }

        //cout << " d[("<<v.f << ", " << v.s << ") = " << d[v.f][v.s] << endl;

        //dfs (q.front().f, q.front().s);

    }

   // dfsTree (1, -1);

    cout << curD+1;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 512 KB Output is correct
2 Incorrect 5 ms 512 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 512 KB Output is correct
2 Incorrect 5 ms 512 KB Output isn't correct
3 Halted 0 ms 0 KB -