Submission
Status:
[PPPPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: Few500
Problemset: fireball
Language: cpp
Time: 0.002 second
Submitted On: 2026-03-25 09:23:50
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, q, cnt = 0;
cin >> n >> m >> q;
vector<vector<int>> grid(n + 1, vector<int>(m + 1));
vector<vector<int>> burned(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
cin >> grid[i][j];
cnt += grid[i][j];
}
queue<pair<int, int>> fireball;
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
while (q--)
{
int j, k;
cin >> j >> k;
if(grid[j][k] && !burned[j][k]){
fireball.push({j, k});
burned[j][k] = 1;
cnt--;
}
while (!fireball.empty())
{
auto [x, y] = fireball.front();
fireball.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx < 1 || nx > n || ny < 1 || ny > m)
continue;
if (burned[nx][ny] || !grid[nx][ny])
continue;
burned[nx][ny] = 1;
cnt--;
fireball.push({nx, ny});
}
}
cout << cnt << '\n';
}
return 0;
}