/ OPS / 题库 /

Puzzle

Puzzle

#描述#
cryboy likes puzzles a lot! Just a few days ago, he found out about the N*M puzzle. For this puzzle, you have all the numbers from 0 to N*M-1 arranged in n rows and m columns. You are allowed to switch two adjacent elements (horizontally or vertically), only if one of them has the value 0. For example, the 4x5 puzzle, the following state is a possible state of the puzzle:<br>
<pre>
 1 2 3 4
 5 6 7 8
 9 10 11 12
13 14 15 16
17 18 19 0
</pre>
<br>
Given the initial state and the final state of the puzzle, you have to decide whether there exists a sequence of moves which brings the puzzle in the initial state into the final state.<br>
<br>
<b>Input:</b><br>
The input contains multiple test cases, each case will be formatted in the following description:<br>
The first line contains two numbers: N, M(2≤N, M≤50).<br>
The following N lines, each of them contains M integers, describing the initial state of the puzzle.<br>
The next N lines, each of them contains M integers, describing the final state of the puzzle.<br>
<br>
<b>Output:</b><br>
For each case, you should print "YES" if the final state can be reached after several moves or "NO", if such a thing is impossible.

#格式#
##输入格式##

##输出格式##

#样例1#
##样例输入1##

4 4
1 2 3 4 
5 6 7 8 
9 10 11 12
13 14 15 0
1 2 3 4
5 6 7 8
9 10 11 0
13 14 15 12
2 3
1 2 3
4 5 0
1 2 0
4 3 5

##样例输出1##

YES
NO

#限制#
1000ms
32768KB

#提示#

#来源#
cryboy

信息

ID
1580
难度
5
分类
category1 点击显示
标签
递交数
0
已通过
0
通过率
?
上传者