sgu 139 Help Needed!

39 篇文章 0 订阅
1 篇文章 0 订阅

题目描述:

139. Help Needed!

time limit per test: 0.5 sec.
memory limit per test: 4096 KB

Little Johnny likespuzzles a lot! Just a few days ago, he found out about the 'traditional'4x4puzzle. For this puzzle, you have all the numbers from 0 to 15arranged in 4 rows and 4 columns. You are allowed to switchtwo adjacent elements (horizontally or vertically), only if one of themhas the value 0. The purpose of the puzzle is to reach the followingfinal state:

                            1  2  3  4
                            5  6  7  8
                            9 10 11 12
                           13 14 15  0

Given the initial stateof the puzzle, you have to decide whether there exists a sequence of moveswhich brings the puzzle into the final state.

Input

The input will consistof  4 lines, each of them containing 4 integers, describingthe initial state of the puzzle.

Output

For every initialstate, you should print "YES" if the final state can be reachedafter several moves or "NO", if such a thing is impossible.

Sample Input #1

1 2 3 4
5 6 7 8
9 10 11 0
13 14 15 12

Sample Output #1

YES

Sample Input #2

2 1 3 4
5 6 7 8
9 10 11 12
0 13 14 15

Sample Output #2

NO


十五数码问题,手贱百度了一下,然后就找到结论,一套就AC了。

逆序对于0到底的哈密顿距离之和的奇偶性不变是好证的,至于为什么一定有解就难了。

贴个代码(重要的思维啊。。。我等爇菜啊。。。。)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 1010;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;

//    std::ios::sync_with_stdio(false);

int a[17];

int main(){
    int n(0),m(0);
    FOR(i,16){cin>>a[i];if(a[i]==0)m=i;}
    FOR(i,16) for(int j=i+1;j<=16;j++)if(a[j]<a[i])n++;
    m=8-((m-1)/4+1+(m-1)%4+1);//cout<<m<<" "<<n<<endl;
    m+=n;
    if(m&1)printf("YES\n");
    else printf("NO\n");
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值