답안 #436876

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
436876 2021-06-25T07:18:43 Z qwerasdfzxcl Sandwich (JOI16_sandwich) C++14
35 / 100
8000 ms 44364 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
struct vertex{
    int x, y, z;
    vertex(){}
    vertex(int _x, int _y, int _z): x(_x), y(_y), z(_z) {}
};
vector<vertex> adj[404][404][2];
char matrix[404][404];
bool visited[404][404][2];
int deg[404][404][2], dp[404][404][2], dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1}, n, m;

bool valid(int x, int y){
    return ((1<=x && x<=n) && (1<=y && y<=m));
}

int count_visited(){
    int ret = 0, cnt = 0;
    queue<vertex> q;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            if (visited[i][j][0] || visited[i][j][1]) ret++;
            if (visited[i][j][0] && !deg[i][j][0]) q.push(vertex(i, j, 0));
            if (visited[i][j][1] && !deg[i][j][1]) q.push(vertex(i, j, 1));
        }
    }
    while(!q.empty()){
        auto v = q.front(); q.pop();
        //printf("%d %d %d\n", v.x, v.y, v.z);
        cnt++;
        for (auto &w:adj[v.x][v.y][v.z]){
            deg[w.x][w.y][w.z]--;
            if (!deg[w.x][w.y][w.z]) q.push(w);
        }
    }
    //printf("%d %d\n", ret, cnt);
    if (ret==cnt) return ret*2;
    return 1e9;
}

void dfs(int x, int y, int c){
    if (visited[x][y][c]) return;
    visited[x][y][c] = 1;
    int nx1 = x+dx[c], nx2 = y+dy[c];
    if (valid(nx1, nx2)){
        adj[x][y][c].emplace_back(nx1, nx2, c);
        deg[nx1][nx2][c]++;
        dfs(nx1, nx2, c);
    }
    int d = 2;
    if (!c && matrix[x][y]=='Z') d = 3;
    if (c && matrix[x][y]=='N') d = 3;
    int nx3 = x+dx[d], nx4 = y+dy[d];
    if (valid(nx3, nx4)){
        adj[x][y][c].emplace_back(nx3, nx4, 0);
        if (d==2 && matrix[nx3][nx4]=='N'){
            deg[nx3][nx4][0]++;
            dfs(nx3, nx4, 0);
        }
        else if (d==3 && matrix[nx3][nx4]=='Z'){
            deg[nx3][nx4][0]++;
            dfs(nx3, nx4, 0);
        }
        else{
            deg[nx3][nx4][1]++, adj[x][y][c].back().z = 1;
            dfs(nx3, nx4, 1);
        }
    }
}

void init(){
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            visited[i][j][0] = visited[i][j][1] = 0;
            deg[i][j][0] = deg[i][j][1] = 0;
            adj[i][j][0].clear();
            adj[i][j][1].clear();
        }
    }
}

int main(){
    scanf("%d %d", &n, &m);
    for (int i=1;i<=n;i++){
        scanf("%s", matrix[i]+1);
        for (int j=1;j<=m;j++) dp[i][j][0] = dp[i][j][1] = 1e9;
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            init(); dfs(i, j, 0);
            dp[i][j][0] = count_visited();
            init(); dfs(i, j, 1);
            dp[i][j][1] = count_visited();
            //printf("%d %d: %d %d\n", i, j, dp[i][j][0], dp[i][j][1]);
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            int ans = min(dp[i][j][0], dp[i][j][1]);
            if (ans==1e9) ans = -1;
            printf("%d ", ans);
        }
        printf("\n");
    }
    return 0;
}

Compilation message

sandwich.cpp: In function 'int main()':
sandwich.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
sandwich.cpp:87:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |         scanf("%s", matrix[i]+1);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7884 KB Output is correct
2 Correct 6 ms 7992 KB Output is correct
3 Correct 6 ms 8268 KB Output is correct
4 Correct 6 ms 7884 KB Output is correct
5 Correct 7 ms 8268 KB Output is correct
6 Correct 1045 ms 8920 KB Output is correct
7 Correct 422 ms 8452 KB Output is correct
8 Correct 462 ms 8492 KB Output is correct
9 Correct 228 ms 8404 KB Output is correct
10 Correct 543 ms 8492 KB Output is correct
11 Correct 744 ms 8520 KB Output is correct
12 Correct 271 ms 8332 KB Output is correct
13 Correct 747 ms 8596 KB Output is correct
14 Correct 760 ms 8572 KB Output is correct
15 Correct 231 ms 8456 KB Output is correct
16 Correct 365 ms 8460 KB Output is correct
17 Correct 371 ms 8460 KB Output is correct
18 Correct 376 ms 8596 KB Output is correct
19 Correct 372 ms 8396 KB Output is correct
20 Correct 254 ms 8496 KB Output is correct
21 Correct 247 ms 8516 KB Output is correct
22 Correct 256 ms 8456 KB Output is correct
23 Correct 251 ms 8596 KB Output is correct
24 Correct 751 ms 8524 KB Output is correct
25 Correct 724 ms 8656 KB Output is correct
26 Correct 773 ms 8652 KB Output is correct
27 Correct 394 ms 8428 KB Output is correct
28 Correct 683 ms 8512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7884 KB Output is correct
2 Correct 6 ms 7992 KB Output is correct
3 Correct 6 ms 8268 KB Output is correct
4 Correct 6 ms 7884 KB Output is correct
5 Correct 7 ms 8268 KB Output is correct
6 Correct 1045 ms 8920 KB Output is correct
7 Correct 422 ms 8452 KB Output is correct
8 Correct 462 ms 8492 KB Output is correct
9 Correct 228 ms 8404 KB Output is correct
10 Correct 543 ms 8492 KB Output is correct
11 Correct 744 ms 8520 KB Output is correct
12 Correct 271 ms 8332 KB Output is correct
13 Correct 747 ms 8596 KB Output is correct
14 Correct 760 ms 8572 KB Output is correct
15 Correct 231 ms 8456 KB Output is correct
16 Correct 365 ms 8460 KB Output is correct
17 Correct 371 ms 8460 KB Output is correct
18 Correct 376 ms 8596 KB Output is correct
19 Correct 372 ms 8396 KB Output is correct
20 Correct 254 ms 8496 KB Output is correct
21 Correct 247 ms 8516 KB Output is correct
22 Correct 256 ms 8456 KB Output is correct
23 Correct 251 ms 8596 KB Output is correct
24 Correct 751 ms 8524 KB Output is correct
25 Correct 724 ms 8656 KB Output is correct
26 Correct 773 ms 8652 KB Output is correct
27 Correct 394 ms 8428 KB Output is correct
28 Correct 683 ms 8512 KB Output is correct
29 Correct 15 ms 7964 KB Output is correct
30 Correct 19 ms 11048 KB Output is correct
31 Execution timed out 8061 ms 44364 KB Time limit exceeded
32 Halted 0 ms 0 KB -