sgu 139 Help Needed!
来源:互联网 发布:js 递归调用 编辑:程序博客网 时间:2024/04/27 19:58
题目描述:
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 from0 to 15arranged in 4 rows and 4 columns. You are allowed to switchtwo adjacent elements (horizontally or vertically), only if one of themhas the value0. 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 45 6 7 89 10 11 013 14 15 12
Sample Output #1
YES
Sample Input #2
2 1 3 45 6 7 89 10 11 120 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;}
- SGU 139 Help Needed!
- sgu 139 Help Needed!
- SGU 139 Help Needed!
- SGU 139 Help Needed!
- SGU 139 Help Needed! 奇偶性
- SGU 139 Help Needed!(机智)
- [SGU]139. Help Needed!
- sgu139:Help Needed!
- SGU139 Help Needed!
- sgu139: Help Needed!
- Help is needed for Dexter
- Help is needed for Dexter
- Help Is needed for Dexter
- Help is needed for Dexter
- Help is needed for Dexter
- SGU139:Help Needed(拼图)
- UVA 11384 - Help is needed for Dexter
- uva 11384 Help is needed for Dexter
- pkg-config的使用
- 使用Win7 PE安装Win 7原版系统图解
- poj1061
- Project Euler problem 2
- 冬天是个冻手冻脚的流氓
- sgu 139 Help Needed!
- 智力题之鬼谷考徒--分析&程序求解
- VS2005项目中添加lib库以及代码中相对路径
- 虚拟机网络三种连接
- uboot2012中添加已有的uboot命令
- Android Audio AudioStreamOutALSA::write函数
- 数据结构链表的一些操作!
- DllMain中不当操作导致死锁问题的分析--死锁介绍
- VM虚拟机网桥无法连接