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 ...
随机推荐
- awk解决实际问题例子
awk很有用,就不说了,下面记录几个我遇到的例子,汇总 1 获得linux环境变量(ENVIRON) awk 'BEGIN{print ENVIRON["PATH"];}' /et ...
- finally中关闭资源
对finally中关闭资源是否还要使用try...catch老是感到迷惑,现在存个例子,省的忘了 public StringBuilder readTxtFile(File file){ String ...
- git 不成功
fatal: Interactive git shell is not enabled.hint: ~/git-shell-commands should exist and have read an ...
- Eralng的常用数据结构
1.记录(record) 适用于小数据,并且用属性名方便查找 2.Key/Value 类型 a.属性列表 就是类似[{Key, Value}]的列表,可以通过proplists模块来处理这样的列表 当 ...
- Html5五子棋
1.效果图 2.代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- crontab 定时任务守护程序,停止服务器时出现 job for * canceled
(1)首先要在程序启动的时候加入定时任务到crontab #! /bin/shmkdir -p /home/apps/components/ams/ 2>/dev/nullcp ./amswat ...
- c#代码启动任务管理器的几种方法
1.直接启动 ProcessStartInfo info = new ProcessStartInfo(); info.FileName = Path.Combine(Environment.GetE ...
- iOS-属性字符串添加下划线、删除线
常用到的属性字符串 ///定义属性字符串NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithStrin ...
- AndroidStudio3更改包名失败
使用Android Studio 3.0 Beta6更改包名refactor---rename一直提示:Refactoring cannot be performedFile xxx\build\xx ...
- verilog实验1:基于FPGA蜂鸣器演奏乐曲并数码管显示
一.实验任务 利用FPGA进行代码开发,使蜂鸣器演奏出乐曲<生日快乐>,将音调显示在数码管.原理为蜂鸣器为交流源蜂鸣器,在引脚上加一定频率的方波就可以发声,而且发声的频率由所加方波决定.这 ...