码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 3567 Eight II IDA*

时间:2018-01-25 17:21:47      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:dea   nsis   this   post   its   sts   stream   solution   change   

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*

标签:dea   nsis   this   post   its   sts   stream   solution   change   

原文地址:https://www.cnblogs.com/xfww/p/8352121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!