HDOJ 1195 Open the Lock
来源:互联网 发布:那年呐服饰淘宝代理可信吗 编辑:程序博客网 时间:2024/04/30 12:17
给你两个四位数,起始和目标,每次变化一个数字,+1,-1,左右相邻交换,求出达到目标数额最小步数;
标记数组可以有两个num[10000],num[10][10][10][10][10],都是标记到达的四位数,先用普通BFS写了一次 ,HDOJ 15MS, swustOJ 超时,学院OJ的数据果然厉害;
贴出普通BFS的代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;struct node{ int num[4]; int step;};int use[10][10][10][10];int tem[4];int a,b;queue<node> que;int BFS(){ while(!que.empty()) { node now=que.front(); que.pop(); /* for(int i=0;i<4;i++) { printf("%d ",now.num[i]); } printf(" %d\n",now.step);*/ if(now.num[0]==tem[0]&&now.num[1]==tem[1]&&now.num[2]==tem[2]&&now.num[3]==tem[3]) return now.step; node next; for(int i=0;i<4;i++) { next=now; next.num[i]++; if(next.num[i]==10) next.num[i]=1; if(!use[next.num[0]][next.num[1]][next.num[2]][next.num[3]]) { use[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1; next.step++; que.push(next); } } for(int i=0;i<4;i++) { next=now; next.num[i]--; if(next.num[i]==0) next.num[i]=9; if(!use[next.num[0]][next.num[1]][next.num[2]][next.num[3]]) { use[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1; next.step++; que.push(next); } } for(int i=0;i<3;i++) { next=now; int temp=next.num[i]; //交换 next.num[i]=next.num[i+1]; next.num[i+1] =temp; if(!use[next.num[0]][next.num[1]][next.num[2]][next.num[3]]) { use[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1; next.step++; que.push(next); } } }}int main(){ int T; scanf("%d",&T); while(T--) { while(!que.empty()) que.pop(); memset(use,0,sizeof(use)); scanf("%d%d",&a,&b); node ans; for(int i=3;i>=0;i--) { ans.num[i]=a%10; a=a/10; } for(int i=3;i>=0;i--) { tem[i]=b%10; b=b/10; } ans.step=0; que.push(ans); printf("%d\n",BFS()); }return 0;}
0 0
- HDOJ 1195 Open the Lock
- HDOJ 1195 Open the Lock (BFS)
- 【更新】HDOJ 1195 Open the Lock (双向BFS)
- HDOJ 题目1195 Open the Lock(双向BFS)
- HDOJ 1195 Open the Lock bfs 双向bfs
- HDU-1195-Open the Lock
- HDU 1195 Open the Lock
- HDU 1195 OPEN THE LOCK
- HDU-Open the Lock-1195
- POJ 1195 Open the Lock
- hdu 1195 Open the Lock
- hdu-1195-Open the Lock
- HDU 1195 Open the Lock
- hdu 1195 Open the Lock
- HDU 1195 Open the Lock
- hdu(1195) Open the Lock
- HDU 1195 Open the Lock
- hdu 1195 Open the Lock
- A + B Problem
- 【OpenCV】OpenCV3的第五天——imgproc组件之图像处理
- 4——Service Discovery in a Microservices Architecture
- Linux 内核延时函数
- 【学习笔记】3D图形核心基础精炼版-9:stage3D实战-图片纹理
- HDOJ 1195 Open the Lock
- iptables详解
- linux服务器自动切割并清理日志
- HTTP 常见状态码详解
- (转)Android之Notification的多种用法
- CMake 7 步教程
- 5——Event-Driven Data Management for Microservices
- 【USACO题库】3.1.3 Humble Numbers丑数
- nyoj 115 城市平乱