• 【luogu P2324 [SCOI2005]骑士精神】 题解


    题目链接:https://www.luogu.org/problemnew/show/P2324

    不懂怎么剪枝,所以说,,我需要氧气。。

    第一道A*

     1 // luogu-judger-enable-o2
     2 #include <cstdio>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <cstring>
     6 #define maxn 10
     7 using namespace std;
     8 int T, ans = 16, mx, sx, sy;
     9 int aim[6][6] = {{0,0,0,0,0,0},
    10                  {0,2,2,2,2,2},
    11                  {0,1,2,2,2,2},
    12                  {0,1,1,0,2,2},
    13                  {0,1,1,1,1,2},
    14                  {0,1,1,1,1,1}};
    15 int dx[8] = {-2,-1,1,2,2,1,-1,-2};
    16 int dy[8] = {1,2,2,1,-1,-2,-2,-1};
    17 int a[maxn][maxn];
    18 inline int h()
    19 {
    20     int ret = 0;
    21     for(int i = 1; i <= 5; i++)
    22     for(int j = 1; j <= 5; j++)
    23     if(a[i][j] != aim[i][j]) ret++;
    24     return ret;
    25 }
    26 inline bool move(int x, int y)
    27 {
    28     if(x >= 1 && x <= 5 && y >= 1 && y <= 5) return true;
    29     return false; 
    30 }
    31 void A_star(int now, int x, int y)
    32 {
    33     if(now > mx) return;
    34     int cnt = h();
    35     if(cnt == 0)
    36     {
    37         ans = min(ans, now);
    38         return;
    39     }
    40     if(cnt + now - 1 > mx) return;
    41     int nowx, nowy;
    42     for(int i = 0; i <= 7; i++)
    43     {
    44         nowx = x + dx[i];
    45         nowy = y + dy[i];
    46         if(move(nowx, nowy))
    47         {
    48             swap(a[nowx][nowy],a[x][y]);
    49             A_star(now+1,nowx,nowy);
    50             swap(a[nowx][nowy],a[x][y]);
    51         }
    52     }
    53 }
    54 int main()
    55 {
    56     cin.sync_with_stdio(false);
    57     cin>>T;
    58     while(T--)
    59     {
    60         char s;
    61         ans = 16;
    62         memset(a,0,sizeof(a));
    63         for(int i = 1; i <= 5; i++)
    64         for(int j = 1; j <= 5; j++)
    65         {
    66             cin>>s;
    67             if(s != '*')
    68             a[i][j] = s + 1 - '0';
    69             if(s == '*')
    70             {
    71                 a[i][j] = 0;
    72                 sx = i;
    73                 sy = j;
    74             }
    75         }
    76         for(mx = 1; mx <= 15; mx++)
    77         {
    78             A_star(0,sx,sy);
    79             if(ans == mx) break;
    80             
    81         }
    82         if(ans == 16)
    83         {
    84             cout<<-1<<endl;
    85         }
    86         else 
    87         {
    88             cout<<ans<<endl;
    89         }
    90     }
    91     return 0;
    92 }

    隐约雷鸣,阴霾天空,但盼风雨来,能留你在此。

    隐约雷鸣,阴霾天空,即使天无雨,我亦留此地。

  • 相关阅读:
    测试流程规范系列(5):BUG提交
    测试流程规范系列(6):测试报告
    测试流程规范系列(7):测试准出
    安全测试系列(1):基本概念
    Linux系列(1):常用命令
    Linux——常用命令详解
    Ant——ant的使用
    java——XML与java对象装换
    SpringMVC——form标签的使用
    SpringMVC案例1——对User表进行CRUD操作
  • 原文地址:https://www.cnblogs.com/MisakaAzusa/p/9004653.html
Copyright © 2020-2023  润新知