Submission #55409

# Submission time Handle Problem Language Result Execution time Memory
55409 2018-07-07T08:05:39 Z 강태규(#1543) Sandwich (JOI16_sandwich) C++11
100 / 100
3185 ms 26140 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;
 
int r, c;
char grid[501][501];

struct _edge {
    int t, x, y;
};

_edge edge[2][501][501][2];
int used[500][500];
int fin[500][500];

int imin(int x, int y) {
    return x < y ? x : y;
}

const int inf = 1e6;
int dfs(int t, int x, int y) {
    if (fin[x][y]) return 0;
    if (used[x][y]) return inf;
    used[x][y] = 1;
    int ret = 1;
    
    for (int i = 0; i < 2; ++i) {
        if (edge[t][x][y][i].t == -1) continue;
        _edge j = edge[t][x][y][i];
        ret += dfs(j.t, j.x, j.y);
        if (ret > inf) break;
    }
    fin[x][y] = 1;
    return imin(ret, inf);
}

int ans[500][500];
char buf[10000];

int main() {
    scanf("%d%d", &r, &c);
    for (int i = 0; i < r; ++i) {
        scanf("%s", grid[i]);
    }
    for (int i = 0; i < r; ++i) {
        for (int j = 0; j < c; ++j) {
            edge[0][i][j][0] = edge[0][i][j][1] = edge[1][i][j][0] = edge[1][i][j][1] = { -1, -1, -1 };
            if (j) edge[0][i][j][0] = { 0, i, j - 1 };
            if (i + 1 < r) edge[grid[i][j] != 'N'][i][j][1] = { grid[i + 1][j] != 'N', i + 1, j };
            if (j + 1 < c) edge[1][i][j][0] = { 1, i, j + 1 };
            if (i) edge[grid[i][j] != 'Z'][i][j][1] = { grid[i - 1][j] != 'Z', i - 1, j };
        }
    }
    for (int i = 0; i < r; ++i) {
        for (int j = 0; j < r; ++j) {
            for (int k = 0; k < c; ++k) {
                used[j][k] = 0;
                fin[j][k] = 0;
            }
        }
        int sum = 0;
        for (int j = 0; j < c; ++j) {
            if (sum < inf) sum += dfs(0, i, j);
            sum = imin(sum, inf);
            ans[i][j] = sum;
        }
    }
    
    for (int i = 0; i < r; ++i) {
        for (int j = 0; j < r; ++j) {
            for (int k = 0; k < c; ++k) {
                used[j][k] = 0;
                fin[j][k] = 0;
            }
        }
        int sum = 0;
        for (int j = c; j--; ) {
            if (sum < inf) sum += dfs(1, i, j);
            sum = imin(sum, inf);
            ans[i][j] = imin(ans[i][j], sum);
        }
    }
    for (int i = 0; i < r; ++i) {
        for (int j = 0; j < c; ++j) {
            printf("%d ", ans[i][j] < inf ? 2 * ans[i][j] : -1);
        }
        printf("\n");
    }
    
	return 0;
}

Compilation message

sandwich.cpp: In function 'int main()':
sandwich.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &r, &c);
     ~~~~~^~~~~~~~~~~~~~~~
sandwich.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s", grid[i]);
         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 440 KB Output is correct
