zoukankan      html  css  js  c++  java
  • hdu 2918(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2918

    思路:这道题与前面几道类似,可以说是被秒杀了!!!构造启发式函数h()=(cnt+3)/4(cnt为不在位的点的个数)。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 using namespace std;
      6 
      7 int map[4][4];
      8 char str[14];
      9 int max_deep;
     10 
     11 int Get_H()
     12 {
     13     int cnt=0;
     14     for(int i=1;i<=3;i++)
     15         for(int j=1;j<=3;j++)
     16             if(map[i][j]!=(i-1)*3+j)cnt++;
     17     return (cnt+3)/4;
     18 }
     19 
     20 void MoveL_A()
     21 {
     22     int tmp=map[1][1];
     23     map[1][1]=map[1][2];
     24     map[1][2]=map[2][2];
     25     map[2][2]=map[2][1];
     26     map[2][1]=tmp;
     27 }
     28 
     29 void MoveR_A()
     30 {
     31     int tmp=map[1][1];
     32     map[1][1]=map[2][1];
     33     map[2][1]=map[2][2];
     34     map[2][2]=map[1][2];
     35     map[1][2]=tmp;
     36 }
     37 
     38 void MoveL_B()
     39 {
     40     int tmp=map[1][2];
     41     map[1][2]=map[1][3];
     42     map[1][3]=map[2][3];
     43     map[2][3]=map[2][2];
     44     map[2][2]=tmp;
     45 }
     46 
     47 void MoveR_B()
     48 {
     49     int tmp=map[1][2];
     50     map[1][2]=map[2][2];
     51     map[2][2]=map[2][3];
     52     map[2][3]=map[1][3];
     53     map[1][3]=tmp;
     54 }
     55 
     56 void MoveL_C()
     57 {
     58     int tmp=map[2][1];
     59     map[2][1]=map[2][2];
     60     map[2][2]=map[3][2];
     61     map[3][2]=map[3][1];
     62     map[3][1]=tmp;
     63 }
     64 
     65 void MoveR_C()
     66 {
     67     int tmp=map[2][1];
     68     map[2][1]=map[3][1];
     69     map[3][1]=map[3][2];
     70     map[3][2]=map[2][2];
     71     map[2][2]=tmp;
     72 }
     73 
     74 void MoveL_D()
     75 {
     76     int tmp=map[2][2];
     77     map[2][2]=map[2][3];
     78     map[2][3]=map[3][3];
     79     map[3][3]=map[3][2];
     80     map[3][2]=tmp;
     81 }
     82 
     83 void MoveR_D()
     84 {
     85     int tmp=map[2][2];
     86     map[2][2]=map[3][2];
     87     map[3][2]=map[3][3];
     88     map[3][3]=map[2][3];
     89     map[2][3]=tmp;
     90 }
     91 
     92 bool IDA_star(int deep)
     93 {
     94     if(Get_H()+deep>max_deep)return false;
     95     if(deep==max_deep)return true;
     96     
     97     MoveL_A();
     98     if(IDA_star(deep+1))return true;
     99     MoveR_A();
    100 
    101     MoveR_A();
    102     if(IDA_star(deep+1))return true;
    103     MoveL_A();
    104 
    105     MoveL_B();
    106     if(IDA_star(deep+1))return true;
    107     MoveR_B();
    108     
    109     MoveR_B();
    110     if(IDA_star(deep+1))return true;
    111     MoveL_B();
    112 
    113     MoveL_C();
    114     if(IDA_star(deep+1))return true;
    115     MoveR_C();
    116 
    117     MoveR_C();
    118     if(IDA_star(deep+1))return true;
    119     MoveL_C();
    120 
    121     MoveL_D();
    122     if(IDA_star(deep+1))return true;
    123     MoveR_D();
    124 
    125     MoveR_D();
    126     if(IDA_star(deep+1))return true;
    127     MoveL_D();
    128     return false;
    129 }
    130 
    131 
    132 int main()
    133 {
    134     int n,t=1;
    135     while(~scanf("%s",str)){
    136         if(strcmp(str,"0000000000")==0)break;
    137         n=str[0]-'0';
    138         for(int j=1,i=1;i<=9;j++,i++){
    139             map[(i-1)/3+1][j]=str[i]-'0';
    140             if(i%3==0)j=0;
    141         }
    142         /*
    143         for(int i=1;i<=3;i++){
    144             for(int j=1;j<=3;j++){
    145                 printf("%d ",map[i][j]);
    146             }
    147             printf("
    ");
    148         }*/
    149         if(Get_H()==0){
    150             printf("%d. 0
    ",t++);
    151             continue;
    152         }
    153         for(max_deep=1;max_deep<=n;max_deep++){
    154             if(IDA_star(0))break;
    155         }
    156         if(max_deep>n){
    157             printf("%d. -1
    ",t++);
    158         }else 
    159             printf("%d. %d
    ",t++,max_deep);
    160     }
    161     return 0;
    162 }
    163 
    164 
    165 
    166 
    167 
    168 
    169             
    View Code
  • 相关阅读:
    ASP.NET Core2利用MassTransit集成RabbitMQ
    ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持wordexcelpptpdf等格式)
    ASP.NET Core2利用Jwt技术在服务端实现对客户端的身份认证
    net core System.Drawing is not supported on this platform.
    小程序开发之维护access_token
    net core 100个案例
    msgSystem.Drawing is not supported on this platform【netcore】
    wpf,前端动画demo,鱼眼效果
    自定义控件,重写 TextBox 实例
    TextBox输入法控制,进入输入框则启用或禁用输入法(ime),禁用后只能输入英文
  • 原文地址:https://www.cnblogs.com/wally/p/3328084.html
Copyright © 2011-2022 走看看