1. 程式人生 > >HDU - 3567 Eight II IDA*

HDU - 3567 Eight II IDA*

tee pan log cfa output hash post operation return

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.

InputThe 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.
OutputFor 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

題目大意:
求一個八數碼問題,從第一個狀態,到第二個狀態所需要的最小步數是多少,並且輸出移動方式,題目保證有解。

方法:
IDA*搜索算法
由於是用遞歸方式進行的,進行最大深度限制,所以不需要hash判重。
估價函數為 當前狀態已經走的步數 + 當前狀態距離目標狀態的曼哈頓距離 (拋去X)

代碼:
#include<iostream>
using namespace std;
#include<cstdio>
#include<vector>
#include<cmath>
int xx[10],yy[10];
int f[12];
int maps1[3][3],maps2[3][3];
int get_value()//估值函數
{
	int q,ans=0;
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			q=maps1[i][j];
			if(q==0) 
				continue;
			int tl=i-xx[q];
			int tr=j-yy[q];
			ans+=abs(tl)+abs(tr);
		}
	}
	return ans;
}
int main(){
	void deal(int);
	f[1]=1;f[0]=0;
	for(int i=2;i<=11;i++)
		f[i]=f[i-1]*i;
	int t;
	int p=1;
	scanf("%d",&t);
	while(t--)
		deal(p++);
	return 0;
}
char s1[20],s2[20];
int dx[]={1,0,0,-1};
int dy[]={0,-1,1,0};
char s[]={‘d‘,‘l‘,‘r‘,‘u‘};
int max_deep=0;
int ans=-1;
vector<char> lj;
int next_deep;
void IDA_Star(int deep,int x,int y,int pre){
	int cj=get_value();
	if(cj==0){
		ans=deep;
		return;
	}
	int value=deep+cj;
	if(value>max_deep){
		if(next_deep>value)
		next_deep=value;
		return;
	}
	for(int i=0;i<4;i++){
		if(i+pre==3)
			continue;
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(nx<0||ny<0||nx>=3||ny>=3)
			continue;
		swap(maps1[x][y],maps1[nx][ny]);
		IDA_Star(deep+1,nx,ny,i);
		if(ans>-1){
			lj.push_back(s[i]);
			return;
		}
		swap(maps1[x][y],maps1[nx][ny]);
	}
}
void deal(int sq){
	scanf("%s%s",s1,s2);
	int xxx=0,yyy=0;
	for(int i=0;i<9;i++){
		if(s1[i]!=‘X‘)
		maps1[i/3][i%3]=s1[i]-48;
		else
		maps1[i/3][i%3]=0,xxx=i/3,yyy=i%3;
	}
	
	for(int i=0;i<9;i++){
		if(s2[i]!=‘X‘)
		maps2[i/3][i%3]=s2[i]-48;
		else
		maps2[i/3][i%3]=0;
		xx[maps2[i/3][i%3]]=i/3;
		yy[maps2[i/3][i%3]]=i%3;
	}
	lj.clear();
	max_deep=get_value();
	ans=-1;
	while(1){
	next_deep=0x3f3f3f3f;
	IDA_Star(0,xxx,yyy,next_deep);
	if(ans>-1)
		break;
	max_deep=next_deep;
	}
	printf("Case %d: %d\n",sq,ans);
	for(int i=lj.size()-1;i>=0;i--)
	putchar(lj[i]);
	putchar(‘\n‘);
}

  

HDU - 3567 Eight II IDA*