Reprinted please indicate the source, thank youHttp://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove
Question: Give the initial and target States of the 24 grids, and then ask if they can be switched.
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4021
Idea: It looks like the famous "Eight Digital" problem. First, for the eight special locations (dead corners), if there is space here, it will be exchanged with adjacent locations, in this way, if the numbers in the two States correspond to different numbers in the dead corner, it is obvious that they cannot reach the specified state, because they cannot be replaced.
After the final corner is fixed, only the 4x4 Board is left, which becomes an extension of the eight digital problem-15 digital. First, let's think about how digital 8 judges the solution: First, write all the numbers (excluding the empty 0) into a row and get a 1 ~ The arrangement of 8, considering the gap switching situation: 1. Left and Right switching, 2. Up and down switching. For left and right exchanges, the number of reverse orders in the written order is not changed. For upper and lower exchanges, it is equivalent to jumping two positions forward or backward in the arrangement, so either two numbers are larger or smaller than them, so that the reverse order number is increased by 2 or reduced by 2, or the two numbers are bigger than the other two, so that the reverse order number remains unchanged. In summary, for the eight digital problems, the operation will not change the parity of the number of reverse orders, so only the initial status and the specified status have the same parity.
After figuring out 8 digital, it's easy to scale up, and now we extend it to n-dimensional (N * n board, N * N-1 digital problem ).
First, regardless of the parity of N, the left and right exchange does not change the reverse order number, n is an odd number: the upper and lower exchange reverse order number to increase the N-1 or reduce the N-1 or unchanged, because N is an odd number, therefore, the parity of the reverse order is not changed. When n is an even number, the parity of the upstream and downstream switches is changed once.
Conclusion: WHEN n is an odd number, the initial state is the same as that of the specified reverse order. When n is an even number, the initial state is calculated from the initial state to the specified state, the number of rows to be moved by the vacancy. If the number of reverse orders in the initial state is the same as the number of reverse orders in the specified state, there is a solution.
Now, this question is simple. Just calculate the inverse ordinal number and the number of rows to be moved by spaces.
# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <queue> # include <algorithm> # include <set> # define INF 110000000 # define M 10005 # define n 10005 # define min (, b) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MEM (a, B) memset (a, B, sizeof ()) # define ll long # define mod 1000000007 using namespace STD; int init [24], goal [24]; int A [8] = {, 21 }; int B [16] = {,}; int Gao (int B) {If (B = 0 | B = 2) return 3; if (B = 1 | B = 7) return 6; if (B = 16 | B = 22) return 17; else return 20;} int main () {int t; scanf ("% d", & T ); while (t --) {(INT I = 0; I <24; I ++) scanf ("% d", & init [I]); For (INT I = 0; I <8; I ++) if (init [A [I] = 0) Swap (init [A [I], init [Gao (A [I]); for (INT I = 0; I <24; I ++) scanf ("% d", & goal [I]); For (INT I = 0; I <8; I ++) if (goal [A [I] = 0) Swap (goal [A [I], goal [Gao (A [I]); bool ans = true; For (INT I = 0; I <8 & Ans; I ++) {// cout <init [A [I] <"" <goal [A [I] <Endl; If (init [A [I]! = Goal [A [I]) ans = false;} If (! Ans) {puts ("Y"); continue;} int cnt1 = 0, cnt2 = 0, pos1 = 0, pos2 = 0; For (INT I = 1; I <16; I ++) {If (init [B [I] = 0) {pos1 = I; continue;} For (Int J = 0; j <I; j ++) if (init [B [J]> init [B [I]) cnt1 ++;} For (INT I = 1; I <16; I ++) {If (goal [B [I] = 0) {pos2 = I; continue;} For (Int J = 0; j <I; j ++) if (goal [B [J]> goal [B [I]) cnt2 ++ ;} if (ABS (pos1/4-pos2/4) + cnt1-cnt2) % 2 = 0) puts ("N"); else puts ("Y");} return 0 ;}