답안 #358532

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
358532 2021-01-25T17:09:09 Z BartolM Furniture (JOI20_furniture) C++17
100 / 100
547 ms 19888 KB
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=1005;

int n, m, q;
int mat[N][N];
int dp[N][N], cnt[2*N];
int dx[4]={-1, 0, 1, 0};
int dy[4]={0, 1, 0, -1};
queue <pii> Q;

int ok(int x, int y) {
    if (x<=0 || y<=0 || x>n || y>m) return 0;
    if (!dp[x][y]) return 0;
    return (!dp[x-1][y] && !dp[x][y-1]) || (!dp[x+1][y] && !dp[x][y+1]);
}

void bfs(int x, int y) {
    Q.push(mp(x, y));
    while (!Q.empty()) {
        pii pp=Q.front();
        Q.pop();
        for (int i=0; i<4; ++i) {
            int nx=pp.X+dx[i], ny=pp.Y+dy[i];
            if (!ok(nx, ny)) continue;
            Q.push(mp(nx, ny));
            dp[nx][ny]=0;
            cnt[nx+ny]--;
        }
    }
}

void ispis() {
    printf("-----------------------\n");
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=m; ++j) {
            printf("%d ", dp[i][j]);
        }
        printf("\n");
    }
    printf("----------------------------\n");
}

void solve() {
    dp[0][1]=1;
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=m; ++j) {
            dp[i][j]=(dp[i-1][j] || dp[i][j-1]) && mat[i][j];
        }
    }
    dp[n+1][m]=1;
    for (int i=n; i>=1; --i) {
        for (int j=m; j>=1; --j) {
            dp[i][j]&=dp[i+1][j] || dp[i][j+1];
            cnt[i+j]+=dp[i][j];
        }
    }

    while (q--) {
        int x, y;
        scanf("%d %d", &x, &y);
//        ispis();
        if (!dp[x][y]) {
            mat[x][y]=0;
            printf("1\n");
            continue;
        }
        cnt[x+y]--;
        if (!cnt[x+y]) {
            cnt[x+y]++;
            printf("0\n");
            continue;
        }
        printf("1\n");
        dp[x][y]=0; mat[x][y]=0;
        bfs(x, y);
    }
}

void load() {
    scanf("%d %d", &n, &m);
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=m; ++j) {
            scanf("%d", &mat[i][j]);
            mat[i][j]^=1;
        }
    }
    scanf("%d", &q);
}

int main() {
    load();
    solve();
    return 0;
}

Compilation message

furniture.cpp: In function 'void solve()':
furniture.cpp:74:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   74 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
furniture.cpp: In function 'void load()':
furniture.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   94 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
furniture.cpp:97:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   97 |             scanf("%d", &mat[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~
furniture.cpp:101:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  101 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 876 KB Output is correct
2 Correct 2 ms 1132 KB Output is correct
3 Correct 3 ms 1004 KB Output is correct
4 Correct 4 ms 1132 KB Output is correct
5 Correct 4 ms 1132 KB Output is correct
6 Correct 5 ms 1132 KB Output is correct
7 Correct 5 ms 1132 KB Output is correct
8 Correct 5 ms 1132 KB Output is correct
9 Correct 5 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 876 KB Output is correct
2 Correct 2 ms 1132 KB Output is correct
3 Correct 3 ms 1004 KB Output is correct
4 Correct 4 ms 1132 KB Output is correct
5 Correct 4 ms 1132 KB Output is correct
6 Correct 5 ms 1132 KB Output is correct
7 Correct 5 ms 1132 KB Output is correct
8 Correct 5 ms 1132 KB Output is correct
9 Correct 5 ms 1132 KB Output is correct
10 Correct 21 ms 876 KB Output is correct
11 Correct 4 ms 748 KB Output is correct
12 Correct 263 ms 8812 KB Output is correct
13 Correct 113 ms 7916 KB Output is correct
14 Correct 435 ms 9312 KB Output is correct
15 Correct 453 ms 17388 KB Output is correct
16 Correct 541 ms 18576 KB Output is correct
17 Correct 507 ms 19564 KB Output is correct
18 Correct 465 ms 18796 KB Output is correct
19 Correct 500 ms 19820 KB Output is correct
20 Correct 377 ms 19820 KB Output is correct
21 Correct 547 ms 19888 KB Output is correct