跳到正文

题解

【简单搜索】UVA652

RainRecall约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月23日
篇幅
约 2 分钟
题目
UVA652

版本

  1. v1当前

拿到题,发现状态最多 9!=3628809!=362880 种情况。考虑广搜,但是因为多组数据,会超时。所以考虑从初始状态往其它状态 bfs,记录状态对应的答案,记得 bfs 的序列是倒着的,所以要么输出的时候反过来,要么直接加在原有答案前面。

然后有一些小坑:

  • 输出一行就要空一行,结尾不多空行,但是要换行。

  • 无解输出 unsolvable。

AC code

C++
#include<iostream>
#include<map>
#include<queue>
using namespace std;
int T;
map<string,string>m;
queue<string>q;
int main(){
	q.push("12345678x");
	int cnt=0;
	while(!q.empty()){
		string c=q.front();q.pop();
		string qwq=m[c];
		int f=c.find('x');
		if(f!=0&&f!=3&&f!=6){
			swap(c[f-1],c[f]);
			if(m[c]==""){
				m[c]="r"+qwq;
				q.push(c);
			}
			swap(c[f-1],c[f]);
		}
		if(f!=2&&f!=5&&f!=8){
			swap(c[f+1],c[f]);
			if(m[c]==""){
				m[c]="l"+qwq;
				q.push(c);
			}
			swap(c[f+1],c[f]);
		}
		if(f!=0&&f!=1&&f!=2){
			swap(c[f-3],c[f]);
			if(m[c]==""){
				m[c]="d"+qwq;
				q.push(c);
			}
			swap(c[f-3],c[f]);
		}
		if(f!=6&&f!=7&&f!=8){
			swap(c[f+3],c[f]);
			if(m[c]==""){
				m[c]="u"+qwq;
				q.push(c);
			}
			swap(c[f+3],c[f]);
		}
	}
	cin>>T;
	for(int i=1;i<=T;++i){
		string o="         ";
		for(int j=0;j<=8;++j)cin>>o[j];
		if(o!="12345678x"){
			if(m[o]=="")cout<<"unsolvable";
			else cout<<m[o];
		}
		if(i!=T)cout<<"\n";cout<<'\n';
	}
	return 0;
}
/*

*/
原文