You are given a 4 × 4 grid, which consists of 15 number cells and an empty cell.
All numbers are unique and ranged from 1 to 15.
In this board, the cells which are adjacent with the empty cell can move to the empty cell.
Your task is to make the input grid to the target grid shown in the figure below.
In the following example (sample input), you can get the target grid in two moves.
Input
The first line contains an integer T (1 <= T <= 10^5) denoting the number of test cases.
Each test case consists of four lines each containing four space-separated integers, denoting the input grid. 0 indicates the empty cell.
Output
For each test case, you have to print the answer in one line.
If you can’t get the target grid within 120 moves, then print ‘No’, else print ‘Yes’.
Sample Input
2
1 2 3 4
5 6 7 8
9 10 0 12
13 14 11 15
1 2 3 4
5 6 7 8
9 10 11 12
13 15 14 0
Sample Output
Yes
No
数字华容道板题。。。我真的不知到为啥。。。背结论吧
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int ans=0;
int a[17];
for(int i=1; i<=16; i++)
{
scanf("%d",&a[i]);
if(a[i]==0)
{
a[i]=16;
ans=ans+(i%4)+(i/4);
if(i%4!=0)
ans++;
}
}
for(int i=1;i<=16;i++)
{
if(a[i]!=i)
{
ans++;
for(int j=1; j<=16; j++)
{
if(a[j]==i)
{
swap(a[i],a[j]);
break;
}
}
}
}
if(ans%2==0)
printf("Yes\n");
else
printf("No\n");
}
}