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<algorithm>
#include<vector>
#include<bitset>
using namespace std;
int Q[3<<17], Deg[3<<17], Res[3<<17], ord[3<<17], cnt,vis[3<<17];
char p[1<<9][1<<9];
vector<int>E[3<<17], F[3<<17];
int n, m;
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));
}
}
int head = 0, tail = 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];
int cur = 0;
for (i = 1; i <= n; i++) {
cur++;
for (j = 1; j <= m; j++) {
int bs = Num(i,j,3);
int head = 0, tail = 0;
if (vis[bs] != cur && !Deg[bs]) {
vis[bs] = cur, Res[bs] += m-j+1;
Q[++tail] = bs;
}
while (head < tail) {
int x = Q[++head];
for (auto &t : F[x]) {
if (vis[t] != cur && !Deg[t]) {
vis[t] = cur, Res[t] += m-j+1;
Q[++tail] = t;
}
}
}
}
cur++;
for (j = m; j >= 1; j--) {
int bs = Num(i, j, 2);
int head = 0, tail = 0;
if (vis[bs] != cur && !Deg[bs]) {
vis[bs] = cur, Res[bs] += j;
Q[++tail] = bs;
}
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;
}
}
}
}
}
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 (stderr)
sandwich.cpp: In function 'int main()':
sandwich.cpp:32: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:34:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", p[i] + 1);
~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |