답안 #307305

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
307305 2020-09-27T15:31:30 Z tieunhi Zoo (COCI19_zoo) C++14
110 / 110
216 ms 45944 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1005;

int n, m, root[N*N];
char c[N][N];
vector<int> g[N*N];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

int inline encode(int x, int y) {
    return (x - 1) * m + y;
}
int get_root(int u) {
    return (root[u] == 0) ? u : root[u] = get_root(root[u]);
}
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); 
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> c[i][j];
        }
    }

    // Merge same components
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (c[i][j] == '*') continue;
            for (int dir = 0; dir < 4; dir++) {
                int x = i + dx[dir];
                int y = j + dy[dir];
                if (x > n || x < 1 || y > m || y < 1) continue;
                if (c[x][y] != c[i][j]) continue;
                
                int p = get_root(encode(i, j));
                int q = get_root(encode(x, y));
                if (p != q) {
                    root[p] = q;
                }
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (c[i][j] == '*') continue;
            for (int dir = 0; dir < 4; dir++) {
                int x = i + dx[dir];
                int y = j + dy[dir];
                if (x > n || x < 1 || y > m || y < 1) continue;
                if (c[x][y] == c[i][j] || c[x][y] == '*') continue;
                
                int p = get_root(encode(i, j));
                int q = get_root(encode(x, y));
                g[p].push_back(q);
                g[q].push_back(p);
            }
        }
    }


    vector<int> d((n+1)*(m+1) + 1, 0);
    queue<int> q;
    int root = get_root(encode(n, m));
    q.push(root); d[root] = 1;
    int res = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        res = max(res, d[u]);
        for (auto v : g[u]) {
            if (d[v]) {
                continue;
            } else {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }
    cout <<res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24064 KB Output is correct
2 Correct 17 ms 24064 KB Output is correct
3 Correct 17 ms 24064 KB Output is correct
4 Correct 16 ms 24192 KB Output is correct
5 Correct 18 ms 24320 KB Output is correct
6 Correct 18 ms 24320 KB Output is correct
7 Correct 18 ms 24320 KB Output is correct
8 Correct 19 ms 24320 KB Output is correct
9 Correct 19 ms 24312 KB Output is correct
10 Correct 19 ms 24320 KB Output is correct
11 Correct 19 ms 24312 KB Output is correct
12 Correct 27 ms 24320 KB Output is correct
13 Correct 17 ms 24320 KB Output is correct
14 Correct 21 ms 24320 KB Output is correct
15 Correct 17 ms 24320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24064 KB Output is correct
2 Correct 17 ms 24064 KB Output is correct
3 Correct 17 ms 24064 KB Output is correct
4 Correct 16 ms 24192 KB Output is correct
5 Correct 18 ms 24320 KB Output is correct
6 Correct 18 ms 24320 KB Output is correct
7 Correct 18 ms 24320 KB Output is correct
8 Correct 19 ms 24320 KB Output is correct
9 Correct 19 ms 24312 KB Output is correct
10 Correct 19 ms 24320 KB Output is correct
11 Correct 19 ms 24312 KB Output is correct
12 Correct 27 ms 24320 KB Output is correct
13 Correct 17 ms 24320 KB Output is correct
14 Correct 21 ms 24320 KB Output is correct
15 Correct 17 ms 24320 KB Output is correct
16 Correct 49 ms 33816 KB Output is correct
17 Correct 53 ms 33784 KB Output is correct
18 Correct 50 ms 34168 KB Output is correct
19 Correct 55 ms 34168 KB Output is correct
20 Correct 50 ms 33856 KB Output is correct
21 Correct 210 ms 44792 KB Output is correct
22 Correct 209 ms 44792 KB Output is correct
23 Correct 213 ms 45048 KB Output is correct
24 Correct 214 ms 45944 KB Output is correct
25 Correct 211 ms 45612 KB Output is correct
26 Correct 212 ms 45176 KB Output is correct
27 Correct 206 ms 44664 KB Output is correct
28 Correct 204 ms 44536 KB Output is correct
29 Correct 215 ms 45816 KB Output is correct
30 Correct 216 ms 45432 KB Output is correct