POJ 3221 Diamond Puzzle(BFS)
来源:互联网 发布:国际阿里云paypal限制 编辑:程序博客网 时间:2024/06/13 00:34
Description
A diamond puzzle is played on a tessellated hexagon like the one shown in Figure 1 below. And in this problem the faces produced by the tessellation are identified as they are numbered in the same figure. If two faces share a side, they are called neighboring faces. Thus, even-numbered faces have three neighboring faces, while odd-numbered faces have only two. At any point during the play of the puzzle, six of the seven faces hold a unique digit ranging from 1 to 6, and the other one is empty. A move in the puzzle is to move a digit from one face to a neighboring empty one.
Starting from any configuration, some series of moves can always make the puzzle look identical to either one shown in Figures 2 and 3. Your task is to calculate the minimum number of moves to make it become the one inFigure 2.
Input
The input contains multiple test cases. The first contains an integer N (0 ≤ N ≤ 5,040), the number of test cases. Then follow N lines, each with a permutation of {0, 1, 2, 3, 4, 5, 6} describing a starting configuration of the puzzle. The ith digit in the permutation is the one in the face numbered i − 1. A zero means the face is empty.
Output
For each test cases, output the minimum number of moves the configuration takes to reach the one shown in Figure 2. If this is impossible, just output “-1
” and nothing else.
Sample Input
3132450624106530123456
Sample Output
10-10
渣渣水平看了题解,果断bfs走起。ps:博客写的太简单了,因为时间比较紧。所以以后时间充足的话还是详细点好。
题意:由当前状态得到最终状态需要最小步数。只允许在空格处移动=-=,从最终状态开始搜
所以就记录0所在位子以及由此0可以移动到的位置。比如:0在位置2,则3,0,1号位置都可以移动到此位置。
另外的对于我来说就是STL里map的使用了,映射关系map<string,int>visit记录有木有被访问过,map<string,int>ans记录到谋一状态所需的最小步数。下面贴挫码。思路来源:点击打开链接
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>#include<queue>#include<map>using namespace std;struct node{ char s[10]; int step;};map<string,int>visit;map<string,int>ans;char s1[10],s2[10]="0123456";int dr[8][5]={{2,4,6,-1},{2,6,-1},{1,3,0,-1},{2,4,-1},{3,5,0,-1},{4,6,-1},{1,5,0,-1}};//记录从0开始的每个位置如果为0时其他可以移动到此位置的点的位置void bfs(){ queue<node>q; node st,ed; strcpy(st.s,s2); st.step=0; visit[s2]=1; q.push(st); while(!q.empty()) { st=q.front(); q.pop(); ans[st.s]=st.step; int temp; for(int i=0;i<7;i++)//找到0的位置 { if(st.s[i]=='0') { temp=i; break; } } for(int i=0;dr[temp][i]!=-1;i++)//对于可以到0的位置依次搜索 { char ss[10]; strcpy(ss,st.s); ss[temp]=st.s[dr[temp][i]];//交换位置 ss[dr[temp][i]]='0'; if(!visit[ss])//没有被访问过 { visit[ss]=1; strcpy(ed.s,ss); ed.step=st.step+1; q.push(ed); } } }}int main(){ bfs(); int n; scanf("%d",&n); while(n--) { getchar(); scanf("%s",s1); if(!strcmp(s1,s2)) printf("%d\n",0); else printf("%d\n",ans[s1]==0?-1:ans[s1]); } return 0;}
- POJ 3221 Diamond Puzzle(BFS)
- poj 3221 Diamond Puzzle (BFS)
- POJ 3221:Diamond Puzzle(六数码 反向bfs)
- poj 3221Diamond Puzzle(bfs+状压)
- poj 3221 Diamond Puzzle 反向bfs
- POJ 3221 Diamond Puzzle.
- POJ3221 Diamond Puzzle(BFS:最短路)
- poj 3131 Cubic Eight-Puzzle (双向bfs+深度控制 立体八数码)
- POJ 3131 Cubic Eight-Puzzle 双向BFS + HASH
- POJ-3131-Cubic Eight-Puzzle(双向BFS+哈希)
- Dearboy's Puzzle (poj 2308 搜索 dfs+bfs)
- AOJ 0121 Seven Puzzle (bfs)
- USACO-Section 4.4 Shuttle Puzzle (BFS)
- AOJ 0121:Seven Puzzle(bfs)
- Multiplication Puzzle (poj 1651)
- Multiplication Puzzle (poj 1651)
- POJ 3678 Kath Puzzle (2-sat)
- POJ 1651 Multiplication Puzzle (区间DP)
- 百度电话面试总结
- CI框架 简介
- Android adb setuid提权漏洞
- Linux学习笔记——虚拟机中安装VMware Tools
- 不仅仅是估不准的问题
- POJ 3221 Diamond Puzzle(BFS)
- 最近研究Android,发现对于外部导入的工程,编译的时候不能够正常生成R.java文件的解决办法
- Complex
- 各种学习视频网址
- Unity3D 4.x 使用Mecanim实现动画控制
- NSArray 类,NSMutableArray 类 ,NSNumber类中的一些方法
- SCRIPT5007:无法获取属性“show”的值,对象为null或未定义
- Java 3个线程依次打印ABC,并且循环10次
- Android性能优化总结