MXN Puzzle
Time Limit: 4000MS |
|
Memory Limit: 131072K |
Total Submissions: 4112 |
|
Accepted: 1140 |
Description
The Eight Puzzle, among other sliding-tile puzzles, are one of the famous problems in artificial intelligence. Along with chess, tic-tac-toe and backgammon, it had been used to study search algorithms.
The Eight Puzzle can be generalized into a m x 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 to a M by N frame with one tile missing. For example, with M = 4 and N = 3, a puzzle
1 |
6 |
2 |
4 |
0 |
3 |
7 |
5 |
9 |
10 |
8 |
11 |
Let's call missing tile 0. The only legal operation are 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:
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
0 |
The following steps solve the puzzle given above.
START |
1 |
6 |
2 |
4 |
0 |
3 |
7 |
5 |
9 |
10 |
8 |
11 |
|
Down ? |
1 |
0 |
2 |
4 |
6 |
3 |
7 |
5 |
9 |
10 |
8 |
11 |
|
Left ? |
1 |
2 |
0 |
4 |
6 |
3 |
7 |
5 |
9 |
10 |
8 |
11 |
|
Up ? |
1 |
2 |
3 |
4 |
6 |
0 |
7 |
5 |
9 |
10 |
8 |
11 |
|
... |
|
Right ? |
1 |
2 |
3 |
4 |
0 |
6 |
7 |
5 |
9 |
10 |
8 |
11 |
|
Up ? |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
0 |
9 |
10 |
8 |
11 |
|
Up ? |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
0 |
11 |
|
Left ? |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
0 |
|
GOAL |
Given a M x N Puzzle, you is to determine whether it can be solved.
Input
The input consists of multiple test cases. Each test case is starts with a line containing M and n (2 ≤ m, n ≤999). This was followed by M lines containing n numbers each describing a M x n puzzle.
The input ends with a pair of zeroes which should is processed.
Output
Output one line for each test case containing a single word YES If the puzzle can is solved and NO oth Erwise.
Sample Input
3 31 0 34 2 57 8 64 31 2 54 6 911 8 103 7 00 0
Sample Output
YESNO
Test instructions: 8 Digital problem upgrade, is by moving the space (with 0 instead of) so that the original state into an orderly 1234 ... 0, however, this problem is n*m digital.
Puzzle: N*m are very big, search must not. Consider the final state, the actual number of reverse order is 0 of the state, and then four modes of operation are divided into: left and right to move, the original sequence of the reverse number does not affect; Move up and down as follows:
-------------0***********
X-------------
X is an arbitrary number, now to move the x up, then ***********, if there is a greater than x,b is less than x, then the number of reverse order after the move will be added to a b-a,x can affect this is the same, other than the other is unchanged.
Then, if the number of columns is even, then the number of ****** is odd, b,a parity, b-a is odd, so once moved, the original sequence of the reverse number of the odd and even change.
Given that the last 0 moves to the last row, the parity changes n-i times (i is 0 of the number of rows), just to determine whether the last is even.
Conversely, if the number of columns is odd, then the number of ****** is even, b,a parity, b-a is even, so after moving once, the original sequence of the reverse number of parity has not changed.
Because no matter how the move, the parity of the same, so that the first initial state of the parity must be consistent with the final state.
Test instructions from http://blog.csdn.net/tmeteorj/article/details/8530105
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<map>#include<stack>#include<queue>#include<vector>#defineINF 2e9#defineMet (b) memset (a,b,sizeof a)typedefLong Longll;using namespacestd;Const intN =999*999+5;Const intM = 4e5+5;intn,m,tot=0, cnt=0;intHead[n],ans[n];intTree[n];intA[n];voidAddintKintnum) { while(k<=999*999-1) {Tree[k]+=num; K+=k& (-k); }}intSum (intk) {intsum=0; while(k>0) {sum+=Tree[k]; K-=k& (-k); } returnsum;}intMain () { while(SCANF ("%d%d", &n,&m), n| |m) {intx,y,t,s=0, nu=0; for(intI=1; i<=n; i++) for(intj=1; j<=m; J + +) {scanf ("%d",&t); if(t==0) x=i,y=J; ElseA[nu++]=T; } Met (Tree,0); for(inti=nu-1; i>=0; i--) {s+=sum (a[i]-1); Add (A[i],1); } if(m&1) if(s&1) puts ("NO"); ElsePuts"YES"); Else if(((n-x) ^s) &1) puts ("NO"); ElsePuts"YES"); } return 0;}
POJ 2893 MXN Puzzle (tree-like array for inverse order)