이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
 
#define sherina ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define int long long
#define INF 1e18
#define endl '\n'
 
using namespace std;
int n , m , c[1005][1005] , q , x , y , aa[1005] , bb[1005] , diag[2005] , sa , sb;
bool inside(int a , int b){
   return 0 < a && a <= n && 0 < b && b <= m;
}
void rec(int x , int y){
    if((x != sa || y != sb) && (!inside(x , y) || c[x][y])) return;
    diag[x + y]--;
    c[x][y] = 1;
    if(c[x + 1][y - 1]){
        rec(x , y - 1);
        rec(x + 1 , y);
    }
    if(c[x - 1][y + 1]){
        //cout << x -1 << " " << y + 1<< endl;
        rec(x - 1 , y); 
        rec(x , y + 1);
    }
}
signed main(){
    cin >> n >> m;
    for(int i = 0 ; i <= n + 1 ; i++){
        c[i][0] = 1;
        c[i][m + 1] = 1;
    }
    for(int i = 0 ; i <= m + 1 ; i++){
        c[0][i] = 1;
        c[n + 1][i] = 1;
    }
    for(int i = 1 ;  i <= n ; i++){
        for(int j = 1 ; j <= m ; j++){
            cin >> c[i][j];
            diag[i + j]++;
            sa = i;
            sb = j;
            if(c[i][j]) rec(i , j);
        }
    }
    cin >> q;
    while(q--){
        cin >> x >> y;
        if(c[x][y]){
            cout << "1" << endl;
        }
        else if(diag[x + y] == 1){
            cout << "0" << endl;
        }
        else{
            cout <<"1" << endl;
            rec(x , y);
        }
    }
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |