hdu 2918 IDA*

8 篇文章 0 订阅

每次maxdeep没有初始化,WA了一下午

AC代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;

int num[3][3];
int pos[][2] = { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 1 }, { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 } };
int maxdeep;

int rot_A_l(){
	int temp = num[0][0];
	num[0][0] = num[0][1];
	num[0][1] = num[1][1];
	num[1][1] = num[1][0];
	num[1][0] = temp;
	return 0;
}

int rot_A_r(){
	int temp = num[0][1];
	num[0][1] = num[0][0];
	num[0][0] = num[1][0];
	num[1][0] = num[1][1];
	num[1][1] = temp;
	return 0;
}

int rot_B_l(){
	int temp = num[0][1];
	num[0][1] = num[0][2];
	num[0][2] = num[1][2];
	num[1][2] = num[1][1];
	num[1][1] = temp;
	return 0;
}

int rot_B_r(){
	int temp = num[0][2];
	num[0][2] = num[0][1];
	num[0][1] = num[1][1];
	num[1][1] = num[1][2];
	num[1][2] = temp;
	return 0;
}

int rot_C_l(){
	int temp = num[1][0];
	num[1][0] = num[1][1];
	num[1][1] = num[2][1];
	num[2][1] = num[2][0];
	num[2][0] = temp;
	return 0;
}

int rot_C_r(){
	int temp = num[1][1];
	num[1][1] = num[1][0];
	num[1][0] = num[2][0];
	num[2][0] = num[2][1];
	num[2][1] = temp;
	return 0;
}

int rot_D_l(){
	int temp = num[1][1];
	num[1][1] = num[1][2];
	num[1][2] = num[2][2];
	num[2][2] = num[2][1];
	num[2][1] = temp;
	return 0;
}

int rot_D_r(){
	int temp = num[1][2];
	num[1][2] = num[1][1];
	num[1][1] = num[2][1];
	num[2][1] = num[2][2];
	num[2][2] = temp;
	return 0;
}

int h(){
	int ans = 0;
	for( int i = 0; i < 3; i++ ){
		for( int j = 0; j < 3; j++ ){
			int temppos = num[i][j];
			ans = max( ans, abs( pos[temppos][0] - i ) + abs( pos[temppos][1] - j ) );
		}
	}
	return ans;
}

bool DFS( int deep ){
	if( h() == 0 ){
		return true;
	}
	if( h() > maxdeep - deep ){
		return false;
	}

	rot_A_l();
	if( DFS( deep + 1 ) ){
		return true;
	}
	rot_A_r();

	rot_A_r();
	if( DFS( deep + 1 ) ){
		return true;
	}
	rot_A_l();

	rot_B_l();
	if( DFS( deep + 1 ) ){
		return true;
	}
	rot_B_r();

	rot_B_r();
	if( DFS( deep + 1 ) ){
		return true;
	}
	rot_B_l();

	rot_C_l();
	if( DFS( deep + 1 ) ){
		return true;
	}
	rot_C_r();

	rot_C_r();
	if( DFS( deep + 1 ) ){
		return true;
	}
	rot_C_l();

	rot_D_l();
	if( DFS( deep + 1 ) ){
		return true;
	}
	rot_D_r();

	rot_D_r();
	if( DFS( deep + 1 ) ){
		return true;
	}
	rot_D_l();

	return false;
}

int main(){
	int Y, Case = 1;
	char s[20];
	while( scanf( "%s", s ) && strcmp( s, "0000000000" ) != 0 ){
		Y = s[0] - '0';
		int temp = 1;
		for( int i = 0; i < 3; i++ ){
			for( int j = 0; j < 3; j++ ){
				num[i][j] = s[temp++] - '1';
			}
		}
		maxdeep = 0;
		while( maxdeep <= Y ){
			if( DFS( 0 ) ){
				break;			
			}
			maxdeep++;
		}
		if( maxdeep <= Y ){
			cout << Case++ << ". " << maxdeep << endl; 
		}else{
			cout << Case++ << ". " << -1 << endl; 
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值