# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
22573 | Lazy Against The Machine (#40) | Young Zebra (KRIII5_YZ) | C++14 | 33 ms | 16192 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 <bits/stdc++.h>
using namespace std;
char dat[404][404];
bool visit[404][404];
int ans[404][404];
pair<int, int> depth[404][404];
vector<pair<int, int>> components;
bool infinite;
int n, m;
int dr[4][2] =
{
1, 0,
0, 1,
-1, 0,
0, -1
};
void dfs(int x, int y, int dx, int dy)
{
visit[x][y] = true;
depth[x][y] = {dx, dy};
components.emplace_back(x, y);
for (int i = 0; i < 4; i++)
{
int nx = x + dr[i][0];
int ny = y + dr[i][1];
int ndx = dx, ndy = dy;
if (nx < 0)
{
--ndx;
nx += n;
}
if (nx >= n)
{
++ndx;
nx -= n;
}
if (ny < 0)
{
--ndy;
ny += m;
}
if (ny >= m)
{
++ndy;
ny -= m;
}
if (dat[x][y] != dat[nx][ny]) continue;
if (visit[nx][ny])
{
if (depth[nx][ny] != make_pair(ndx, ndy)) infinite = true;
continue;
}
dfs(nx, ny, ndx, ndy);
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
scanf("%s", dat[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (visit[i][j]) continue;
infinite = false;
components.clear();
dfs(i, j, 0, 0);
for (auto &e : components)
{
if (infinite) ans[e.first][e.second] = -1;
else ans[e.first][e.second] = components.size();
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++) printf("%d ", ans[i][j]);
printf("\n");
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |