POJ 3221 Diamond Puzzle.
~~~~
id=3221
显然是BFS找最优解。但是终止条件不好写。看到有一仅仅队交上去一直TLE。
比赛完了看题解原来是以目标状态为起点,BFS给每一个状态打表。用一个map映射存起来。
~~~~
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<map>
using namespace std; char str[10];
char s[10]="0123456";
map<string,int> v; //状态映射
map<string,int>ans; //答案映射
struct node
{
int t;
char s[10];
};
//分别在0~6位置能够到达的目标位置,以-1结束。 int dir[7][4]={
{2,4,6,-1},{2,6,-1},{1,0,3,-1},{2,4,-1},
{3,0,5,-1},{4,6,-1},{1,0,5,-1} };
int Find(char* s)
{
for(int i=0;i<7;i++)
if(s[i]=='0') return i;
}
int bfs()
{
queue<node> q;
node cur,next;
strcpy(cur.s,s);cur.t=0;
q.push(cur);
v[cur.s]=1;
ans[cur.s]=0;
while(!q.empty())
{
cur=q.front(); q.pop();
int pos=Find(cur.s); //'0'的位置。 for(int i=0;dir[pos][i]!=-1;i++)
{
char temp[10];
strcpy(temp,cur.s);
//交换‘0’和和对应能去的位置
swap(temp[pos],temp[dir[pos][i]]);
if(!v[temp])
{
v[temp]=1;
strcpy(next.s,temp);
next.t=cur.t+1;
ans[next.s]=next.t;
q.push(next);
}
}
}
}
int main()
{
v.clear();
ans.clear();
bfs();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
if(strcmp(str,s)==0) puts("0");
else printf("%d\n",ans[str]==0?-1:ans[str]);
}
return 0;
}
POJ 3221 Diamond Puzzle.的更多相关文章
- POJ 3221 Diamond Puzzle(BFS)
Description A diamond puzzle is played on a tessellated hexagon like the one shown in Figure 1 below ...
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- poj 3678 Katu Puzzle(2-sat)
Description Katu Puzzle ≤ c ≤ ). One Katu ≤ Xi ≤ ) such that for each edge e(a, b) labeled by op and ...
- POJ 3678 Katu Puzzle (经典2-Sat)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 2401 Descr ...
- POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)
传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K T ...
- POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- poj 1651 Multiplication Puzzle
题目链接:http://poj.org/problem?id=1651 思路:除了头尾两个数不能取之外,要求把所有的数取完,每取一个数都要花费这个数与相邻两个数乘积的代价,需要这个代价是最小的 用dp ...
- poj 3678 Katu Puzzle(Two Sat)
题目链接:http://poj.org/problem?id=3678 代码: #include<cstdio> #include<cstring> #include<i ...
- POJ 3678 Katu Puzzle(2 - SAT) - from lanshui_Yang
Description Katu Puzzle is presented as a directed graph G(V, E) with each edge e(a, b) labeled by a ...
随机推荐
- LockDemo 锁对象
class Resource { private boolean flag = false; private String name; private int count; //资源锁 Lock lo ...
- HDU_1022_Train Problem I
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Java线程的sleep方法
sleep方法的签名: public static void sleep (long millis) sleep方法是Thread类的一个方法,作用是:在指定的毫秒内让正在执行的线程休眠(暂停执行) ...
- 安卓app测试之内存监控
一.通过Dumpsys 来取值 1.adb shell dumpsys meminfo 获取的所有进程的内存信息,以及总内存,剩余内存,使用的内存等信息. 2.想获得某一进程内存的详细信息,在后面加上 ...
- RabbitMQ系列(一)--消息中间件MQ如何去选择
MQ在项目中的应用很普遍,本人所在项目组使用的是ActiveMQ,但是后面介绍的RabbitMQ... 一.应用场景 1.异步处理 2.流量削峰.秒杀 3.日志处理,推荐kafka 4.应用解耦 二. ...
- 调用微信扫一扫功能,踩坑'invalid signature'
在vue项目中,调用微信扫一扫功能,在安卓系统下完全正常,ios系统下却报错'invalid signature'的错误,这可能令许多小伙伴困惑,经过查询大量博客相关资料,才找到了解决的方法. 原因: ...
- 了解固态硬盘SSD,竟然如此简单!小白也能懂!
https://www.youtube.com/watch?v=alb6-zp52mA
- java学习日志--char和int的相互转换
package shugen; /*ASCLL码表 * 48 数字0 * 49 1 * 50 2 * 51 3 * 52 4 * 53 5 * 54 6 * 55 7 * 56 8 * 57 9 */ ...
- 【IDEA】【Git】pull代码始终无法pull到最新的代码或者提示pull no items 【解决方式】
最近pull代码老是提示pull no items,但是本地并不是最新的代码,看了各种博客始终无法解决,最后靠自己的方式解决.下面是解决方法. 方法:1.首先git --> repository ...
- Linux:iscsi存储服务器配置
服务器添加4块硬盘 mdadm -Cv /dev/md0 -n 3 -l 5 -x 1 /dev/sdb /dev/sdc /dev/sdd /dev/sde 记下UUID值 mdadm -D /de ...