#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
char arr[410][410];
int chk[410][410];
int pos[410][410][2];
int res[160010];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
int n, m;
int f(int x, int y, char c, int p, int px, int py)
{
int r, nx, ny, npx, npy, t, i;
chk[x][y] = p;
pos[x][y][0] = px;
pos[x][y][1] = py;
r = 1;
for(i = 0; i<4; i++)
{
nx = x+dx[i];
ny = y+dy[i];
npx = px;
npy = py;
if(nx < 0)
{
npx--;
nx += n;
}
else if(nx >= n)
{
npx++;
nx -= n;
}
if(ny < 0)
{
npy--;
ny += m;
}
else if(ny >= m)
{
npy++;
ny -= m;
}
if(arr[nx][ny] != c)
continue;
if(chk[nx][ny] != p)
{
t = f(nx, ny, c, p, npx, npy);
if(t == -1)
return -1;
r += t;
continue;
}
if(pos[nx][ny][0] == npx && pos[nx][ny][1] == npy)
continue;
return -1;
}
return r;
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int p, t, i, j;
scanf("%d%d", &n, &m);
for(i = 0; i<n; i++)
scanf("%s", arr[i]);
p = 1;
for(i = 0; i<n; i++)
{
for(j = 0; j<m; j++)
{
if(chk[i][j])
{
printf("%d ", res[chk[i][j]]);
continue;
}
t = f(i, j, arr[i][j], p, 0, 0);
res[p++] = t;
printf("%d ", t);
}
printf("\n");
}
}
Compilation message
YZ.cpp: In function 'int main()':
YZ.cpp:82:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
YZ.cpp:84:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", arr[i]);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
500 ms |
11980 KB |
Execution timed out |
2 |
Halted |
0 ms |
0 KB |
- |