首页 > 搜索 > DFS搜索 > HDU 2918-Tobo or not Tobo-BFS-[解题报告]HOJ
2014
02-23

HDU 2918-Tobo or not Tobo-BFS-[解题报告]HOJ

Tobo or not Tobo

问题描述 :

The game of Tobo is played on a plastic board designed into a 3 × 3 grid with cells numbered from 1 to 9 as shown in figure (a). The grid has four dials (labeled “A" to “D" in the figure.) Each dial can be rotated in 90 degrees increment in either direction. Rotating a dial causes the four cells currently adjacent to it to rotate along. For example, figure (b) shows the Tobo after rotating dial “A" once in a clockwise direction. Figure (c) shows the Tobo in figure (b) after rotating dial “D" once in a counterclockwise direction.

Kids love to challenge each other playing the Tobo. Starting with the arrangement shown in figure (a), (which we’ll call the standard arrangement,) one kid would randomly rotate the dials, X number of times, in order to “shuffle" the board. Another kid then tries to bring the board back to its standard arrangement, taking no more than X rotations to do so. The less rotations are needed to restore it, the better. This is where you see a business opportunity. You would like to sell these kids a program to advise them on the minimum number of steps needed to bring a Tobo back to its standard arrangement.

输入:

Your program will be tested on one or more test cases. Each test case is specified on a line by itself. Each line is made of 10 decimal digits. Let’s call the first digit Y . The remaining 9 digits are non-zeros and describe the current arrangement of the Tobo in a row-major top-down, left-to-right ordering. The first sample case corresponds to figure (c).

The last line of the input file is a sequence of 10 zeros.

输出:

Your program will be tested on one or more test cases. Each test case is specified on a line by itself. Each line is made of 10 decimal digits. Let’s call the first digit Y . The remaining 9 digits are non-zeros and describe the current arrangement of the Tobo in a row-major top-down, left-to-right ordering. The first sample case corresponds to figure (c).

The last line of the input file is a sequence of 10 zeros.

样例输入:

3413569728
1165432789
0000000000

样例输出:

1. 2
2. -1

转载请注明出处,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents          
by—cxlove

继续IDA*搜索,估价函数H仍然是曼哈顿距离,每一次转换会改变4个位置的曼哈顿距离,分别改变1,所以把曼哈顿距离和+3/4便可以作为H函数,表示至少需要多少步,一个DFS的剪枝。

这题最多九步,BFS应该也无压力

可惜没有优化到0MS。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LD long double
#define LL __int64
#define M 200005
using namespace std;
int T,a[9];
int depth;
char str[10];
bool flag;
int rotation[4][4]={{0,1,4,3},{1,2,5,4},{3,4,7,6},{4,5,8,7}};
int get_h(int *b){
	int ans=0;
	for(int i=0;i<9;i++)
		ans+=abs(i/3-(b[i]-1)/3)+abs(i%3-(b[i]-1)%3);
	return (ans+3)/4;
}
void change(int *b,int kind){
	if(kind&1){
		kind/=2;
		int tmp=b[rotation[kind][3]];
		for(int i=3;i>0;i--)
	    	b[rotation[kind][i]]=b[rotation[kind][i-1]];
    	b[rotation[kind][0]]=tmp;
	}
	else{
		kind/=2;
    	int tmp=b[rotation[kind][0]];
    	for(int i=1;i<4;i++)
	    	b[rotation[kind][i-1]]=b[rotation[kind][i]];
    	b[rotation[kind][3]]=tmp;
	}
}
void IDAstar(int *b,int tmp_depth,int pre){
	if(flag)
		return;
	if(get_h(b)>tmp_depth)
		return;
	if(tmp_depth==0&&get_h(b)==0){
		flag=true;
		return;
	}
	for(int i=0;i<8;i++){
		if(pre>=0&&pre/2==i/2&&(pre%2)^(i%2))
			continue;
		int tmp[9];
		for(int j=0;j<9;j++)
			tmp[j]=b[j];
		change(tmp,i);
		IDAstar(tmp,tmp_depth-1,i);
	}
}
int main(){
	int cas=0;
	while(scanf("%s",str)!=EOF&&strcmp(str,"0000000000")){
		T=str[0]-'0';
		for(int i=0;i<9;i++)
			a[i]=str[i+1]-'0';	
		flag=false;
		for(depth=get_h(a);depth<=T;depth++){	
			IDAstar(a,depth,-1);
			if(flag){
				printf("%d. %d\n",++cas,depth);
				break;
			}
		}
		if(!flag)
			printf("%d. -1\n",++cas);
	}
	return 0;
}

解题参考:http://blog.csdn.net/acm_cxlove/article/details/7746452


, ,