#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <string>
using namespace std;
int N, M;
char sp[444][444];
int bcnt, wcnt;
bool visit[444][444][5];
int ans[444][444];
int cnt;
int mxcnt;
char cur;
int spx, spy;
int an[444];
int idx = 1;
bool chk;
void dfs(int, int, int);
int main()
{
cin >> N >> M;
for (int i = 0; i < N; i++)
{
scanf("%s", sp[i]);
for (int j = 0; sp[i][j] != 0; j++)
sp[i][j] == 'W' ? wcnt++ : bcnt++;
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
cnt = 0;
mxcnt = 0;
cur = sp[i][j];
if(!ans[i][j])
{
dfs(i, j, 0);
an[idx++] = cnt;
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (sp[i][j] == 'B' && an[ans[i][j]] > bcnt)
printf("-1 ");
if (sp[i][j] == 'W' && an[ans[i][j]] > wcnt)
printf("-1 ");
else
printf("%d ", an[ans[i][j]]);
}
printf("\n");
}
}
void dfs(int x, int y, int dir)
{
cnt++;
visit[x][y][0] = true;
if (chk)
visit[x][y][1] = true;
if (x - 1 >= 0 && !visit[x - 1][y][dir] && sp[x - 1][y] == cur)
{
dfs(x - 1, y, dir);
}
if (x + 1 < N && !visit[x + 1][y][dir] && sp[x + 1][y] == cur)
{
dfs(x + 1, y, dir);
}
if (y - 1 >= 0 && !visit[x][y - 1][dir] && sp[x][y - 1] == cur)
{
dfs(x, y - 1, dir);
}
if (y + 1 < M && !visit[x][y + 1][dir] && sp[x][y + 1] == cur)
{
dfs(x, y + 1, dir);
}
if (x == 0 && !visit[N - 1][y][0] && !visit[N - 1][y][1] && sp[N - 1][y] == cur)
{
chk = true;
dfs(N - 1, y, 0);
chk = false;
}
if (x == N - 1 && !visit[0][y][0] && !visit[0][y][1] && sp[0][y] == cur)
{
chk = true;
dfs(0, y, 0);
chk = false;
}
if (y == 0 && !visit[x][M - 1][0] && !visit[x][M - 1][1] && sp[x][M - 1] == cur)
{
chk = true;
dfs(x, M - 1, 0);
chk = false;
}
if (y == M - 1 && !visit[x][0][0] && !visit[x][0][1] && sp[x][0] == cur)
{
chk = true;
dfs(x, 0, 0);
chk = false;
}
ans[x][y] = idx;
}
Compilation message
YZ.cpp: In function 'int main()':
YZ.cpp:36:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", sp[i]);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
23 ms |
7152 KB |
Output is partially correct |
2 |
Partially correct |
23 ms |
7640 KB |
Output is partially correct |
3 |
Partially correct |
19 ms |
8048 KB |
Output is partially correct |
4 |
Partially correct |
16 ms |
5436 KB |
Output is partially correct |
5 |
Partially correct |
19 ms |
4628 KB |
Output is partially correct |
6 |
Partially correct |
16 ms |
4488 KB |
Output is partially correct |
7 |
Partially correct |
26 ms |
7984 KB |
Output is partially correct |
8 |
Partially correct |
16 ms |
8880 KB |
Output is partially correct |
9 |
Partially correct |
16 ms |
7600 KB |
Output is partially correct |
10 |
Partially correct |
19 ms |
7524 KB |
Output is partially correct |
11 |
Partially correct |
19 ms |
11264 KB |
Output is partially correct |
12 |
Partially correct |
16 ms |
11264 KB |
Output is partially correct |
13 |
Partially correct |
13 ms |
6780 KB |
Output is partially correct |
14 |
Incorrect |
13 ms |
6684 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |