제출 #1180162

#제출 시각아이디문제언어결과실행 시간메모리
1180162NAMINFurniture (JOI20_furniture)C++20
0 / 100
2 ms1608 KiB
#include <bits/stdc++.h> #define endl "\n" #define ll long long using namespace std; const int mxN = 1004; int N,M; int G[mxN][mxN]; int coli[mxN][mxN][4]; vector<int> depy{-1,0,1,0,1,-1,1}; vector<int> depx{0,-1,0,1,1,1,-1}; bool ok(int x,int y){ return x >= 0 && x < M && y >= 0 && y < N; } void dfs(int x,int y,int val){ if(coli[y][x][val]) return; coli[y][x][val] = 1; for(int d=0;d<7;d++){ int dx = x+depx[d],dy = y+depy[d]; if(!ok(dx,dy)) continue; if(!G[dy][dx]) continue; dfs(dx,dy,val); } } void fillL(int x,int y){ queue<pair<int,int>> q; q.push(make_pair(x,y)); while(!q.empty()){ x = q.front().first,y = q.front().second; q.pop(); while(x >= 0){ if(G[y][x]) break; if(ok(x,y+1) && !G[y+1][x]) break; if(ok(x+1,y-1) && G[y-1][x+1]){ q.push(make_pair(x,y-1)); } if(y == 0 && x == 0) break; G[y][x] = 1; x--; } } } void fillR(int x,int y){ queue<pair<int,int>> q; q.push(make_pair(x,y)); while(!q.empty()){ x = q.front().first,y = q.front().second; q.pop(); while(x < M){ if(G[y][x]) break; if(ok(x,y-1) && !G[y-1][x]) break; if(ok(x-1,y+1) && G[y+1][x-1]){ q.push(make_pair(x,y+1)); } if(y == N-1 && x == M-1) break; G[y][x] = 1; x++; } } } void update(int x,int y){ // add block to make the extrems bigger if(x+1 < M && !G[y][x+1]){ x++; if((!ok(x,y-1) || G[y-1][x]) && (!ok(x-1,y) || (G[y][x-1]))){ fillR(x,y); } x--; } if(x-1 >= 0 && !G[y][x-1]){ x--; if((!ok(x,y+1) || G[y+1][x]) && (!ok(x+1,y) || G[y][x+1])){ fillL(x,y); } x++; } if(y-1 >= 0 && !G[y-1][x]){ y--; if((!ok(x,y+1) || G[y+1][x]) && (!ok(x+1,y) || G[y][x+1])){ fillL(x,y); } if((!ok(x,y-1) || G[y-1][x]) && (!ok(x-1,y) || (G[y][x-1]))){ fillR(x,y); } y++; } if(y+1 < N && !G[y+1][x]){ y++; if((!ok(x,y+1) || G[y+1][x]) && (!ok(x+1,y) || G[y][x+1])){ fillL(x,y); } if((!ok(x,y-1) || G[y-1][x]) && (!ok(x-1,y) || (G[y][x-1]))){ fillR(x,y); } y--; } } void update2(){ for(int y=0;y<N;y++){ for(int x=0;x<M;x++){ if(y == 0 && x == 0) continue; if(y == N-1 && x == M-1) continue; if((!ok(x,y-1) || G[y-1][x]) && (!ok(x-1,y) || (G[y][x-1]))){ G[y][x] = 1; } } } for(int y=N-1;y>=0;y--){ for(int x=M-1;x>=0;x--){ if(y == 0 && x == 0) continue; if(y == N-1 && x == M-1) continue; if((!ok(x,y+1) || G[y+1][x]) && (!ok(x+1,y) || G[y][x+1])){ G[y][x] = 1; } } } } void solve(){ int left = 3,right = 1,up = 0,down=2; cin >> N >> M; for(int i=0;i<N;i++){ for(int j=0;j<M;j++){ for(int k=0;k<4;k++){ coli[i][j][k] = 0; } } } for(int i=0;i<N;i++){ for(int j=0;j<M;j++){ cin >> G[i][j]; } } /*for(int i=N-1;i>=0;i--){ for(int j=M-2;j>=0;j--){ if(G[i][j] || (i == 0 && j == 0)) continue; if((!ok(j,i-1) || G[i-1][j]) && (!ok(j-1,i) || (G[i][j-1]))){ fillR(j,i); } if((!ok(j,i+1) || G[i+1][j]) && (!ok(j+1,i) || G[i][j+1])){ fillL(j,i); } } }*/ update2(); for(int i=0;i<N;i++){ if(!coli[i][0][left] && G[i][0]) dfs(0,i,left); if(!coli[i][M-1][right] && G[i][M-1]) dfs(M-1,i,right); } for(int i=0;i<M;i++){ if(!coli[0][i][up] && G[0][i]) dfs(i,0,up); if(!coli[N-1][i][down] && G[N-1][i]) dfs(i,N-1,down); } int Q; cin >> Q; while(Q--){ int y,x; cin >> y >> x; y--,x--; if(G[y][x]){ cout << 1 << endl; continue; } vector<int> curcoli(4,0); if(y == N-1) curcoli[down] = true; if(y == 0) curcoli[up] = true; if(x == 0) curcoli[left] = true; if(x == M-1) curcoli[right] = true; for(int d=0;d<7;d++){ int dx = x+depx[d],dy = y+depy[d]; if(!ok(dx,dy)) continue; if(!G[dy][dx]) continue; for(int k=0;k<4;k++){ curcoli[k] |= coli[dy][dx][k]; } } bool can = true; if(curcoli[left] && curcoli[up]) can = false; else if(curcoli[left] && curcoli[right]) can = false; else if(curcoli[down] && curcoli[up]) can = false; else if(curcoli[right] && curcoli[down]) can = false; if(can){ cout << 1 << endl; G[y][x] = 1; //update2(); update(x,y); for(int k=0;k<4;k++){ if(curcoli[k]){ dfs(x,y,k); } } } else{ cout << 0 << endl; } } } int main(){ ios::sync_with_stdio(false); cin.tie(0); int t = 1; //cin >> t; while(t--){ solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...