# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
22786 | 이대회 트래쉬 대회에야옹 (#40) | Young Zebra (KRIII5_YZ) | C++14 | 26 ms | 11264 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 <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][5];
int ans[444][444];
int cnt;
int mxcnt;
char cur;
int spx, spy;
int an[444];
int idx = 1;
bool chk;
void dfs(int, 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++)
{
cnt = 0;
mxcnt = 0;
cur = sp[i][j];
if(!ans[i][j])
{
dfs(i, j, 0);
an[idx++] = cnt;
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (sp[i][j] == 'B' && an[ans[i][j]] > bcnt)
printf("-1 ");
if (sp[i][j] == 'W' && an[ans[i][j]] > wcnt)
printf("-1 ");
else
printf("%d ", an[ans[i][j]]);
}
printf("\n");
}
}
void dfs(int x, int y, int dir)
{
cnt++;
visit[x][y][0] = true;
if (chk)
visit[x][y][1] = true;
if (x - 1 >= 0 && !visit[x - 1][y][dir] && sp[x - 1][y] == cur)
{
dfs(x - 1, y, dir);
}
if (x + 1 < N && !visit[x + 1][y][dir] && sp[x + 1][y] == cur)
{
dfs(x + 1, y, dir);
}
if (y - 1 >= 0 && !visit[x][y - 1][dir] && sp[x][y - 1] == cur)
{
dfs(x, y - 1, dir);
}
if (y + 1 < M && !visit[x][y + 1][dir] && sp[x][y + 1] == cur)
{
dfs(x, y + 1, dir);
}
if (x == 0 && !visit[N - 1][y][0] && !visit[N - 1][y][1] && sp[N - 1][y] == cur)
{
chk = true;
dfs(N - 1, y, 0);
chk = false;
}
if (x == N - 1 && !visit[0][y][0] && !visit[0][y][1] && sp[0][y] == cur)
{
chk = true;
dfs(0, y, 0);
chk = false;
}
if (y == 0 && !visit[x][M - 1][0] && !visit[x][M - 1][1] && sp[x][M - 1] == cur)
{
chk = true;
dfs(x, M - 1, 0);
chk = false;
}
if (y == M - 1 && !visit[x][0][0] && !visit[x][0][1] && sp[x][0] == cur)
{
chk = true;
dfs(x, 0, 0);
chk = false;
}
ans[x][y] = idx;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |