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

3
1324506
2410653
0123456

Sample Output

10
-1
0

渣渣水平看了题解,果断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)的更多相关文章

  1. POJ 3984 迷宫问题(BFS)

    迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...

  2. POJ 2435Navigating the City(bfs)

    题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][ ...

  3. poj 3669 Meteor Shower(bfs)

    Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...

  4. POJ 3221 Diamond Puzzle.

    ~~~~ 题目链接:http://poj.org/problem? id=3221 显然是BFS找最优解.但是终止条件不好写.看到有一仅仅队交上去一直TLE. 比赛完了看题解原来是以目标状态为起点,B ...

  5. 【POJ - 3414】Pots(bfs)

    Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...

  6. POJ 1573 Robot Motion(BFS)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12856   Accepted: 6240 Des ...

  7. POJ 3126 Prime Path (BFS)

    [题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...

  8. poj 2251 Dungeon Master(bfs)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  9. POJ 3678 Katu Puzzle (2-SAT)

                                                                         Katu Puzzle Time Limit: 1000MS ...

随机推荐

  1. 关于oracle的sqlplus的另一些小技巧

    执行脚本的命令在上一节已经讲过,不再重复. sqlplus user/password@ip:port/servicename @/path/sqltest.sql; sqltest的内容及注释: - ...

  2. 最快的序列化组件protobuf的.net版本protobuf.net

    Protobuf是google开源的一个项目,用户数据序列化反序列化,google声称google的数据通信都是用该序列化方法.它比xml格式要少的多,甚至比二进制数据格式也小的多.     Prot ...

  3. python 根据文件创建时间排序

    #coding:utf8 import os,time directory = "d:/scrapy tutorial/" t = [] d = {} for filename i ...

  4. 洛谷 P1824 进击的奶牛【二分答案/类似青蛙过河】

    题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi<=1,000,000 ...

  5. 训练指南 UVA - 11354(最小生成树 + 倍增LCA)

    layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...

  6. hdu6166

    hdu6166 题意 给出一个有向图,选择 \(k\) 个点,问这 \(k\) 个点任意两点距离的最小值. 分析 按结点编号的二进制位,每次可以把所有点分到两个集合,那么求两个集合的点间的最短路即可( ...

  7. 洛谷——P1226 取余运算||快速幂

    P1226 取余运算||快速幂 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod ...

  8. SPOJ IITWPC4F - Gopu and the Grid Problem (双线段树区间修改 区间查询)

    Gopu and the Grid Problem Gopu is interested in the integer co-ordinates of the X-Y plane (0<=x,y ...

  9. notepad++ 开始和结尾

    作者:zecy链接:https://www.zhihu.com/question/37708379/answer/73181634来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  10. Dumpzilla工具第615行bug的解决办法

    Dumpzilla工具第615行bug的解决办法   在Dumpzilla使用选项frequency时,会提示SQL语法错误.这是由于其中SQL语句编写错误.需要将615行中: where url l ...