poj2965
来源:互联网 发布:ug编程模具 编辑:程序博客网 时间:2024/05/16 15:03
剪枝十分重要 visi数组 记录 已经在树中的值 避免重复
#include <iostream>#include <cstdio>#include <climits>#include <cstring>#include <cstdlib>#define maxn 65536using namespace std;char a[5];int b[5][5];int aa=0;int v[maxn],x[maxn],y[maxn],fa[maxn],px[100000],py[100000];bool visi[maxn];int bfs(){ memset(visi,0,sizeof(visi)); for(int i=1;i<=4;++i) for(int j=1;j<=4;++j) { b[i][j]=(1<<((i-1)*4))+(1<<((i-1)*4+1))+(1<<((i-1)*4+2))+(1<<((i-1)*4+3)); for(int m=1;m<=4;++m) if(m!=i) b[i][j]+=(1<<((m-1)*4+j-1)); } int head=0,tail=0,temp,f,xx,yy,t; v[head]=aa;x[head]=0;y[head]=0;fa[head]=0; visi[v[head]]=true; while(true) { if(v[head]==0) return head; temp=v[head];xx=x[head];yy=y[head];f=head;++head; for(int i=1;i<=4;++i) for(int j=1;j<=4;++j) { t=temp^b[i][j]; if((i!=xx||j!=yy)&&!visi[t]) { v[++tail]=t; x[tail]=i;y[tail]=j;fa[tail]=f; visi[t]=true; if(v[tail]==0) return tail; } } }}int main(){ for(int i=1;i<=4;++i) { scanf("%s",a); for(int j=0;j<4;++j) if(a[j]=='+') aa+=1<<((i-1)*4+j); } int t=bfs(); int ans=0; while(t!=0) { px[ans]=x[t]; py[ans]=y[t]; ++ans; t=fa[t]; } printf("%d\n",ans); for(int i=ans-1;i>=0;--i) { printf("%d %d\n",px[i],py[i]); } return 0;}
0 0
- POJ2965
- poj2965
- poj2965
- POJ2965
- poj2965
- poj2965
- poj2965
- POJ2965
- POJ2965
- poj2965
- poj2965
- POJ2965
- POJ2965
- POJ2965
- poj2965
- POJ2965
- poj2965
- poj2965
- Jtemplate解析Json小引
- 加载图标的使用
- 浅学正则表达式
- Leaks检查内存泄漏,内存清理、监控的Android开发工具
- CSS完美hack方案
- poj2965
- CSS 中 # 和 . 的区别
- OpenCV学习笔记(八) 边缘、线与圆的检测
- 仿微信公众号发送到桌面快捷方式
- 【宝宝总是饿1】C#如何与数据库相连?
- sql server2008 R2 各个版本的区别与选择
- LeetCode 231,326,342 --Power of Two & Three & Four
- CyanogenMod 11.0模拟器goldfish的编译方法及下载地址
- spring-boot整合Mock进行单元测试