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. gcc 学习

    gcc -lrt 参数说明:  说明在连接生成可执行文件的时候,将连接库librt.so or librt.a -l 参数说明 连接库 编译 使用 clock_gettime函数的,gcc需要添加上 ...

  2. hdu 5191(思路题)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. 在阿里云“专有网络”网络类型中配置vsftpd

    原文地址:传送门 环境:云服务器ECS,网络类型为“专有网络”,创建ECS绑定公网IP:系统镜像为Debian 8  现象:FTP客户端可以连接FTP服务端,但“读取目录列表失败”.       原因 ...

  4. 让IE6/IE7/IE8支持CSS3属性的8种方法介绍

    我们都知道,IE浏览器暂不支持CSS3的一些属性.国外的工程师们,不安于此现状,他们总是尽量使用一些手段使IE浏览器也能支持CSS3属性,我觉得这些都是很有意义,很有价值的工作,可以推动整个技术领域的 ...

  5. NOIP2014飞扬的小鸟

    长为n,高为m的二维平面,其中有k个管道(忽略管道的宽度)小鸟始终在游戏界面内移动.从最左边任意高度位置出发,到达游戏界面最右边,游戏完成每个单位时间沿横坐标方向右移距离为1,竖直移动的距离由玩家控制 ...

  6. Dfs【p4906】小奔关闹钟

    Background 由于今天是星期一,闹钟准时响了,由于小奔太困了,所以她想关停闹钟. Description 可是,他的闹钟电路太复杂了,有很多个开关,每个开关都连着其他开关,其他开关又连着更多的 ...

  7. 一个Sqrt函数引发的血案

    源码下载地址:http://diducoder.com/sotry-about-sqrt.html 好吧,我承认我标题党了,不过既然你来了,就认真看下去吧,保证你有收获. 我们平时经常会有一些数据运算 ...

  8. [JSOI2007]重要的城市(x)

    开始(脑残ing)诶? 暴力能过 噼里啪啦码码码 TLE TLE 啥?看错复杂度?带个25的常数 ?*……!%@……*%#…!@#!@#……*!@#& Floyd,并记录两点间的一个重要的城市 ...

  9. Android 获取 json

    Android  获取 json MainActivity.java package com.example.jsontest; import java.io.IOException; import ...

  10. P2P通信标准协议(四)之SIP

    在前面几篇文章中我们介绍了建立p2p通信的一般协议(簇),以及一种完整的NAT传输解决方案ICE, 但是对于多用户的通信情况,还有一些通用协议来实现标准化的管理,如之前讲过的SDP和SIP等,SIP( ...