hdu 3600 Simple Puzzle 八数码问题有解的条件及其推广

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)
 

 

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
#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
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值