#C1174. Grid Pathfinding with Blocked Cells

    ID: 41089 Type: Default 1000ms 256MiB

Grid Pathfinding with Blocked Cells

Grid Pathfinding with Blocked Cells

Given a grid with n rows and m columns, some cells are blocked. You are to determine whether there exists a path from the top-left cell \((1,1)\) to the bottom-right cell \((n,m)\). You can move only in the four primary directions: up, down, left, and right. A cell once visited or blocked cannot be revisited or passed through. This problem can be solved using a breadth-first search (BFS) algorithm.

Please note that if the starting cell or the destination cell is blocked, the answer is automatically \(NO\).

inputFormat

The input is given via stdin in the following format:

  • The first line contains two integers \(n\) and \(m\) separated by a space, representing the number of rows and columns of the grid respectively.
  • The second line contains an integer \(k\), the number of blocked cells.
  • The following \(k\) lines each contain two integers \(r\) and \(c\), representing the row and column indices of a blocked cell.

It is guaranteed that \(1 \leq r \leq n\) and \(1 \leq c \leq m\).

outputFormat

Output via stdout a single line: YES if a path exists from the top-left cell to the bottom-right cell, or NO otherwise.

## sample
3 3
3
1 2
2 2
3 2
NO

</p>