답안 #45427

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
45427 2018-04-13T13:31:01 Z choikiwon Young Zebra (KRIII5_YZ) C++17
7 / 7
71 ms 7148 KB
#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int N, M;
char G[402][402];
int vis[402][402];
pii dist[402][402];
int sz[1000010];

int main() {
    scanf("%d %d", &N, &M);

    for(int i = 0; i < N; i++) {
        scanf("\n");
        for(int j = 0; j < M; j++) {
            scanf("%c", &G[i][j]);
        }
    }

    memset(vis, -1, sizeof(vis));

    int ccnt = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++) {
            if(vis[i][j] != -1) continue;

            queue<int> q;
            q.push(i * M + j);
            vis[i][j] = ccnt;

            int inf = 0;
            int cnt = 1;
            while(!q.empty()) {
                int u = q.front(); q.pop();
                int r = u / M;
                int c = u % M;

                for(int k = 0; k < 4; k++) {
                    int nr = (r + dy[k] + N) % N;
                    int nc = (c + dx[k] + M) % M;
                    pii nd = dist[r][c];
                    if(r + dy[k] < 0) nd.first--;
                    if(r + dy[k] >= N) nd.first++;
                    if(c + dx[k] < 0) nd.second--;
                    if(c + dx[k] >= M) nd.second++;
                    if(G[nr][nc] != G[r][c]) continue;
                    if(vis[nr][nc] != -1) {
                        if(dist[nr][nc] != nd) {
                            inf = 1;
                        }
                        continue;
                    }
                    cnt++;
                    q.push(nr * M + nc);
                    vis[nr][nc] = ccnt;
                    dist[nr][nc] = nd;
                }
            }

            sz[ccnt] = inf? -1 : cnt;
            ccnt++;
        }
    }

    for(int i = 0; i < N; i++) {
        for(int j = 0; j < M; j++) {
            printf("%d ", sz[ vis[i][j] ]);
        }
        printf("\n");
    }
}

Compilation message

YZ.cpp: In function 'int main()':
YZ.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &N, &M);
     ~~~~~^~~~~~~~~~~~~~~~~
YZ.cpp:19:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("\n");
         ~~~~~^~~~~~
YZ.cpp:21:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%c", &G[i][j]);
             ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 2936 KB Output is correct
2 Correct 68 ms 3320 KB Output is correct
3 Correct 66 ms 3320 KB Output is correct
4 Correct 50 ms 3552 KB Output is correct
5 Correct 57 ms 3776 KB Output is correct
6 Correct 64 ms 4380 KB Output is correct
7 Correct 61 ms 4380 KB Output is correct
8 Correct 66 ms 4440 KB Output is correct
9 Correct 38 ms 4656 KB Output is correct
10 Correct 39 ms 5132 KB Output is correct
11 Correct 38 ms 5420 KB Output is correct
12 Correct 40 ms 5580 KB Output is correct
13 Correct 38 ms 5580 KB Output is correct
14 Correct 53 ms 5580 KB Output is correct
15 Correct 40 ms 5676 KB Output is correct
16 Correct 43 ms 5984 KB Output is correct
17 Correct 36 ms 5984 KB Output is correct
18 Correct 37 ms 6048 KB Output is correct
19 Correct 46 ms 6208 KB Output is correct
20 Correct 46 ms 6368 KB Output is correct
21 Correct 38 ms 6528 KB Output is correct
22 Correct 38 ms 6556 KB Output is correct
23 Correct 46 ms 6836 KB Output is correct
24 Correct 71 ms 6864 KB Output is correct
25 Correct 39 ms 7148 KB Output is correct
26 Correct 2 ms 7148 KB Output is correct
27 Correct 2 ms 7148 KB Output is correct
28 Correct 3 ms 7148 KB Output is correct
29 Correct 4 ms 7148 KB Output is correct
30 Correct 3 ms 7148 KB Output is correct
31 Correct 2 ms 7148 KB Output is correct
32 Correct 3 ms 7148 KB Output is correct
33 Correct 3 ms 7148 KB Output is correct
34 Correct 2 ms 7148 KB Output is correct
35 Correct 2 ms 7148 KB Output is correct
36 Correct 5 ms 7148 KB Output is correct
37 Correct 5 ms 7148 KB Output is correct
38 Correct 5 ms 7148 KB Output is correct
39 Correct 5 ms 7148 KB Output is correct
40 Correct 4 ms 7148 KB Output is correct