原题链接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+剪枝的更多相关文章

  1. NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝

    题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 ...

  2. 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...

  3. [BZOJ1054][HAOI2008]移动玩具 bfs+hash

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2432  Solved: 1355[Submit][Stat ...

  4. HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)

    题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...

  5. hdu.1067.Gap(bfs+hash)

    Gap Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  6. BFS+Hash(储存,判重) HDOJ 1067 Gap

    题目传送门 题意:一个图按照变成指定的图,问最少操作步数 分析:状态转移简单,主要是在图的存储以及判重问题,原来队列里装二维数组内存也可以,判重用神奇的hash技术 #include <bits ...

  7. POJ3697+BFS+hash存边

    /* 疾速优化+hash存边 题意:给定一个包含N(1 ≤ N ≤ 10,000)个顶点的无向完全图,图中的顶点从1到N依次标号.从这个图中去掉M(0 ≤ M ≤ 1,000,000)条边,求最后与顶 ...

  8. poj 2046 Gap(bfs+hash)

    Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...

  9. 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 ...

随机推荐

  1. web、pc、wap、app的区别

    通常情况下web=pc,wap=app,前者指电脑用的程序,后者指手机用的程序. 更深层的区别是,pc电脑上软件,web电脑上的网页,wap手机上的网页,app手机用软件

  2. Oracle多行记录合并的几种方法

    今天正好遇到需要做这个功能,顺手搜了一下网络,把几种方法都列出来,方便以后参考. 1 什么是合并多行字符串(连接字符串)呢,例如: SQL> desc test; Name Type Nulla ...

  3. Maven初步

    Maven初入 maven 是一个项目管理工具, 它包含了一个 项目对象模型(Project Object Model POM), 一组标准集合, 一个项目生命周期(Project Lifecycle ...

  4. TensorFlow-相关 API(学习笔记 )

    1.tf.nn.conv2d conv2d( input, filter, strides, padding, use_cudnn_on_gpu=True, data_format='NHWC', n ...

  5. pwd 的“P”选项

    1.目录是链接目录时,pwd -P  显示出实际路径,而非使用连接(link)路径:pwd显示的是连接路径 例: [root@localhost soft]# cd /etc/init.d [root ...

  6. [Android] Toast问题深度剖析(一)

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 题记 Toast 作为 Android 系统中最常用的类之一,由于其方便的api设计和简洁的交互体验,被我们所广泛采用.但是,伴随着我们开发的深 ...

  7. c#实现自然排序效果,按1,2,11而不是1,11,12,区分字母文字和数字

    排序有时候要考虑后缀.这样看起来比较自然. 参考了codeproject上一篇文章:http://www.codeproject.com/Articles/22978/Implementing-the ...

  8. 51NOD 1227 平均最小公倍数 [杜教筛]

    1227 平均最小公倍数 题意:求\(\frac{1}{n} \sum_{i=1}^n lcm(n,i)\) 和的弱化版? \[ ans = \frac{1}{2}((\sum_{i=1}^n \su ...

  9. POJ 2007 Scrambled Polygon [凸包 极角排序]

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8636   Accepted: 4105 ...

  10. BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 2917  Solved: 918[Submit][St ...