【搜索进阶】HDU 1667 The Rotation Game

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1667
IDA*的入门题目,状态过多容易超内存,正好体现了IDA*的优势,每次操作移动能使一个数字进入中间的八个位置,所以构造h()=8-max(1,2,3在中间8个位置的个数)
#include<cstdio>
#include<cstring>
using namespace std;
int maze[10][10]={0};
bool check(){
    if(maze[3][3]!=maze[3][4]) return false;
    if(maze[3][3]!=maze[3][5]) return false;
    if(maze[3][3]!=maze[4][3]) return false;
    if(maze[3][3]!=maze[5][3]) return false;
    if(maze[3][3]!=maze[5][4]) return false;
    if(maze[3][3]!=maze[5][5]) return false;
    if(maze[3][3]!=maze[4][5]) return false;
    return true;
} 
void roA(){
    int tmp=maze[1][3];
    for(int i=1;i<=6;i++) maze[i][3]=maze[i+1][3];
    maze[7][3]=tmp;
} 
void roB(){
    int tmp=maze[1][5];
    for(int i=1;i<=6;i++) maze[i][5]=maze[i+1][5];
    maze[7][5]=tmp;
}
void roC(){
    int tmp=maze[3][7];
    for(int i=7;i>=2;i--) maze[3][i]=maze[3][i-1];
    maze[3][1]=tmp;
}
void roD(){
    int tmp=maze[5][7];
    for(int i=7;i>=2;i--) maze[5][i]=maze[5][i-1];
    maze[5][1]=tmp;
} 
void roE(){
    int tmp=maze[7][5];
    for(int i=7;i>=2;i--) maze[i][5]=maze[i-1][5];
        maze[1][5]=tmp;
} 
void roF(){
    int tmp=maze[7][3];
    for(int i=7;i>=2;i--) maze[i][3]=maze[i-1][3];
    maze[1][3]=tmp;            
} 
void roG(){
    int tmp=maze[5][1];
    for(int i=1;i<=6;i++) maze[5][i]=maze[5][i+1];
    maze[5][7]=tmp;
} 
void roH(){
    int tmp=maze[3][1];
    for(int i=1;i<=6;i++) maze[3][i]=maze[3][i+1];
    maze[3][7]=tmp;
} 
int h(){
    int a[4]={0};
    for(int i=3;i<=5;i++){
        a[maze[3][i]]++;
        a[maze[5][i]]++;
    }
    a[maze[4][3]]++;
    a[maze[4][5]]++; 
    int max=0;
    for(int i=1;i<=3;i++){
        if(max<a[i]) max=a[i];
    }
    return 8-max;
} 
char ans[100];
int deep;
bool dfs(int d){
    if(d==deep) return check();
    if(h()+d>deep) return false;
    roA();
    ans[d]='A';
    if(dfs(d+1)) return true;
    roF();
    roB();
    ans[d]='B';
    if(dfs(d+1)) return true;
    roE();
    roC();
    ans[d]='C';
    if(dfs(d+1)) return true;
    roH(); 
    roD();
    ans[d]='D';
    if(dfs(d+1)) return true;
    roG(); 
    roE();
    ans[d]='E';
    if(dfs(d+1)) return true;
    roB(); 
    roF();
    ans[d]='F';
    if(dfs(d+1)) return true;
    roA();
    roG();
    ans[d]='G';
    if(dfs(d+1)) return true;
    roD();
    roH();
    ans[d]='H';
    if(dfs(d+1)) return true;
    roC();
    return false;
} 
int main(){
    for(int i=1;i<=7;i++) maze[3][i]=maze[5][i]=1;
    for(int i=1;i<=7;i++) maze[i][3]=maze[i][5]=1;
    while(true){
        for(int i=1;i<=7;i++){
            for(int j=1;j<=7;j++){
                if(maze[i][j]==0) continue;
                scanf("%d",&maze[i][j]);
                if(maze[i][j]==0) return 0;
            }
        }
        if(check()){
            puts("No moves needed");
            printf("%d\n",maze[3][3]);
            continue;
        }
        deep=1;
        while(true){
            if(dfs(0)) break;
            deep++;
        }
        for(int i=0;i<deep;i++)    putchar(ans[i]);
        puts("");
        printf("%d\n",maze[3][3]);
    }
    return 0;
}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值