#include<bits/stdc++.h>
using namespace std;
int rr[4] = {1,0,-1,0};
int cc[4] = {0,1,0,-1};
char bd[805][805];
int vis[2][805][805];
int n,m;
int ans[160001], clr = 1;
int my_clr[401][401], cntt[2][160001];
void dfs(int flag, int r, int c, int md){
//printf("%d %d %d %d %d %d\n",flag,r,c,bf,br,bc);
cntt[md][clr]++;
for(int i=0;i<4;i++){
int new_flag = flag, nr = r, nc = c;
nr += rr[i]; nc += cc[i];
if(nr == -1){
nr = n-1;
new_flag = 1;
}
if(nr == n){
nr = 0;
new_flag = 1;
}
if(nc == -1){
nc = m-1;
new_flag = 1;
}
if(nc == m){
nc = 0;
new_flag = 1;
}
if(bd[r][c] == bd[nr][nc] && vis[new_flag][nr][nc] != clr){
vis[new_flag][nr][nc] = clr;
dfs(new_flag, nr, nc, md);
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++) scanf("%s",bd[i]);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(vis[1][i][j] == 0){
vis[1][i][j] = clr;
dfs(1,i,j,0);
clr++;
}
my_clr[i][j] = vis[1][i][j];
}
}
n*=2;m*=2;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
vis[1][i][j] = 0;
bd[i][j] = bd[i%(n/2)][j%(m/2)];
}
}
for(int i=0;i<n/2;i++){
for(int j=0;j<m/2;j++){
if(ans[my_clr[i][j]] == 0){
clr = my_clr[i][j];
vis[1][i][j] = clr;
dfs(1,i,j,1);
if(cntt[0][clr] == cntt[1][clr]){
ans[clr] = cntt[0][clr];
}else{
ans[clr] = -1;
}
}
printf("%d ",ans[my_clr[i][j]]);
}
printf("\n");
}
}
Compilation message
YZ.cpp: In function 'int main()':
YZ.cpp:41:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
^
YZ.cpp:42:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0;i<n;i++) scanf("%s",bd[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
19208 KB |
Output is correct |
2 |
Correct |
46 ms |
25236 KB |
Output is correct |
3 |
Correct |
36 ms |
17980 KB |
Output is correct |
4 |
Correct |
33 ms |
13476 KB |
Output is correct |
5 |
Correct |
29 ms |
12580 KB |
Output is correct |
6 |
Correct |
29 ms |
11040 KB |
Output is correct |
7 |
Correct |
39 ms |
15156 KB |
Output is correct |
8 |
Correct |
39 ms |
19920 KB |
Output is correct |
9 |
Correct |
49 ms |
20016 KB |
Output is correct |
10 |
Correct |
33 ms |
15136 KB |
Output is correct |
11 |
Correct |
39 ms |
20032 KB |
Output is correct |
12 |
Correct |
39 ms |
20016 KB |
Output is correct |
13 |
Correct |
39 ms |
18904 KB |
Output is correct |
14 |
Correct |
23 ms |
18568 KB |
Output is correct |
15 |
Correct |
46 ms |
22664 KB |
Output is correct |
16 |
Correct |
36 ms |
14272 KB |
Output is correct |
17 |
Correct |
23 ms |
19968 KB |
Output is correct |
18 |
Correct |
26 ms |
19968 KB |
Output is correct |
19 |
Correct |
33 ms |
10216 KB |
Output is correct |
20 |
Correct |
63 ms |
18836 KB |
Output is correct |
21 |
Correct |
43 ms |
23256 KB |
Output is correct |
22 |
Correct |
53 ms |
22960 KB |
Output is correct |
23 |
Correct |
69 ms |
17732 KB |
Output is correct |
24 |
Correct |
53 ms |
20480 KB |
Output is correct |
25 |
Correct |
56 ms |
24876 KB |
Output is correct |
26 |
Correct |
0 ms |
10216 KB |
Output is correct |
27 |
Correct |
0 ms |
10216 KB |
Output is correct |
28 |
Correct |
0 ms |
10788 KB |
Output is correct |
29 |
Correct |
0 ms |
10788 KB |
Output is correct |
30 |
Correct |
0 ms |
10216 KB |
Output is correct |
31 |
Correct |
0 ms |
10216 KB |
Output is correct |
32 |
Correct |
0 ms |
10216 KB |
Output is correct |
33 |
Correct |
0 ms |
10216 KB |
Output is correct |
34 |
Correct |
0 ms |
10216 KB |
Output is correct |
35 |
Correct |
0 ms |
10216 KB |
Output is correct |
36 |
Correct |
0 ms |
11544 KB |
Output is correct |
37 |
Correct |
0 ms |
10824 KB |
Output is correct |
38 |
Correct |
3 ms |
10216 KB |
Output is correct |
39 |
Correct |
3 ms |
10620 KB |
Output is correct |
40 |
Correct |
0 ms |
11380 KB |
Output is correct |