A Number Searching Problem
View as PDF has recently gotten bored of word searches, and decided to search for numbers instead! He is given an by
grid, with each cell being a random integer from
to
inclusive. To solve the number search, he must find a path of numbers that goes from
. More specifically, a path is valid if a cell with number
is adjacent to a cell with
for all
from
to
along the path.
Help determine if there exists a solution to the number search!
Input Specification
The first line contains the integer
.
The next lines contains
spaced integers, where each integer is from
to
inclusive.
Output Specification
Output YES if there exists a solution to the number search, otherwise output NO.
Sample Input 1
4
3 2 1 4
2 3 1 1
1 3 2 2
3 4 1 4
Output for Sample Input 1
YES
Sample Input 2
3
2 1 3
1 2 2
3 1 3
Output for Sample Input 2
NO
Comments