We Are On Fire
Practice
4.4 (24 votes)
Approved
Breadth first search
Depth first search
Graphs
Medium
Open
Problem
60% Success 8970 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

An intergallactic war is on. Aliens are throwing fire-balls at our planet and this fire is so deadly that whichever nation it hits, it will wipe out not only that nation, but also spreads to any other nation which lies adjacent to it.

Given an NxM map which has N x M cells. Each cell may have a country or else it may be the ocean. Fire cannot penetrate the ocean or go beyond the edges of the map (a wise man managed to bound the corners preventing fire from wrapping around the corners).

You will be given a initial state of the planet represented by an N x M grid consists of 0 or 1 representing ocean and nation respectively. Also there will be Q attacks of the fire-ball. After each query, you are to tell the number of nations still left unburnt.

Input:

First line of input contains 3 space separated integers N,M,Q where N x M is the size of the planet. Q is the number of fire-ball attacks.
N lines follow each with M values of either 0 or 1, denoting whether that particular coordinate is a nation(1) or ocean(0).
Q lines follow. Each line has a coordinate X,Y where the next fire-ball attack takes place.

Output:

For each Q, output the number of nations still existing on a single line.

Note:

  • Two countries are said to be adjacent if they share an edge.
  • Aliens can shoot the fireball at any place on the planet within its domain any number of times.
  • Once a nation is destroyed, it will continue to remain destroyed over all the forthcoming queries.
  • Large IO. Prefer scanf/printf(in C/C++).

Constraints:

  • 1 ≤ N,M ≤ 103
  • 1 ≤ X ≤ N
  • 1 ≤ Y ≤ M
  • 1 ≤ Q ≤ 106

Scoring:

  • 1 ≤ N,M ≤ 102 , Q ≤ 103: (30 pts)
  • Original Constraints : (70 pts)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
4 votes
Tags:
ImplementationGraphsAlgorithmsBrute ForceBreadth-first search
Points:30
8 votes
Tags:
GraphsAlgorithmsBreadth-first search
Points:10
166 votes
Tags:
ReadyBasic ProgrammingApprovedVery EasyVery-Easy