The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 398 Weeks Ago, 3 Days Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
You have a rectangular grid of size N × M, namely, there are cells (x, y) for 1 = x = N, 1 = y = M. Two cells are adjacent if they share a side. More formally, two cells (x1, y1), (x2, y2) are adjacent if |x1 - x2| + |y1 - y2| = 1. Between two adjacent cells there can be a wall. Two cells a and b are connected if there is a way between them (in other words there is a sequence of cells c1, c2, ..., ck that c1 = a, ck = b, and for each 1 = i , ci, ci+1 are adjacent cells without wall between them).
Now you are given Q queries, each of whom is of following four types.
You must assume that there are no walls on the grid before the queries.
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains three space-separated integers N, M and Q, denoting the grid sizes and amount of queries. For next Q lines, each line contains a query. The format of queries are the same as described by the statement.
For each test case, output an integer, denoting the sum of answers for queries of type 3 and 4. Note that you just print only the sum of the answers for each test case.
Subtask 1: (15 points)
Subtask 2: (15 points)
Subtask 3: (70 points)
Query 1. There are a lot of ways from (1,1) to (3,4). One of them is marked in red in the picture #1. So these are connected, then Answer = 1 in this query.
Query 2. In the picture #2 you can see the grid after second query.
Query 3. In the picture #3 you can see the grid after third query.
Query 4. In the picture #4 you can see the grid after fourth query.
Query 5. In the picture #5 the largest connected component marked in green. Thus Answer = 12.
Query 6. In the picture #6 you can see the grid after sixth query.
Query 7. In the picture #7 the largest connected component marked in green. Thus Answer = 7.
Query 8. As you can see in the picture #8, there are no ways from (1,1) to (3,4). So they are no longer connected, then Answer = 0.
Query 9. In the picture #9 you can see the grid after ninth query. There is no difference between it and the grid after the sixth query, because before the ninth query the wall was already built.
Query 10. When a = b, two cells a and b are always connected. Thus Answer = 1.
Therefore, the sum of the answers is 1 + 12 + 7 + 0 + 1 = 21, which you should print.

Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n.P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll