hdu 3600 Simple Puzzle 判断N 数码是否有解

Simple Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 742    Accepted Submission(s): 234


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)
 

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)
 

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
 

Author
alpc83
 

Source
2010 ACM-ICPC Multi-University Training Contest(16)——Host by NUDT
 

Recommend
zhengfeng
题意  输入n
给出n*n的矩阵
假如 n=3
是否能变为
1 2 3
4 5 6
8 9 0
如果可以输出YES

分析:

当N为奇数时逆序数之和sum奇偶同性可互达,N为偶数时,逆序数之和sum加上空格所在行距目标空格行的距离dis之和要和终点状态逆序数同奇偶

 

详细请参考:

http://blog.csdn.net/hnust_xiehonghao/article/details/7951173

 

代码:

#include <stdio.h>  
#include <stdlib.h>  
#define MAXN 1000100  
int a[MAXN] = {0};  
int tmp[MAXN]; 
int num;  
void merge(int left, int middle, int right)  
{  
     int i, j, k;  
     i = left, j= middle+1, k = 1;  
     while(i<= middle && j <= right)  
     {  
               if(a[j] < a[i])  
               {  
                    tmp[k++] = a[j++];  
					num+=(middle-i+1);
               }  
               else  
			   {  
                    tmp[k++] = a[i++];  
			   }  
     }  
     while(i <= middle)    
    tmp[k++] = a[i++];  
     while(j <= right)   
    tmp[k++] = a[j++];       
     for(i = left, k = 1; i<= right; i++, k++)  
            a[i] = tmp[k];  
}  
void merge_sort(int left, int right)  
{  
     if(left < right)  
     {  
             int middle = (left + right)/2;  
             merge_sort(left, middle);  
             merge_sort(middle+1, right);  
             merge(left, middle, right);  
     }  
}  
int main()
{  
    int n,i,j,cnt,k,hang;  
	while( scanf("%d",&n)&&n)
	{
	       cnt=0;
    for(i=0;i<n;++i)  
	{
		for(j=0;j<n;j++)
		{
			scanf("%d",&k);
			if(k!=0) a[cnt++]=k;
			else  hang=n-1-i;
		}
	}
	num=0;
    merge_sort(0,cnt-1);  
    if(n%2==1)
	{
		 if(num%2==0) printf("YES\n");
		 else printf("NO\n");
	}
	else
	{
         if((num%2==0&&hang%2==0)||(num%2==1&&hang%2==1))
			 printf("YES\n");
		 else printf("NO\n");
	}
	}
    return 0;  
}   


 

你可能感兴趣的:(Integer,input,360,merge,Training,Numbers)