#C4943. Collect All Items
Collect All Items
Collect All Items
You are given an \(M \times N\) grid where each cell is represented by an integer. The values in the grid have the following meaning:
- \(0\): An empty cell.
- \(1\): A wall through which the player cannot pass.
- \(2\): The player's starting position.
- \(3\): An item that needs to be collected.
The player can move in four directions: up, down, left, and right. The player cannot move through walls. Your task is to determine whether it is possible for the player to eventually collect all items in the grid.
If the player can visit all cells containing items using valid moves, print YES
; otherwise, print NO
.
Note: You must read the grid from standard input and output the answer to standard output.
inputFormat
The first line contains two integers \(M\) and \(N\) representing the number of rows and columns of the grid, respectively. Each of the following \(M\) lines contains \(N\) space-separated integers representing the grid.
For example:
5 5 0 0 0 0 0 0 1 1 1 0 0 1 2 1 0 0 1 0 1 3 0 0 3 0 0
outputFormat
Output a single line with either YES
if the player can collect all items or NO
otherwise.
5 5
0 0 0 0 0
0 1 1 1 0
0 1 2 1 0
0 1 0 1 3
0 0 3 0 0
YES