luogu#P1141. 01迷宫

    ID: 141 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>搜索并查集广度优先搜索 BFS队列

01迷宫

Problem Description

There is an n×nn \times n grid maze consisting only of digits 00 and 11. If you are on a cell with 00, you may move to one of the 4 adjacent cells with 11. Similarly, if you are on a cell with 11, you may move to one of the 4 adjacent cells with 00.

Your task is: for the given maze, for each specified starting cell, determine how many cells are reachable (including the starting cell).

Input Format

The first line contains two positive integers n,mn,m.

The next nn lines each contain nn characters, each of which is either 00 or 11, with no spaces between characters.

Then follow mm lines. Each line contains two positive integers i,ji,j, referring to the cell at row ii, column jj of the maze, asking how many cells are reachable starting from this cell.

Output Format

Output mm lines. For each query, print the corresponding answer.

2 2
01
10
1 1
2 2

4
4

Hint

For the sample, all cells are mutually reachable.

  • For 20%20\% of the testdata, n10n \leq 10.
  • For 40%40\% of the testdata, n50n \leq 50.
  • For 50%50\% of the testdata, m5m \leq 5.
  • For 60%60\% of the testdata, n,m100n,m \leq 100.
  • For 100%100\% of the testdata, 1n10001\le n \leq 1000, 1m1000001\le m \leq 100000.

Translated by ChatGPT 5