HDU - 3567 Eight II IDA*

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‘);
}

  

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

时间: 2024-10-12 10:11:23

HDU - 3567 Eight II IDA*的相关文章

HDU 3567 Eight II(八数码 II)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; fo

HDU 3567 Eight II

Eight II Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 356764-bit integer IO format: %I64d      Java class name: Main Eight-puzzle, which is also called "Nine grids", comes from an old game. In this ga

HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3

http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过,而且也不好控制字典序 换个角度想,虽然起始状态有很多,但是到底哪一位是1,哪一位是2不是最重要的,最重要的是和目标状态对应,所以可以把起始状态重新编码为"12345678"这种形式(先不考虑X),然后目标状态也对应过去,当考虑X的时候,我们可以认为起始状态只有9种,分别是'X'在各个位置的

HDU 3567 Eight II BFS预处理

题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问题实际上是每个小块位置的变化,上面的数字只是用来标记位置的, 所以通过映射将初末序列进行置换就好了,然后因为每次的x字符的置换位置不一样 所以需要以123456789这个初始串打9遍表就好了733ms #include <iostream> #include <cstdio> #inc

hdu 5147 Sequence II(树状数组)

题目链接:hdu 5147 Sequence II 预处理每个位置作为b和c可以组成的对数,然后枚举b的位置计算. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int maxn = 50005; int N, arr[maxn], fenw[maxn], lef[maxn], rig[maxn]; #d

HDU 3081Marriage Match II(二分+并查集+网络流之最大流)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3081 有一段时间没写最大流的题了,这题建图居然想了好长时间...刚开始是按着最终的最大流即是做多轮数去想建图,结果根本没思路,后来想了想,可以用二分答案的思想来找最终答案.然后很明显的并查集,但是并查集学的略渣,居然卡在并查集上了..= =. 但是也不是并查集的事..是我建图的思想太正了,稍微用点逆向思维并查集就可以很好利用了. 建图思路是:建立一个源点与汇点,将女孩与源点相连,男孩与汇点相连,权值

HDU 2236 无题II(二分图匹配+二分)

HDU 2236 无题II 题目链接 思路:行列只能一个,想到二分图,然后二分区间长度,枚举下限,就能求出哪些边是能用的,然后建图跑二分图,如果最大匹配等于n就是符合的 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 105; int t, n, x[N][N], have[N]

HDU 5919 Sequence II(主席树+逆序思想)

Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1422    Accepted Submission(s): 362 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2

hdu 5233 Gunner II 离散化

Gunner II Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5233 Description 很久很久以前,有一个叫Jack的枪手.他非常喜欢打猎.一天,他去了一个小树林.那儿有n只鸟,还有n棵树.第i只鸟站在第i棵树的顶端.这些树从左到右排成一条直线.每一棵树都有它的高度.Jack站在最左边那棵树的左边.当Jack在高度为H的地方向右发射一棵子弹时,站在高度为