#include<iostream>
using namespace std;
char Dy[2010][2010];
int visit[2010][2010];
char Data[410][410];
int ans[410][410];
int dir[4][2] = { 0,1,1,0,0,-1,-1,0 };
int num = 1;
int cnt[160010];
int flag = 0;
int n, m;
void dfs(int y, int x, char base)
{
for (int i = 0; i < 4; i++)
{
if (y + dir[i][0] < 0)
{
if (Data[n - 1][x%m] == base)
flag = 1;
continue;
}
if (y + dir[i][0] == 5 * n)
{
if (Data[0][x%m] == base)
flag = 1;
continue;
}
if (x + dir[i][1] < 0)
{
if (Data[y%n][m - 1] == base)
flag = 1;
continue;
}
if (x + dir[i][1] == 5 * m)
{
if (Data[y%n][0] == base)
flag = 1;
continue;
}
if (Dy[y + dir[i][0]][x + dir[i][1]] == base && visit[y+dir[i][0]][x+dir[i][1]]==0)
{
cnt[num]++;
visit[y + dir[i][0]][x + dir[i][1]] = num;
dfs(y + dir[i][0], x + dir[i][1], base);
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> Data[i];
int start_n = 2*n;
int start_m = 2*m;
for (int i = 0; i < 5 * n; i++)
for (int j = 0; j < 5 * m; j++)
Dy[i][j] = Data[i%n][j%m];
for (int i = start_n; i < start_n + n; i++)
{
for (int j = start_m; j < start_m + m; j++)
{
flag = 0;
if (visit[i][j])continue;
cnt[num]++;
visit[i][j] = num;
dfs(i, j, Dy[i][j]);
if (flag)
cnt[num] = -1;
num++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << cnt[visit[i + start_n][j + start_m]] << " ";
}
cout << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
27928 KB |
Output is correct |
2 |
Incorrect |
79 ms |
34908 KB |
Extra information in the output file |
3 |
Halted |
0 ms |
0 KB |
- |