soj1091 指环王 bfs+hash+剪枝
原题链接http://acm.scu.edu.cn/soj/problem.action?id=1091
这题的主要解法就是搜索,我用的是bfs,用map将二维数组处理成字符串作为主键,到达当前状态的最少步数作为键值,就能实现判重,如果当前最少步数已经超过10步,直接退出。但是这样做的时间是1292ms,虽然能够通过,但没能达到最优。
如果能够找到完美的编码函数,就可以不用map,时间应该能够更快,但是25!/12!/12!已经超过了数组的最大容量,找到编码函数也没用了。后来我又想到可以利用剪枝,因为这题限制了10步,设当前的位置不对的棋子数为h(x),当前已经走了y步,如果h(x)+y>10就说明不可能在10步之内完成,直接剪掉。
前面说过,可以把二维数组处理成为字符串作为主键,也可以把二维数组进行二进制压缩,用int作为主键应该会更快。
总之,最大的优化就是剪枝,剪枝后时间为0ms。这题用dfs+剪枝也能过,但没能判重,浪费了不少时间。比赛时,写A*代码应该最为简单。
AC代码:
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=30;
int goal[][5]={
1,1,1,1,1,
0,1,1,1,1,
0,0,2,1,1,
0,0,0,0,1,
0,0,0,0,0
};
const int dx[]={1,1,2,2,-1,-1,-2,-2};
const int dy[]={2,-2,1,-1,2,-2,1,-1};
int v[maxn];
struct node{
int x,y;
node(){
}
node(int x,int y):x(x),y(y){
}
};
void deal(){
v[0]=1;
for(int i=1;i<27;++i) v[i]=2*v[i-1];
}
inline int get1(int (*a)[5]){ //统计位置不对的棋子
int c=0;
for(int i=0;i<5;++i)
for(int j=0;j<5;++j){
if(a[i][j]==2) continue;
else {
if(a[i][j]!=goal[i][j]) ++c;
}
}
return c;
}
int get2(int (*a)[5]){ //二进制的值
int c=0;
for(int i=0;i<5;++i)
for(int j=0;j<5;++j){
c+=a[i][j]*v[i*5+j];
}
return c;
}
int get3(int (*a)[5]){ //空地的位置
for(int i=0;i<5;++i)
for(int j=0;j<5;++j)
if(a[i][j]==2) return i*5+j;
}
void get4(int (*a)[5],int h,int pos){ //解码
h-=2*v[pos];
a[pos/5][pos%5]=2;
for(int i=0;i<25;++i){
if(i==pos) {
h=h>>1;
continue;
}
else {
a[i/5][i%5]=h&1;
h=h>>1;
}
}
}
int bfs(int (*a)[5]){
if(get1(a)>10) return -1;
map<int,node>ha;
queue<int>q;
int f=get2(a);
q.push(f);
ha[f]=node(0,get3(a));
while(!q.empty()){
int h=q.front();
q.pop();
int b[5][5];
node g=ha[h];
get4(b,h,g.y);
int d1=get1(b);
if(d1+g.x>10) continue;
if(d1==0) return g.x;
int x=g.y/5,y=g.y%5;
int old[5][5];
for(int i=0;i<8;++i){
int nx=x+dx[i],ny=y+dy[i];
if(nx<0||ny<0||nx>=5||ny>=5) continue;
memcpy(old,b,sizeof(b));
swap(old[x][y],old[nx][ny]);
int k=get2(old);
if(ha.count(k)) continue;
q.push(k);
ha[k]=node(ha[h].x+1,get3(old));
}
}
return -1;
}
int main(){
deal();
int T;
scanf("%d",&T);
char ch[5][5];
int s[5][5];
while(T--){
for(int i=0;i<5;++i)
scanf("%s",ch[i]);
for(int i=0;i<5;++i)
for(int j=0;j<5;++j){
s[i][j]=ch[i][j]-'0';
}
int ans=bfs(s);
if(ans==-1) printf("Unsolvable in less than 11 move(s).\n");
else printf("Solvable in %d move(s).\n",ans);
}
return 0;
}
如有不当之处欢迎指出!
soj1091 指环王 bfs+hash+剪枝的更多相关文章
- NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝
题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 ...
- 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)
http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...
- [BZOJ1054][HAOI2008]移动玩具 bfs+hash
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2432 Solved: 1355[Submit][Stat ...
- HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)
题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...
- hdu.1067.Gap(bfs+hash)
Gap Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- BFS+Hash(储存,判重) HDOJ 1067 Gap
题目传送门 题意:一个图按照变成指定的图,问最少操作步数 分析:状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的hash技术 #include <bits ...
- POJ3697+BFS+hash存边
/* 疾速优化+hash存边 题意:给定一个包含N(1 ≤ N ≤ 10,000)个顶点的无向完全图,图中的顶点从1到N依次标号.从这个图中去掉M(0 ≤ M ≤ 1,000,000)条边,求最后与顶 ...
- poj 2046 Gap(bfs+hash)
Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...
- poj 2697 A Board Game(bfs+hash)
Description Dao was a simple two-player board game designed by Jeff Pickering and Ben van Buskirk at ...
随机推荐
- xml格式字符串转为Map
import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom ...
- ueditor表格边框没有颜色的解决
问题: 用ueditor画表格,会发现表格存在,但是表格边框没有颜色. 解决方法: 需要对js文件中的样式进行修改,这里我引用的编辑器样式文件是ueditor.all.min.js,所以先找到该文件, ...
- 企业级Docker私有仓库之Harbor部署(http)
部署环境 Centos7.3 x64 docker-ce-17.06.0 docker-compose-1.15.0 Python-2.7.5(系统默认) Docker及Docker-compose安 ...
- linux中的&&和||(linux中=和==效果是一样的)
1. 命令1 && 命令2 命令1执行成功在执行命令2 2. 命令1 || 命令2 命令1执行失败后在执行命令2 我觉得这完全就是判断呀.
- Python算法——二叉树
一.二叉树 from collections import deque class BiTreeNode: def __init__(self, data): self.data = data sel ...
- Android Training Note
版本适配 Tip:为了能在几个Android版本中都能提供最好的特性和功能,你应该在你的app中使用Android Support Library,它能使你的app能在旧平台上使用最近的几个平台的AP ...
- 使用Django实现分页器功能
要使用Django实现分页器,必须从Django中导入Paginator模块 from django.core.paginator import Paginator 假如现在有150条记录要显示,每页 ...
- MySQL笔记-turncat、drop、delete的区别
TRUNCATE 语法: TRUNCATE TABLE [schema.] table [{DROP | REUSE} STORAGE]功能: 删除整个表的数据并释放空间 描述: 由于Trunca ...
- Python基础篇(二)
Python最基本的数据结构是序列(sequence),序列中的每个元素被分以以0开头的唯一的一个id号. Python中有6种内建的序列:列表,元组,字符串,Unicode字符串,buffer对象和 ...
- Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]
CF Round250 E. The Child and Binary Tree 题意:n种权值集合C, 求点权值和为1...m的二叉树的个数, 形态不同的二叉树不同. 也就是说:不带标号,孩子有序 ...