Submission

Status:

[PPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: Neozaawwman1

Problemset: fireball

Language: cpp

Time: 0.003 second

Submitted On: 2025-12-20 22:58:19

#include <bits/stdc++.h>
using namespace std;

int N, M, Q;
const int ex = 1005;
int mp[ex][ex];
int ct = 0;
queue<pair<int,int>> q;

int xx[4] = {-1,0,0,1};
int yy[4] = {0,-1,1,0};

void BFS(int x1, int y1){
    q.push({x1,y1});
    while(!q.empty()){
        int x,y;
        tie(x,y) = q.front(); q.pop();

        if(mp[y][x] == 0) continue; // ไม่ใช่ประเทศ
        mp[y][x] = 0;               // ประเทศถูกเผา
        ct--;

        for(int i=0;i<4;i++){
            int nx = x + xx[i];
            int ny = y + yy[i];
            if(nx<0 || ny<0 || nx>=M || ny>=N) continue;
            if(mp[ny][nx] == 0) continue;
            q.push({nx,ny});
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N >> M >> Q;

    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            cin >> mp[i][j];
            if(mp[i][j] == 1) ct++;   // 1 = ประเทศ
        }
    }

    for(int i=0;i<Q;i++){
        int a,b;
        cin >> a >> b;

        // แปลง 1-index → 0-index
        if(mp[a-1][b-1] == 1)
            BFS(b-1, a-1);

        cout << ct << "\n";
    }
}