#include <stdio.h>
#include <string.h>
#define N 50
#define M 50
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int kk[N + 1][M + 1];
int rect(int i1, int j1, int i2, int j2) {
return kk[i2][j2] - kk[i2][j1] - kk[i1][j2] + kk[i1][j1];
}
int main() {
static char cc[N][M + 1];
static int nzl[N][M], nzr[N][M], znl[N][M], znr[N][M], nzu[N][M], nzd[N][M], znu[N][M], znd[N][M], dp[N + 1][M + 1][N + 1][M + 1], ans[N][M];
int n, m, i, i1, i2, j, j1, j2;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%s", cc[i]);
for (i = 0; i < n; i++)
memset(ans[i], 0x3f, m * sizeof *ans[i]);
for (i = 0; i < n; i++) {
nzl[i][0] = -1;
for (j = 1; j < m; j++)
nzl[i][j] = cc[i][j - 1] == 'N' && cc[i][j] == 'Z' ? j - 1 : nzl[i][j - 1];
nzr[i][m - 1] = m;
for (j = m - 2; j >= 0; j--)
nzr[i][j] = cc[i][j] == 'N' && cc[i][j + 1] == 'Z' ? j + 1 : nzr[i][j + 1];
znl[i][0] = -1;
for (j = 1; j < m; j++)
znl[i][j] = cc[i][j - 1] == 'Z' && cc[i][j] == 'N' ? j - 1 : znl[i][j - 1];
znr[i][m - 1] = m;
for (j = m - 2; j >= 0; j--)
znr[i][j] = cc[i][j] == 'Z' && cc[i][j + 1] == 'N' ? j + 1 : znr[i][j + 1];
}
for (j = 0; j < m; j++) {
nzu[0][j] = -1;
for (i = 1; i < n; i++)
nzu[i][j] = cc[i - 1][j] == 'N' && cc[i][j] == 'Z' ? i - 1 : nzu[i - 1][j];
nzd[n - 1][j] = n;
for (i = n - 2; i >= 0; i--)
nzd[i][j] = cc[i][j] == 'N' && cc[i + 1][j] == 'Z' ? i + 1 : nzd[i + 1][j];
znu[0][j] = -1;
for (i = 1; i < n; i++)
znu[i][j] = cc[i - 1][j] == 'Z' && cc[i][j] == 'N' ? i - 1 : znu[i - 1][j];
znd[n - 1][j] = n;
for (i = n - 2; i >= 0; i--)
znd[i][j] = cc[i][j] == 'Z' && cc[i + 1][j] == 'N' ? i + 1 : znd[i + 1][j];
}
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
kk[i + 1][j + 1] = cc[i][j] == 'N' ? 0 : 1;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
kk[i][j] += kk[i][j - 1];
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
kk[i][j] += kk[i - 1][j];
for (i1 = 0; i1 <= n; i1++)
for (j1 = 0; j1 <= m; j1++)
for (i2 = n; i2 >= i1; i2--)
memset(dp[i1][j1][i2], 0x3f, (m + 1) * sizeof *dp[i1][j1][i2]);
dp[0][0][n][m] = 0;
for (i1 = 0; i1 <= n; i1++)
for (j1 = 0; j1 <= m; j1++)
for (i2 = n; i2 >= i1; i2--)
for (j2 = m; j2 >= j1; j2--) {
int x = dp[i1][j1][i2][j2];
if (x == INF)
continue;
if (i1 + 1 < i2 && nzr[i1][j1] > j2 - 1)
dp[i1 + 1][j1][i2][j2] = min(dp[i1 + 1][j1][i2][j2], x + (j2 - j1));
if (i2 - 1 > i1 && znr[i2 - 1][j1] > j2 - 1)
dp[i1][j1][i2 - 1][j2] = min(dp[i1][j1][i2 - 1][j2], x + (j2 - j1));
if (j1 + 1 < j2 && nzd[i1][j1] > i2 - 1)
dp[i1][j1 + 1][i2][j2] = min(dp[i1][j1 + 1][i2][j2], x + (i2 - i1));
if (j2 - 1 > j1 && znd[i1][j2 - 1] > i2 - 1)
dp[i1][j1][i2][j2 - 1] = min(dp[i1][j1][i2][j2 - 1], x + (i2 - i1));
for (i = i1; i <= i2; i++)
for (j = j1; j <= j2; j++) {
int ul = rect(i1, j1, i, j) == (i - i1) * (j - j1);
int ur = rect(i1, j, i, j2) == 0;
int dl = rect(i, j1, i2, j) == 0;
int dr = rect(i, j, i2, j2) == (i2 - i) * (j2 - j);
if (ul && i1 < i && j1 < j)
ans[i - 1][j - 1] = min(ans[i - 1][j - 1], (x + (i - i1) * (j - j1)) * 2);
if (ur && i1 < i && j < j2)
ans[i - 1][j] = min(ans[i - 1][j], (x + (i - i1) * (j2 - j)) * 2);
if (dl && i < i2 && j1 < j)
ans[i][j - 1] = min(ans[i][j - 1], (x + (i2 - i) * (j - j1)) * 2);
if (dr && i < i2 && j < j2)
ans[i][j] = min(ans[i][j], (x + (i2 - i) * (j2 - j)) * 2);
if (ul && dr) {
dp[i1][j][i][j2] = min(dp[i1][j][i][j2], x + (i - i1) * (j - j1) + (i2 - i) * (j2 - j));
dp[i][j1][i2][j] = min(dp[i][j1][i2][j], x + (i - i1) * (j - j1) + (i2 - i) * (j2 - j));
}
if (ur && dl) {
dp[i1][j1][i][j] = min(dp[i1][j1][i][j], x + (i - i1) * (j2 - j) + (i2 - i) * (j - j1));
dp[i][j][i2][j2] = min(dp[i][j][i2][j2], x + (i - i1) * (j2 - j) + (i2 - i) * (j - j1));
}
}
}
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
printf("%d%c", ans[i][j] == INF ? -1 : ans[i][j], j + 1 < m ? ' ' : '\n');
return 0;
}
Compilation message
sandwich.c: In function 'main':
sandwich.c:21:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
sandwich.c:23:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf("%s", cc[i]);
| ^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
16 ms |
22156 KB |
Output is correct |
7 |
Correct |
1734 ms |
20820 KB |
Output is correct |
8 |
Correct |
2576 ms |
22136 KB |
Output is correct |
9 |
Correct |
767 ms |
16900 KB |
Output is correct |
10 |
Correct |
974 ms |
22136 KB |
Output is correct |
11 |
Correct |
16 ms |
22160 KB |
Output is correct |
12 |
Correct |
8 ms |
11792 KB |
Output is correct |
13 |
Correct |
17 ms |
22160 KB |
Output is correct |
14 |
Correct |
19 ms |
22100 KB |
Output is correct |
15 |
Correct |
7074 ms |
22152 KB |
Output is correct |
16 |
Correct |
87 ms |
22144 KB |
Output is correct |
17 |
Correct |
76 ms |
22144 KB |
Output is correct |
18 |
Correct |
56 ms |
22144 KB |
Output is correct |
19 |
Correct |
123 ms |
22144 KB |
Output is correct |
20 |
Correct |
64 ms |
22100 KB |
Output is correct |
21 |
Correct |
31 ms |
22144 KB |
Output is correct |
22 |
Correct |
24 ms |
22160 KB |
Output is correct |
23 |
Correct |
21 ms |
22156 KB |
Output is correct |
24 |
Correct |
20 ms |
22160 KB |
Output is correct |
25 |
Correct |
22 ms |
22160 KB |
Output is correct |
26 |
Correct |
20 ms |
22164 KB |
Output is correct |
27 |
Correct |
72 ms |
22096 KB |
Output is correct |
28 |
Correct |
19 ms |
22160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
16 ms |
22156 KB |
Output is correct |
7 |
Correct |
1734 ms |
20820 KB |
Output is correct |
8 |
Correct |
2576 ms |
22136 KB |
Output is correct |
9 |
Correct |
767 ms |
16900 KB |
Output is correct |
10 |
Correct |
974 ms |
22136 KB |
Output is correct |
11 |
Correct |
16 ms |
22160 KB |
Output is correct |
12 |
Correct |
8 ms |
11792 KB |
Output is correct |
13 |
Correct |
17 ms |
22160 KB |
Output is correct |
14 |
Correct |
19 ms |
22100 KB |
Output is correct |
15 |
Correct |
7074 ms |
22152 KB |
Output is correct |
16 |
Correct |
87 ms |
22144 KB |
Output is correct |
17 |
Correct |
76 ms |
22144 KB |
Output is correct |
18 |
Correct |
56 ms |
22144 KB |
Output is correct |
19 |
Correct |
123 ms |
22144 KB |
Output is correct |
20 |
Correct |
64 ms |
22100 KB |
Output is correct |
21 |
Correct |
31 ms |
22144 KB |
Output is correct |
22 |
Correct |
24 ms |
22160 KB |
Output is correct |
23 |
Correct |
21 ms |
22156 KB |
Output is correct |
24 |
Correct |
20 ms |
22160 KB |
Output is correct |
25 |
Correct |
22 ms |
22160 KB |
Output is correct |
26 |
Correct |
20 ms |
22164 KB |
Output is correct |
27 |
Correct |
72 ms |
22096 KB |
Output is correct |
28 |
Correct |
19 ms |
22160 KB |
Output is correct |
29 |
Incorrect |
387 ms |
2860 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |