답안 #306184

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
306184 2020-09-24T19:25:59 Z dsabolic Furniture (JOI20_furniture) C++17
100 / 100
593 ms 24440 KB
#include <bits/stdc++.h>

#define rep(i, a, n) for(int (i) = (a); (i) < (n); ++(i))

const int dx[2] = {0, 1};
const int dy[2] = {1, 0};

using namespace std;
using pii = pair<int, int>;

const int N = 1e3 + 50;

int n, m;
int mat[N][N];
int dp[N][N];
int dp2[N][N];
int dig[2 * N];

void purge(int i, int j) {
        dp[i][j] = 0;
        if(dp2[i][j]) dig[i + j]--;

        for(int x = 0; x < 2; ++x) {
                int nx = i + -dx[x], ny = j + -dy[x];

                if(nx < 0 || ny < 0) continue;

                if(dp[nx][ny] == 1) {
                        purge(nx, ny);
                } else if(dp[nx][ny] == 2) {
                        dp[nx][ny] = 1;
                }
        }
}

void purge2(int i, int j) {
        dp2[i][j] = 0;
        if(dp[i][j]) dig[i + j]--;

        for(int x = 0; x < 2; ++x) {
                int nx = i + dx[x], ny = j + dy[x];

                if(nx >= n || ny >= m) continue;

                if(dp2[nx][ny] == 1) purge2(nx, ny);
                else if(dp2[nx][ny] == 2) dp2[nx][ny]--;
        }
}

int main() {
        scanf("%d%d", &n, &m);

        rep(i, 0, n)
                rep(j, 0, m)
                        scanf("%d", &mat[i][j]);

        for(int i = n - 1; i >= 0; --i) {
                for(int j = m - 1; j >= 0; --j) {
                        if(i == n - 1 && j == m - 1) {
                                dp[i][j] = 1;
                                continue;
                        }
                        if(mat[i][j] == 1) continue;

                        dp[i][j] = (dp[i + 1][j] > 0) + (dp[i][j + 1] > 0);
                }
        }
        for(int i = 0; i < n; ++i) {
                for(int j = 0; j < m; ++j) {
                        if(!i && !j) {
                                dp2[i][j] = 1;
                                continue;
                        }
                        if(mat[i][j] == 1) continue;

                        if(i) dp2[i][j] += (dp2[i - 1][j] > 0);
                        if(j) dp2[i][j] += (dp2[i][j - 1] > 0);
                }
        }

        rep(i, 0, n) {
                rep(j, 0, m) {
                        //printf("%d ", dp[i][j]);
                        dig[i + j] += (dp[i][j] > 0 && dp2[i][j] > 0);
                        //if(i + j == 1) printf("dig %d\n", dig[i + j]);
                }
                //printf("\n");
        }

        int q;
        for(scanf("%d", &q); q; q--) {
                int x, y; scanf("%d%d", &x, &y);
                --x, --y;

                if(dig[x + y] == 1 && dp[x][y] && dp2[x][y]) {
                        printf("0\n");
                } else {
                        printf("1\n");

                        if(dp[x][y]) purge(x, y);
                        if(dp2[x][y]) purge2(x, y);
                }
        }

        return 0;
}

Compilation message

furniture.cpp: In function 'int main()':
furniture.cpp:3:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define rep(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
      |                              ^
furniture.cpp:53:9: note: in expansion of macro 'rep'
   53 |         rep(i, 0, n)
      |         ^~~
furniture.cpp:3:30: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    3 | #define rep(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
      |                              ^
furniture.cpp:54:17: note: in expansion of macro 'rep'
   54 |                 rep(j, 0, m)
      |                 ^~~
furniture.cpp:3:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    3 | #define rep(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
      |                              ^
furniture.cpp:81:9: note: in expansion of macro 'rep'
   81 |         rep(i, 0, n) {
      |         ^~~
furniture.cpp:3:30: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    3 | #define rep(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
      |                              ^
furniture.cpp:82:17: note: in expansion of macro 'rep'
   82 |                 rep(j, 0, m) {
      |                 ^~~
furniture.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   51 |         scanf("%d%d", &n, &m);
      |         ~~~~~^~~~~~~~~~~~~~~~
furniture.cpp:55:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   55 |                         scanf("%d", &mat[i][j]);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~
furniture.cpp:91:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   91 |         for(scanf("%d", &q); q; q--) {
      |             ~~~~~^~~~~~~~~~
furniture.cpp:92:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   92 |                 int x, y; scanf("%d%d", &x, &y);
      |                           ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1280 KB Output is correct
2 Correct 3 ms 1664 KB Output is correct
3 Correct 3 ms 1536 KB Output is correct
4 Correct 5 ms 1536 KB Output is correct
5 Correct 5 ms 1664 KB Output is correct
6 Correct 5 ms 1664 KB Output is correct
7 Correct 7 ms 1792 KB Output is correct
8 Correct 5 ms 1664 KB Output is correct
9 Correct 5 ms 1664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1280 KB Output is correct
2 Correct 3 ms 1664 KB Output is correct
3 Correct 3 ms 1536 KB Output is correct
4 Correct 5 ms 1536 KB Output is correct
5 Correct 5 ms 1664 KB Output is correct
6 Correct 5 ms 1664 KB Output is correct
7 Correct 7 ms 1792 KB Output is correct
8 Correct 5 ms 1664 KB Output is correct
9 Correct 5 ms 1664 KB Output is correct
10 Correct 18 ms 1408 KB Output is correct
11 Correct 4 ms 1024 KB Output is correct
12 Correct 266 ms 17400 KB Output is correct
13 Correct 104 ms 13944 KB Output is correct
14 Correct 499 ms 21508 KB Output is correct
15 Correct 483 ms 21752 KB Output is correct
16 Correct 555 ms 22904 KB Output is correct
17 Correct 575 ms 23928 KB Output is correct
18 Correct 517 ms 23288 KB Output is correct
19 Correct 593 ms 24312 KB Output is correct
20 Correct 411 ms 24440 KB Output is correct
21 Correct 512 ms 24312 KB Output is correct