# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
22456 | Jongwon Party (#40) | Young Zebra (KRIII5_YZ) | C++14 | 500 ms | 11980 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |