#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];
int ans[444][444];
int cnt;
char cur;
int spx, spy;
void dfs(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++)
{
memset(visit, false, sizeof(visit));
cnt = 0;
cur = sp[i][j];
spx = i;
spy = j;
if (sp[i - 1][j] == cur && ans[i - 1][j])
ans[i][j] = ans[i - 1][j];
if (sp[i + 1][j] == cur && ans[i + 1][j])
ans[i][j] = ans[i + 1][j];
if (sp[i][j + 1] == cur && ans[i][j + 1])
ans[i][j] = ans[i][j + 1];
if (sp[i][j - 1] == cur && ans[i][j - 1])
ans[i][j] = ans[i][j - 1];
else
{
dfs(i, j);
ans[i][j] = cnt;
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
printf("%d ", ans[i][j]);
printf("\n");
}
}
void dfs(int x, int y)
{
cnt++;
visit[x][y] = true;
if (x - 1 >= 0 && !visit[x - 1][y] && sp[x - 1][y] == cur)
{
dfs(x - 1, y);
}
if (x + 1 < N && !visit[x + 1][y] && sp[x + 1][y] == cur)
{
dfs(x + 1, y);
}
if (y - 1 >= 0 && !visit[x][y - 1] && sp[x][y - 1] == cur)
{
dfs(x, y - 1);
}
if (y + 1 < M && !visit[x][y + 1] && sp[x][y + 1] == cur)
{
dfs(x, y + 1);
}
if (x == 0 && !visit[N - 1][y] && sp[N - 1][y] == cur)
dfs(N - 1, y);
if (x == N - 1 && !visit[0][y] && sp[0][y] == cur)
dfs(0, y);
if (y == 0 && !visit[x][M - 1] && sp[x][M - 1] == cur)
dfs(x, M - 1);
if (y == M - 1 && !visit[x][0] && sp[x][0] == cur)
dfs(x, 0);
}
Compilation message
YZ.cpp: In function 'int main()':
YZ.cpp:32: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 |
Execution timed out |
500 ms |
10340 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |