#include<cstdio>
#include<algorithm>
#include<vector>
#include<bitset>
using namespace std;
int n, m, Q[501000], Deg[501000], head, tail, Res[501000], cur, ord[501000], cnt,vis[501000];
char p[410][410];
vector<int>E[501000], F[501000];
int Num(int x, int y, int ck) {
if (x<1 || x>n || y<1 || y>m)return 0;
int cc = 0;
if (p[x][y] == 'N') {
if (ck == 0 || ck == 3)cc = 1;
else cc = 2;
}
else {
if (ck == 0 || ck == 2)cc = 1;
else cc = 2;
}
return ((x - 1)*m + (y - 1)) * 2 + cc;
}
void Add_Edge(int a, int b) {
if (!a || !b)return;
E[a].push_back(b);
Deg[a]++;
F[b].push_back(a);
}
int main() {
int i, j;
//freopen("input.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) {
scanf("%s", p[i] + 1);
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
Add_Edge(Num(i, j, 0), Num(i - 1, j, 0));
Add_Edge(Num(i, j, 1), Num(i + 1, j, 1));
Add_Edge(Num(i, j, 2), Num(i, j - 1, 2));
Add_Edge(Num(i, j, 3), Num(i, j + 1, 3));
}
}
if (n > 50 || m > 50)return 0;
for (i = 1; i <= n*m * 2; i++) {
if (!Deg[i])Q[++tail] = i;
}
while (head < tail) {
int x = Q[++head];
for (auto &t : F[x]) {
Deg[t]--;
if (!Deg[t])Q[++tail] = t;
}
}
for (i = 1; i <= tail; i++)ord[++cnt] = Q[i];
for (i = 1; i <= n; i++) {
cur++;
head = tail = 0;
for (j = 1; j <= m; j++) {
int bs = ((i - 1)*m + j - 1) * 2;
if (vis[bs + 1] != cur && !Deg[bs+1]) {
vis[bs + 1] = cur, Res[bs + 1] -= j;
Q[++tail] = bs + 1;
}
if (vis[bs + 2] != cur && !Deg[bs+2]) {
vis[bs + 2] = cur, Res[bs + 2] -= j;
Q[++tail] = bs + 2;
}
while (head < tail) {
int x = Q[++head];
for (auto &t : F[x]) {
if (vis[t] != cur && !Deg[t]) {
vis[t] = cur, Res[t] -= j;
Q[++tail] = t;
}
}
}
}
cur++;
head = tail = 0;
for (j = m; j >= 1; j--) {
int bs = ((i - 1)*m + j - 1) * 2;
if (vis[bs + 1] != cur && !Deg[bs + 1]) {
vis[bs + 1] = cur, Res[bs + 1] += j + 1;
Q[++tail] = bs + 1;
}
if (vis[bs + 2] != cur && !Deg[bs + 2]) {
vis[bs + 2] = cur, Res[bs + 2] += j + 1;
Q[++tail] = bs + 2;
}
while (head < tail) {
int x = Q[++head];
for (auto &t : F[x]) {
if (vis[t] != cur && !Deg[t]) {
vis[t] = cur, Res[t] += j + 1;
Q[++tail] = t;
}
}
}
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
int bs = ((i - 1)*m + j - 1) * 2, res = 1e9;
if (!Deg[bs + 1])res = min(res, Res[bs + 1]);
if (!Deg[bs + 2])res = min(res, Res[bs + 2]);
if (res > 8e8)res = -1;
else res *= 2;
printf("%d ", res);
}
printf("\n");
}
}
Compilation message
sandwich.cpp: In function 'int main()':
sandwich.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
sandwich.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", p[i] + 1);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23908 KB |
Output is correct |
3 |
Correct |
20 ms |
23960 KB |
Output is correct |
4 |
Correct |
20 ms |
23960 KB |
Output is correct |
5 |
Correct |
20 ms |
23960 KB |
Output is correct |
6 |
Correct |
23 ms |
24420 KB |
Output is correct |
7 |
Incorrect |
28 ms |
24472 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
20 ms |
23908 KB |
Output is correct |
3 |
Correct |
20 ms |
23960 KB |
Output is correct |
4 |
Correct |
20 ms |
23960 KB |
Output is correct |
5 |
Correct |
20 ms |
23960 KB |
Output is correct |
6 |
Correct |
23 ms |
24420 KB |
Output is correct |
7 |
Incorrect |
28 ms |
24472 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |