![]() | ||||
Online Judge | Problem Set | Authors | Online Contests | User |
---|---|---|---|---|
Web Board Home Page F.A.Qs Announcement | Current Contest Past Contests Scheduled Contests Award Contest |
M ¡Á N Puzzle
Description The
Eight Puzzle, among other sliding-tile puzzles, is one of the famous
problems in artificial intelligence. Along with chess, tic-tac-toe and
backgammon, it has been used to study search algorithms. The Eight Puzzle can be generalized into an M ¡Á N Puzzle where at least one of M and N is odd. The puzzle is constructed with MN − 1 sliding tiles with each a number from 1 to MN − 1 on it packed into a M by N frame with one tile missing. For example, with M = 4 and N = 3, a puzzle may look like: Let's
call missing tile 0. The only legal operation is to exchange 0 and the
tile with which it shares an edge. The goal of the puzzle is to find a
sequence of legal operations that makes it look like: The following steps solve the puzzle given above. START DOWN UP ¡ RIGHT UP LEFT GOAL Given an M ¡Á N puzzle, you are to determine whether it can be solved. Input The input consists of multiple test cases. Each test case starts with a line containing M and N (2 ¡Ü M, N ¡Ü 999). This line is followed by M lines containing N numbers each describing an M ¡Á N puzzle. The input ends with a pair of zeroes which should not be processed. Output Output one line for each test case containing a single word YES if the puzzle can be solved and NO otherwise. Sample Input Sample Output Source |
[Submit] [Go Back] [Status] [Discuss]
All Copyright Reserved 2003-2005 Ying Fuchen,Xu Pengcheng
Any problem, Please Contact Administrator