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 ...
随机推荐
- What is JSON
JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON具有以下这些形式: 对象是一个无序的“‘名称/值’对” ...
- 【转载】Caffe学习:运行caffe自带的两个简单例子
原文:http://www.cnblogs.com/denny402/p/5075490.html 为了程序的简洁,在caffe中是不带练习数据的,因此需要自己去下载.但在caffe根目录下的data ...
- 17Oracle Database 维护
Oracle Database 维护 备份 还原
- Django 缓存之配置Redis
一.cache介绍 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存. 缓存工作原理:缓存是将一些常用的数据保存内存或 ...
- 利用WebUploader进行图片批量上传,在页面显示后选择多张图片压缩至指定路径【java】
WebUploader是由Baidu WebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件.在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流IE浏览 ...
- js之标签操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- textbook references
* math 1. Teubner-Taschenbuch der Mathematik * CFD
- Code(poj 17801)
求出一个长度为10^n+n-1的序列,其中包含了所有的n位数(一共10^n个数,从00000(n个0)~10^n-1) /* 典型的欧拉回路题目 对于n=4为密码想要序列最短 那么 1234 234? ...
- windows server 2008R2 上安装配置freesshd
从FREESSHD官方网站下载最新的软件版本,下载地址是http://www.freesshd.com/?ctt=download 双击刚刚下载的freeSSHd.exe进行安装,安装时其他都是默认安 ...
- 洛谷—— P1657 选书
https://www.luogu.org/problem/show?pid=1657 题目描述 学校放寒假时,信息学奥赛辅导老师有1,2,3……x本书,要分给参加培训的x个人,每人只能选一本书,但是 ...