3 Correct 4 ms 1164 KB Output is correct
4 Correct 2 ms 1164 KB Output is correct
5 Correct 3 ms 1240 KB Output is correct
6 Correct 4 ms 1296 KB Output is correct
7 Correct 6 ms 1296 KB Output is correct
8 Correct 6 ms 1448 KB Output is correct
9 Correct 5 ms 1448 KB Output is correct
10 Correct 5 ms 1448 KB Output is correct
11 Correct 8 ms 1448 KB Output is correct
12 Correct 5 ms 1448 KB Output is correct
13 Correct 7 ms 1452 KB Output is correct
14 Correct 7 ms 1452 KB Output is correct
15 Correct 6 ms 1452 KB Output is correct
16 Correct 6 ms 1452 KB Output is correct
17 Correct 4 ms 1452 KB Output is correct
18 Correct 6 ms 1452 KB Output is correct
19 Correct 5 ms 1452 KB Output is correct
20 Correct 5 ms 1472 KB Output is correct
21 Correct 5 ms 1472 KB Output is correct
22 Correct 6 ms 1472 KB Output is correct
23 Correct 6 ms 1472 KB Output is correct
24 Correct 9 ms 1472 KB Output is correct
25 Correct 7 ms 1500 KB Output is correct
26 Correct 7 ms 1532 KB Output is correct
27 Correct 7 ms 1692 KB Output is correct
28 Correct 7 ms 1728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 440 KB Output is correct
3 Correct 4 ms 1164 KB Output is correct
4 Correct 2 ms 1164 KB Output is correct
5 Correct 3 ms 1240 KB Output is correct
6 Correct 4 ms 1296 KB Output is correct
7 Correct 6 ms 1296 KB Output is correct
8 Correct 6 ms 1448 KB Output is correct
9 Correct 5 ms 1448 KB Output is correct
10 Correct 5 ms 1448 KB Output is correct
11 Correct 8 ms 1448 KB Output is correct
12 Correct 5 ms 1448 KB Output is correct
13 Correct 7 ms 1452 KB Output is correct
14 Correct 7 ms 1452 KB Output is correct
15 Correct 6 ms 1452 KB Output is correct
16 Correct 6 ms 1452 KB Output is correct
17 Correct 4 ms 1452 KB Output is correct
18 Correct 6 ms 1452 KB Output is correct
19 Correct 5 ms 1452 KB Output is correct
20 Correct 5 ms 1472 KB Output is correct
21 Correct 5 ms 1472 KB Output is correct
22 Correct 6 ms 1472 KB Output is correct
23 Correct 6 ms 1472 KB Output is correct
24 Correct 9 ms 1472 KB Output is correct
25 Correct 7 ms 1500 KB Output is correct
26 Correct 7 ms 1532 KB Output is correct
27 Correct 7 ms 1692 KB Output is correct
28 Correct 7 ms 1728 KB Output is correct
29 Correct 2 ms 1728 KB Output is correct
30 Correct 10 ms 6460 KB Output is correct
31 Correct 227 ms 13384 KB Output is correct
32 Correct 233 ms 13384 KB Output is correct
33 Correct 73 ms 13384 KB Output is correct
34 Correct 2925 ms 13584 KB Output is correct
35 Correct 1808 ms 13584 KB Output is correct
36 Correct 35 ms 13584 KB Output is correct
37 Correct 2180 ms 13628 KB Output is correct
38 Correct 1791 ms 13628 KB Output is correct
39 Correct 2215 ms 13628 KB Output is correct
40 Correct 3185 ms 13900 KB Output is correct
41 Correct 860 ms 13900 KB Output is correct
42 Correct 3000 ms 14196 KB Output is correct
43 Correct 2562 ms 14196 KB Output is correct
44 Correct 2600 ms 14196 KB Output is correct
45 Correct 2564 ms 14196 KB Output is correct
46 Correct 1367 ms 14196 KB Output is correct
47 Correct 1866 ms 14196 KB Output is correct
48 Correct 983 ms 14196 KB Output is correct
49 Correct 1485 ms 14196 KB Output is correct
50 Correct 1733 ms 14196 KB Output is correct
51 Correct 1528 ms 14196 KB Output is correct
52 Correct 1286 ms 14196 KB Output is correct
53 Correct 1541 ms 14196 KB Output is correct
54 Correct 1599 ms 14196 KB Output is correct
55 Correct 1606 ms 14196 KB Output is correct
56 Correct 1626 ms 14196 KB Output is correct
57 Correct 1317 ms 14196 KB Output is correct
58 Correct 2309 ms 14196 KB Output is correct
59 Correct 3064 ms 14196 KB Output is correct
60 Correct 1699 ms 14196 KB Output is correct
61 Correct 2162 ms 14196 KB Output is correct
62 Correct 1966 ms 26140 KB Output is correct
63 Correct 1949 ms 26140 KB Output is correct