zoukankan      html  css  js  c++  java
  • Uva1343-The Rotation Game-IDA*算法

    原题https://uva.onlinejudge.org/external/13/1343.pdf


    题意:  

    有个#字型的棋盘,2行2列,一共24个格。

    如图:每个格子是1或2或3,一共8个1,8个2,8个3.

    有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面。

    求:使中心8个格子数字一致的最少步骤,要输出具体的操作步骤及最终中心区域的数字。如果有多个解,输出字典序最小的操作步骤。


    分析:

    很明显的一个状态空间搜索问题,不过可以注意到,虽然每一个状态有八个可能的后续状态,随着操作数n的增加,总状态数 8^n 还是大得可怕。比如当n=11时,总状态为8^11 = 85亿。就算通过自己创建特制的哈希表进行状态判重,优化效果并不明显,因为最近一直在做状态空间搜索问题,即使用bfs+剪枝+哈希表,这些程序都无一例外的超时了,所以现在看到状态空间搜索问题,如果没有特别好的剪枝,我绝对不敢用bfs了.....

    回到这道题,所有可以用bfs,回溯解决的问题,尤其是解答树的结点数没有明显上限的题,选择用迭代加深搜索算法都特别好用(原因可以参考我上一篇文章)。这里IDA*(迭代加深A*算法)其实说白了就是迭代加深+剪枝.

     

    A*算法是对于每一步考虑 g(n) + h()和MAXD的关系。

    稍微解释一下,g(n)是从起点到当前状态的总步数,MAXD是我们提前通过计算证明得到的最短路线总步数的上限,h()是启发函数,是整个算法的关键,我们设计的h()可以预估从当前状态到目标状态至少需要的步数。

    这样,上面的关系式就很好理解了。g(n) + h() > MAXD 意味着当前已经走的步数+至少还需要的步数 > 我可以走的步数上限,这种状态,必然已经没有继续的必要,回溯。

    对于这道题,可以注意到,对于每一次操作,我们最多可以让中心格子多一个目标数字,如果当前中心格子待整理的数字个数大于我们还可以走的步数,回溯。

    这样,就得到了

        if (d + num_unordered() > MAXD) return false;

        这一核心剪枝公式。 剩下的就简单了。

    代码只有52行,还是很简洁的。而且运行速度很快。过30组数据只用了126ms.

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 const int MAXN = 24, LEN = 8;
     6 int board[LEN][LEN - 1] = { {0, 2, 6, 11, 15, 20, 22}, {1, 3, 8, 12, 17, 21, 23},
     7                         {10, 9, 8, 7, 6, 5, 4}, {19, 18, 17, 16, 15, 14, 13},
     8                         {23, 21, 17, 12, 8, 3, 1}, {22, 20, 15, 11, 6, 2, 0},
     9                         {13, 14, 15, 16, 17, 18, 19}, {4, 5, 6, 7, 8, 9, 10} };
    10 int check_order[] = {6, 7, 8, 11, 12, 15, 16, 17}, a[MAXN], maxd;
    11 char order[30];
    12 
    13 int unordered() {
    14     int n1 = 0, n2 = 0, n3 = 0;
    15     for (int i = 0; i < LEN; i++) 
    16         if (a[check_order[i]] == 1)  n1++;
    17         else if (a[check_order[i]] == 2) n2++;
    18         else n3++;
    19     return LEN - max(max(n1, n2), n3);
    20 }
    21 
    22 void rotate(int di) {
    23     int t = a[board[di][0]];
    24     for (int i = 1; i < LEN - 1; i++) a[board[di][i - 1]] = a[board[di][i]];
    25     a[board[di][LEN - 2]] = t;
    26 }
    27 
    28 bool dfs(int d) {
    29     int cnt = unordered();
    30     if (!cnt) return true;
    31     if (cnt + d > maxd) return false;
    32     int temp[MAXN]; memcpy(temp, a, sizeof(a));
    33     for (int i = 0; i < LEN; i++) {
    34         rotate(i);
    35         order[d] = i + 'A';
    36         if (dfs(d + 1)) return true;
    37         memcpy(a, temp, sizeof(a));
    38     }
    39     return false;
    40 }
    41 
    42 int main() {
    43     freopen("in", "r", stdin);
    44     while (scanf("%d", &a[0]) && a[0]) {
    45         for (int i = 1; i < MAXN; i++) scanf("%d", &a[i]);
    46         if (!unordered()) { printf("No moves needed
    %d
    ", a[6]); continue;}
    47         for (maxd = 1;; maxd++) if (dfs(0)) break;
    48         for (int i = 0; i < maxd; i++) printf("%c", order[i]);
    49         printf("
    %d
    ", a[6]);
    50     }
    51     return 0;
    52 }

     

    顺便纪念一下排第六(前面3个是virtual oj......)

  • 相关阅读:
    c++求最大公约数、最小公倍数
    五个简单的shell脚本
    微信小程序slot(一)
    小程序详解子传父
    小程序封装组件详解父传子
    小程序生命周期详解
    小程序之confirm-type改变键盘右下角的内容和input按钮详解
    小程序之按钮你不知道的v2
    小程序image图片缩小不变形
    小程序之navigator跳转方式
  • 原文地址:https://www.cnblogs.com/Bowen-/p/4955782.html
Copyright © 2011-2022 走看看