Just an Old Puzzle
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 266 Accepted Submission(s): 176
Problem Description
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
Source
2019 Multi-University Training Contest 4
图片转自:https://blog.youkuaiyun.com/birdmanqin/article/details/97953000
AC代码:
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
typedef long long ll;
const int MAXN = 50;
const int INF = 0x3f3f3f3f;
ll t, n;
int arr[MAXN];
int main() {
//ios::sync_with_stdio(false);
scanf("%lld", &t);
int blank;
int castle;
while(t--){
castle = 0;
rep(i, 1, 16){
scanf("%d", &arr[i]);
if(arr[i] == 0)
blank = (i-1)/4+1;
}
rep(i, 1, 16){
if(arr[i] == 0) continue;
rep(j, i+1, 16){
if(arr[j] == 0) continue;
if(arr[i] > arr[j]) castle++;
}
}
printf("%s\n", ((blank%2 == castle%2)?"Yes":"No"));
}
return 0;
}