Eight-puzzle, which is also called "Nine grids", comes from an old game.
In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.
We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.
A state of the board can be represented by a string S using the rule showed below.
The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.
Input
The first line is T (T <= 200), which means the number of test cases of this problem.
The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.
Output
For each test case two lines are expected.
The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.
Sample Input
2
12X453786
12345678X
564178X23
7568X4123
Sample Output
Case 1: 2
dd
Case 2: 8
urrulldr
题意:
给你两个八数码,让你输出从第一个八数码到第二个八数码的最短路径,且要求该路径也是最短路径下的字典序最小的路径。
思路:
一开始我是用类似A*的限制条件和优先队列做这道题,后来一直WA一直WA,就换成IDA*算法
经过多次修改后的依旧WA的代码,原因应该是错在优先队列优先级的问题上,我加的限制条件不符合答案
WA的例子有:
2
X87654321
12345678X
2185X6734
12345678X
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define rep1(i,s,e) for(int i=s;i<e;i++)
#define rep2(i,s,e,c) for(int i=s;i>=e;i--)
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define pfn() printf("\n")
#define pfs() printf(" ")
#define sfi(x) scanf("%d",&x)
#define sfi1(x,y) scanf("%d%d",&x,&y)
#define sff(x) scanf("%lf",&x)
#define sfl(x) scanf("%lld",&x)
#define memset1(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
const int MAX = 37e4 + 50;
const int mod = 996873654;
int f[10];//f[i]表示i!,阶乘
int xy[][2]={{1,0},{0,-1},{0,1},{-1,0}};//d,l,r,u
char chars[5]={'d','l','r','u'};
//struct Path{
// char ch;
// int pre_cantor;
//}path[MAX];
int vis[MAX];
vector< pair<int,int> > va(10);
struct node{
int x,y;
int a[3][3];
int cantor;
int g;//以目前的步数作为g
int h;//计算1~8的数字到end需要的步数作为评估值,必定小于实际操作数
int len;
char process[121];
void init(int _x,int _y){
x=_x; y=_y;
}
void get_h(){
h=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(a[i][j])
h+=abs(i-va[a[i][j]].first)+abs(j-va[a[i][j]].second);
}
}
}
bool operator < (const node& t) const{
if(h+g!=t.g+t.h) return h+g>t.g+t.h;
else{
int len1=strlen(process),len2=strlen(t.process);
if(len1!=len2) return len1>len2;
else{
if(memcmp(process,t.process,len1)>0) return true;
return false;
}
}
}
}p,p1;
int cantor(int a[][3]){
vector<int> v;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
v.push_back(a[i][j]);
int number=0;
for(int i=0;i<9;i++){
int num=0;
for(int j=i+1;j<9;j++){
if(v[j]<v[i]){
num++;
}
}
number+=num*f[9-i-1];
}
return number;
}
void init(string str,node &p,int choice){
int k=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(str[k]=='X') str[k]='0';
p.a[i][j]=str[k++]-'0';
if(p.a[i][j]==0){
p.init(i,j);
}
}
}
if(choice){
rep1(i,0,9){
if(str[i]!='0'){
va[str[i]-'0']=make_pair(i/3,i%3);
}
}
}
}
int main(){
f[0]=f[1]=1;
for(int i=2;i<=8;i++){
f[i]=f[i-1]*i;
}
int t,k=0;
sfi(t);
while(t--){
string src,des;
cin>>src>>des;
memset1(vis);
priority_queue<node> q;
init(des,p,1);
int end=cantor(p.a);
init(src,p,0);
int begin=cantor(p.a);
p.cantor=begin;
p.g=1; p.get_h(); p.len=-1;
vis[p.cantor]=1;
q.push(p);
while(q.size()){
p=q.top();
q.pop();
if(p.cantor==end) break;
for(int i=0;i<4;i++){
int tex=p.x+xy[i][0],tey=p.y+xy[i][1];
if(tex>=0&&tex<3&&tey>=0&&tey<3){
p1=p;
p1.init(tex,tey);
swap(p1.a[tex][tey],p1.a[p.x][p.y]);
p1.cantor=cantor(p1.a);
if(!vis[p1.cantor]||p.g+1<vis[p1.cantor]){
vis[p1.cantor]=p.g+1;
p1.g++; p1.get_h();
p1.len=p.len+1;
p1.process[p1.len]=chars[i];
q.push(p1);
}
}
}
}
printf("Case %d: %d\n",++k,p.len+1);
printf("%s",p.process);
pfn();
}
return 0;
}
IDA*算法:
参考了https://blog.csdn.net/starcuan/article/details/18963153
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define rep1(i,s,e) for(int i=s;i<e;i++)
#define rep2(i,s,e,c) for(int i=s;i>=e;i--)
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define pfn() printf("\n")
#define pfs() printf(" ")
#define sfi(x) scanf("%d",&x)
#define sfi1(x,y) scanf("%d%d",&x,&y)
#define sff(x) scanf("%lf",&x)
#define sfl(x) scanf("%lld",&x)
#define memset1(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int MAX = 1e4 + 50;
const int mod = 996873654;
struct node{
int a[3][3];
int x,y;//X所在位置
}src;
int xy[][2]={{1,0},{0,-1},{0,1},{-1,0}};//d,l,r,u,因为是按顺序dfs的,所以字典序最小
char chars[5]={'d','l','r','u'};
char reverse_chars[5]={'u','r','l','d'};
int maxDepth=0,temp;//深度边界,下一次的深度边界
char path[130];//最优解
vector< pair<int,int> > v(10);
int get_h(int a[][3]){//估值函数
int h=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
int x = a[i][j];
//其余八个数确认了,剩下一个数就确定位置了
if(x) h += abs(i-v[x].first) + abs(j-v[x].second);
}
}
return h;
}
void init(string s,string t){
int k=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(t[k]=='X') t[k]='0';
v[t[k++]-'0']=make_pair(i,j);
}
}
k=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(s[k]=='X'){
s[k]='0';
src.x=i; src.y=j;
}
src.a[i][j]=s[k++]-'0';
}
}
}
bool dfs(node cur, int depth, int h, char preDir){
//IDA*估值函数剪枝
//当前局面的估价函数值+当前的搜索深度>预定义的最大搜索深度时剪枝
if(depth + h > maxDepth){
temp=min(temp,depth+h);
return false;
}
if(h==0){
path[depth]='\0';
return true;
}
node next;
for(int i=0;i<4;i++){
if(reverse_chars[i]==preDir) continue;//不返回上一个状态
next=cur;
next.x+=xy[i][0]; next.y+=xy[i][1];
if(next.x<0||next.x>2||next.y<0||next.y>2) continue;
swap(next.a[next.x][next.y],next.a[cur.x][cur.y]);
int next_h=get_h(next.a);//计算next的h值
path[depth]=chars[i];
if(dfs(next,depth+1,next_h,chars[i])){
return true;
}
}
return false;
}
void IDAstar(){
int h=get_h(src.a);
for(maxDepth=h;;maxDepth=temp){
temp=inf;
if(dfs(src,0,h,'\0')) break;
}
}
int main(){
int t,k=0;
sfi(t);
while(t--){
string s,t;
cin>>s>>t;
init(s,t);
IDAstar();
printf("Case %d: %d\n",++k,strlen(path));
printf("%s\n",path);
}
return 0;
}
可以拿来对拍答案的案例:
12
X87654321
12345678X
687X54321
12345678X
8X7654321
12345678X
687354X21
12345678X
6875X4321
12345678X
8576X4321
12345678X
87X654321
12345678X
21854376X
12345678X
21X568734
12345678X
2185X6734
12345678X
241568X73
12345678X
178254X63
12345678X