HDU 1667

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1667

        据说是IDA*搜索入门题,以前老是不敢看IDA*,认为很复杂。其实也没想象中的那么难。看IDA*先对 迭代搜索有了解。然后看IDA*就简单多了。IDA*就是在迭代加深上添加了评估函数。。评估函数一般其实就是,当前状态预估到结束状态的最小步数。


下面是AC代码:

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int map[10][10];
bool check(){
    if(map[3][3]!=map[3][4]) return false; if(map[3][3]!=map[3][5]) return false;
    if(map[3][3]!=map[4][3]) return false; if(map[3][3]!=map[4][5]) return false;
    if(map[3][3]!=map[5][3]) return false; if(map[3][3]!=map[5][4]) return false;
    if(map[3][3]!=map[5][5]) return false;
    return true;
}

void moveA(){
    int tmp=map[1][3];
    for(int i=1;i<=6;i++) map[i][3]=map[i+1][3];
     map[7][3]=tmp;
}
void moveB(){
    int tmp=map[1][5];
    for(int i=1;i<=6;i++) map[i][5]=map[i+1][5];
     map[7][5]=tmp;
}

void moveC(){
    int tmp=map[3][7];
    for(int i=7;i>=2;i--) map[3][i]=map[3][i-1];
    map[3][1]=tmp;
}
void moveD(){
    int tmp=map[5][7];
    for(int i=7;i>=2;i--) map[5][i]=map[5][i-1];
    map[5][1]=tmp;
}

void moveE(){
   int tmp=map[7][5];
   for(int i=7;i>=2;i--) map[i][5]=map[i-1][5];
    map[1][5]=tmp;
}
void moveF(){
   int tmp=map[7][3];
   for(int i=7;i>=2;i--) map[i][3]=map[i-1][3];
   map[1][3]=tmp;
}

 void moveG(){
   int tmp=map[5][1];
   for(int i=1;i<=6;i++) map[5][i]=map[5][i+1];
   map[5][7]=tmp;
}

 void moveH(){
   int tmp=map[3][1];
   for(int i=1;i<=6;i++) map[3][i]=map[3][i+1];
   map[3][7]=tmp;
}
int smallest_deep(){                                     //评估函数
    int cnt[4]={0};
    for(int i=3;i<=5;i++){
         cnt[map[3][i]]++;
         cnt[map[5][i]]++;
     }

     cnt[map[4][3]]++;
     cnt[map[4][5]]++;

     int max=0;
     for(int i=1;i<=3;i++){
         if(max<cnt[i]) max=cnt[i];
    }
    return 8-max;

}
int deep;
char ans[1000];
bool dfs(int cur_deep){
    if(cur_deep==deep) return check();

    if(cur_deep+smallest_deep()>deep) return false;

    moveA();  ans[cur_deep]='A';   if(dfs(cur_deep+1)) return true; moveF();

    moveB();  ans[cur_deep]='B';   if(dfs(cur_deep+1)) return true; moveE();

    moveC();  ans[cur_deep]='C';   if(dfs(cur_deep+1)) return true; moveH();

    moveD();  ans[cur_deep]='D';   if(dfs(cur_deep+1)) return true; moveG();

    moveE();  ans[cur_deep]='E';   if(dfs(cur_deep+1)) return true; moveB();

    moveF();  ans[cur_deep]='F';   if(dfs(cur_deep+1)) return true; moveA();

    moveG();  ans[cur_deep]='G';   if(dfs(cur_deep+1)) return true; moveD();

    moveH();  ans[cur_deep]='H';   if(dfs(cur_deep+1)) return true; moveC();

    return false;
}
int main(){
    for(int i=1;i<=7;i++)  map[3][i]=map[5][i]=1;

    for(int i=1;i<=7;i++)  map[i][3]=map[i][5]=1;

    while(true){
        for(int i=1;i<=7;i++) for(int j=1;j<=7;j++){
             if(map[i][j]==0)  continue;
             scanf("%d",&map[i][j]);
             if(map[i][j]==0)  return 0;
        }
        if(check()){
            printf("No moves needed\n");
             printf("%d\n",map[3][3]);
            continue;
        }
        deep=1;

        while(true){
            if(dfs(0)) break;
            deep++;
        }
        ans[deep]='\0';
        cout<<ans<<endl;
        cout<<map[3][3]<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值