Simple Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 207 Accepted Submission(s): 62
Problem Description
Alpc83 is proud of his IQ, so he usually try to challenge the puzzle which is famous but more difficult than what we have known. Some days ago, he played the eight digits puzzle, and he thought it’s so easy. Then he want to challenge a N*N-1 digits puzzle whose rules are same with the eight digits puzzle. Firstly he have all the numbers from 0 to N*N-1 arranged in N rows and N columns randomly. Because he don’t want to waste time, he hope to make sure whether he can finally solve the puzzle if he is clever enough. Now he ask you for help.
(Ps. Don’t you know the eight digits puzzle? Oh, my god! Let me tell you: you have all numbers from 0 to 8 arranged in 3 rows and 3 columns. You are allowed to switch two adjacent elements (horizontally or vertically), only if one of them has the value 0. You have to decide whether there exists a sequence of moves which brings the puzzle in the initial state into the final state.)
In this puzzle , we will give you a initial state, and we set the final state is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
(n=4 for example)
(Ps. Don’t you know the eight digits puzzle? Oh, my god! Let me tell you: you have all numbers from 0 to 8 arranged in 3 rows and 3 columns. You are allowed to switch two adjacent elements (horizontally or vertically), only if one of them has the value 0. You have to decide whether there exists a sequence of moves which brings the puzzle in the initial state into the final state.)
In this puzzle , we will give you a initial state, and we set the final state is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
(n=4 for example)
Input
Each case starts with a line contains the integer N.
The following N lines describe the initial state, each of them containing n integers, describing the initial state of the puzzle.
A line with N = 0 indicates the end of the input; do not write any output for this case. (N<=300)
The following N lines describe the initial state, each of them containing n integers, describing the initial state of the puzzle.
A line with N = 0 indicates the end of the input; do not write any output for this case. (N<=300)
Output
For each test case, print "YES" if the final state can be reached after several moves or "NO", if such a thing is impossible.
Sample Input
2 2 1 3 0 0
Sample Output
NO#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=201000;
int a[maxn+1],t[maxn+1];
long long cnt;
void merge_sort(int x,int y)
{
if(y-x>1)
{
int m=x+(y-x)/2;
int p=x,q=m,i=x;
merge_sort(x,m);
merge_sort(m,y);
while(p<m||q<y)
{
if(q>=y||(p<m&&a[p]<=a[q])) t[i++]=a[p++];
else {t[i++]=a[q++];cnt+=m-p;}
}
for(i=x;i<y;i++) a[i]=t[i];
}}
int main()
{
int n;
while(scanf("%d",&n)==1&&n)
{
int pl=0;
int x,y;
for(int i=0;i<n*n;i++)
{
scanf("%d",&a[pl]);
if(a[pl]==0){x=i/n,y=i%n;continue;}
pl++;
}
//for(int i=0;i<pl;i++) cout<<a[i]<<"...";cout<<endl;
cnt=0;
merge_sort(0,pl);
//for(int i=0;i<pl;i++) cout<<a[i]<<"...";cout<<endl;
if(n&1)
{
if(cnt%2==0) printf("YES/n");
else printf("NO/n");
}
else
{
int step=n-1-x;
if(cnt%2==0&&step%2==0) printf("YES/n");
else if(cnt%2==1&&step%2==1) printf("YES/n");
else printf("NO/n");
}}
return 0;
}
/*
4
1 2 3 0
4 5 6 7
8 9 10 11
12 13 14 15
*/