hdu 2514 Another Eight Puzzle
来源:互联网 发布:情侣装 知乎 编辑:程序博客网 时间:2024/05/05 18:39
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2514
就是DFS+回溯。。。我蠢蠢的把所有状态枚举了。。
但是在输出处理上没注意。。。WA了几次
下面是AC代码:
#include<iostream>#include<cstring>using namespace std;int a[10],b[10];int mark[10];int flag;int judge(int num,int n){if(n==1&&num==2)return 1;if(n==8&&num==7)return 1;if((n>1&&n<8)&&(num==(n-1)||num==(n+1)))return 1;return 0;}int cheak(int pos ,int n){int i;if(pos==1){for(i=2;i<=4;i++)if(judge(a[i],n))return 0;return 1;}if(pos==2){if(judge(a[1],n)) return 0;if(judge(a[3],n)) return 0;if(judge(a[5],n)) return 0;if(judge(a[6],n)) return 0;return 1;}if(pos==3){if(judge(a[1],n)) return 0;if(judge(a[2],n)) return 0;for(i=4;i<=7;i++)if(judge(a[i],n))return 0;return 1;}if(pos==4){if(judge(a[1],n)) return 0;if(judge(a[3],n)) return 0;for(i=6;i<=7;i++)if(judge(a[i],n))return 0;return 1;}if(pos==5){if(judge(a[2],n)) return 0;if(judge(a[3],n)) return 0;if(judge(a[6],n)) return 0;if(judge(a[8],n)) return 0;return 1;}if(pos==6){for(i=2;i<=5;i++)if(judge(a[i],n))return 0;return 1;for(i=7;i<=8;i++)if(judge(a[i],n))return 0;return 1;}if(pos==7){if(judge(a[3],n)) return 0;if(judge(a[4],n)) return 0;if(judge(a[6],n)) return 0;if(judge(a[8],n))return 0;return 1;}if(pos==8){for(i=5;i<=6;i++)if(judge(a[i],n))return 0;return 1;}}void DFS(int pos){int i;if(pos>8){flag++;for(i=1;i<=8;i++)b[i]=a[i];return ;}if(a[pos]==0){for( i=1;i<=8;i++){if(!mark[i]&&cheak(pos,i)){mark[i]=1;a[pos]=i;DFS(pos+1);mark[i]=0;a[pos]=0;}}}elseDFS(pos+1);}int main(){int t,i,j;cin>>t;for(j=1;j<=t;j++){memset(mark,0,sizeof(mark));for(i=1;i<=8;i++){cin>>a[i];mark[a[i]]=1;}flag=0;DFS(1);if(!flag){printf("Case %d: ",j);printf("No answer\n");}else if(flag>1){printf("Case %d: ",j);printf("Not unique\n");}else{printf("Case %d: ",j);printf("%d",b[1]);for(i=2;i<=8;i++)printf(" %d",b[i]);printf("\n");}}return 0;}
- HDU 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle
- HDU 2514 Another Eight Puzzle
- hdu(2514) Another Eight Puzzle
- HDU 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle 枚举
- hdu 2514—— Another Eight Puzzle
- HDU 2514--Another Eight Puzzle【DFS】
- hdu 2514 Another Eight Puzzle(DFS+回溯)
- hdu 2514 Another Eight Puzzle 图填数字 next_permutation+暴力
- hdu 2514 Another Eight Puzzle(DFS暴搜)
- hdu 2541 Another Eight Puzzle(dfs)
- hdoj 2514 Another Eight Puzzle(DFS)
- hdu2514 Another Eight Puzzle
- Another Eight Puzzle
- Another Eight Puzzle
- S3C6410芯片揭密之一 S3C6410体系结构
- lower_bound 用法
- qemu启动虚拟机
- Quartz在Spring中设置动态定时任务
- vc resource 文件在另一个编辑器中打开
- hdu 2514 Another Eight Puzzle
- Fedora Gnome3 系统设置
- c++内存分布,代码段、全局数据区、栈、堆
- 网络最热的ARM|Linux|wince|ucos|经典资料完整版
- Nginx配置文件详细说明
- Win7下锁定屏幕刷新频率
- 谷歌对搜索算法进行重大修改
- hdu 1716
- Rookit技术基础(3)