Grid

4.2

31 votes
Algorithms, Easy, Graphs, Shortest Path Algorithm
Problem

You are given a grid A of size N×M, two integers (Si,Sj) and Q tasks. Each task contains two integers, (Di,Dj). Each cell in the grid is either empty cells denoted by O (Capital English character 'o') or occupied cells denoted by . Initially, you are at (Si,Sj). Find the minimum number of steps in which you have to take to reach (Di,Dj) from (Si,Sj) without traversing the occupied cells.

In a single step, you can move from any cell (i,j) to the 4 neighboring cells i.e. (i1,j), (i+1,j), (i,j1) and (i,j+1) provided these cells are inside the grid A.

Input Foramt
The first line of input contains 3 space separated integers N, M and Q where N and M denotes the dimensions of the grid A and Q denotes the number of tasks.
Each of the next N lines contain a string of length M, jth character in the ith line of which is either O or denoting that the cell (i,j) is empty or occupied.
The next line contains 2 space separated integers Si and Sj denoting the source cell of the grid.
This is followed by Q lines each containing 2 space separated integers Di and Dj.

Output Format
Print the answer to each query on a new line. If there is no path between (Si,Sj) and (Di,Dj) , print 1.

Constraints

  • 1N,M103
  • 1Q105
  • Ai,j is either O or
  • 1Si,DiN
  • 1Sj,DjM
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The shortest path from (2,2) to (1,1) is (2,2)->(2,1)->(1,1) consisting of 2 steps, hence the answer is 2.
No path exists from cell (2,2) to (1,2), hence the answer is 1.

Editor Image

?