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

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

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

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

分析 : 还是状态空间的搜索,对象就是一个数字序列,判断中心位置是否一样,可以看出如果使用BFS,每一层还是爆炸,所以使用IDA*,关键还是模拟操作和h函数,这里的h函数是这样定义的,可以看出每一次操作,最多给当前局面添加一个符合要求的数字,那就统计一下中心区域最多的相同数字有多少,然后如果8-h > max_depth - cur_depth的话代表最好的情况下都无法解决,剪枝。模拟操作应该就是很白痴的数组转移赋值了,代码很长,很烦,建议看看刘汝佳的代码。

#include<bits/stdc++.h>
using namespace std;
];
int ans_num;
];
bool Is_ok(int *arr)
{
    ];
    ] || temp!=arr[] || temp!=arr[] || temp!=arr[] || temp!=arr[] || temp!=arr[] || temp!=arr[])
        return false;
    return true;
}

inline int h(const int *now)
{
    ];
    memset(cnt, , sizeof(cnt));
    cnt[now[]]++,  cnt[now[]]++,  cnt[now[]]++,
    cnt[now[]]++, cnt[now[]]++, cnt[now[]]++,
    cnt[now[]]++, cnt[now[]]++;
    ], cnt[]);
    ret = max(ret, cnt[]);
    return ret;
}

inline void Change(int *tmp, int one, int two, int three, int four, int five, int six, int seven)
{
    ];
    index[] = one, index[] = two, index[] = three,
    index[] = four, index[] = five, index[] = six;
    index[] = seven;
    int temp = tmp[one];//!
    ; i<; i++)
        tmp[index[i]] = tmp[index[i+]];
    tmp[index[]] = temp;
}

bool DFS(int *now, int cur_depth, int max_depth, int per_dir)
{
     - h(now) > max_depth - cur_depth) return false;
    if(cur_depth >= max_depth) return false;//!?
    ; dir<=; dir++){//!
        ];
        ){
            &&dir==) || (dir==&&per_dir==)) continue;
            &&dir==) || (dir==&&per_dir==)) continue;
            &&dir==) || (dir==&&per_dir==)) continue;
            &&dir==) || (dir==&&per_dir==)) continue;
        }
        ; i<; i++) tmp[i] = now[i];
        int top = cur_depth;
        switch(dir){
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
            : ans[top]=,,,,,,);break;
        }
        if(Is_ok(tmp)){
            ans[top+] = '\0';
            ans_num = tmp[];
            return true;
        }
        , max_depth, dir)) return true;
    }
    return false;
}
int main(void)
{
    ]) && Init[]){
        ; i<=; i++){
            scanf("%d", &Init[i]);
        }
        if(Is_ok(Init)){
            puts("No moves needed");
            printf(]);
            continue;
        }
        ;
        ){
            , max_depth, )) break;
            max_depth++;
        }
        puts(ans);
        printf("%d\n", ans_num);
    }
    ;
}

刘汝佳代码:

// UVa1343 The Rotation Game
// Rujia Liu
// This solutions uses IDA* instead of BFS described in the book, because it's shorter 8-)
// It's shorter because no need for lookup tables and "automatically" lexicographically smallest solution.
#include<cstdio>
#include<algorithm>
using namespace std;

/*
      00    01
      02    03
04 05 06 07 08 09 10
      11    12
13 14 15 16 17 18 19
      20    21
      22    23
*/

// lines E~H are computed with the help of rev[]
][]={
  { , , ,,,,}, // A
  { , , ,,,,}, // B
  {, , , , , , }, // C
  {,,,,,,}, // D
};

] = {, , , , , , , }; // reverse lines of each line

// center squares
] = {, , , , , , , };

];
];

bool is_final() {
  ; i < ; i++)
    ]]) return false;
  return true;
}

int diff(int target) {
  ;
  ; i < ; i++)
    if(a[center[i]] != target) ans++;
  return ans;
}

inline int h() {
  ), diff()), diff());
}

inline void move(int i) {
  ]];
  ; j < ; j++) a[line[i][j]] = a[line[i][j+]];
  a[line[i][]] = tmp;
}

bool dfs(int d, int maxd) {
  if(is_final()) {
    ans[d] = '\0';
    printf("%s\n", ans);
    return true;
  }
  if(d + h() > maxd) return false;
  ; i < ; i++) {
    ans[d] = 'A' + i;
    move(i);
    , maxd)) return true;
    move(rev[i]);
  }
  return false;
}

int main() {
  ; i < ; i++)
    ; j < ; j++) line[i][j] = line[rev[i]][-j];
  ]) ==  && a[]) {
    ; i < ; i++) scanf("%d", &a[i]);
    ; i < ; i++) ;
    if(is_final()) {
      printf("No moves needed\n");
    } else {
      ; ; maxd++)
        , maxd)) break;
    }
    printf(]);
  }
  ;
}

瞎:遇到这种看起来很烦的题目,还是没有那种敏感性去试想状态空间搜索,一来就是想着如何模拟,然后脑袋一团shit,思路根本没有,所以总结应该很重要了,提供了一个思考的方向在那里,真正应该思考的是如何去实现这道题所对应的模型,而不是乱想。

UVa 1343 The Rotation Game (状态空间搜索 && IDA*)的更多相关文章

  1. UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]

    解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...

  2. UVA - 1343 The Rotation Game (BFS/IDA*)

    题目链接 紫书例题. 首先附上我第一次bfs+剪枝TLE的版本: #include<bits/stdc++.h> using namespace std; typedef long lon ...

  3. UVA 1343 The Rotation Game

    题意: 给出图,往A-H方向旋转,使中间8个格子数字相同.要求旋转次数最少,操作序列字典序尽量小. 分析: 用一维数组存24个方格.二维数组代表每个方向对应的7个方格.IDA*剪枝是当8-8个方格中重 ...

  4. UVa 11212 Editing a Book (IDA* && 状态空间搜索)

    题意:你有一篇n(2≤n≤9)个自然段组成的文章,希望将它们排列成1,2,…,n.可以用Ctrl+X(剪切)和Ctrl+V(粘贴)快捷键来完成任务.每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴.注 ...

  5. UVa 1343 旋转游戏(dfs+IDA*)

    https://vjudge.net/problem/UVA-1343 题意:如图所示,一共有8个1,8个2和8个3,如何以最少的移动来使得中间8个格子都为同一个数. 思路:状态空间搜索问题. 用ID ...

  6. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  7. 7-10Editing aBook uva11212(迭代加深搜索 IDA*)

    题意:  给出n( 2<=n<=9) 个乱序的数组  要求拍成升序  每次 剪切一段加上粘贴一段算一次  拍成1 2 3 4 ...n即可     求排序次数 典型的状态空间搜索问题   ...

  8. 埃及分数 迭代加深搜索 IDA*

    迭代加深搜索 IDA* 首先枚举当前选择的分数个数上限maxd,进行迭代加深 之后进行估价,假设当前分数之和为a,目标分数为b,当前考虑分数为1/c,那么如果1/c×(maxd - d)< a ...

  9. UVA - 11212 Editing a Book(IDA*算法+状态空间搜索)

    题意:通过剪切粘贴操作,将n个自然段组成的文章,排列成1,2,……,n.剪贴板只有一个,问需要完成多少次剪切粘贴操作可以使文章自然段有序排列. 分析: 1.IDA*搜索:maxn是dfs的层数上限,若 ...

随机推荐

  1. 【VS开发】浅谈UDP(数据包长度,收包能力,丢包及进程结构选择)

    UDP数据包长度 UDP数据包的理论长度 udp数据包的理论长度是多少,合适的udp数据包应该是多少呢?从TCP-IP详解卷一第11章的udp数据包的包头可以看出,udp的最大包长度是2^16-1的个 ...

  2. 第六周&Java实验报告四(类的继承)

    一.实验目的 (1)掌握类的继承 (2)变量的继承和覆盖,方法的继承,重载和覆盖的实现: 二.实验的内容 (1)根据下面的要求实现圆类Circle. 1.圆类Circle的成员变量:radius表示圆 ...

  3. http请求跨域问题分析

    http请求跨域问题分析 个人认为可能涉及很多http的知识,本人才疏学浅不敢妄自揣测,只是做个笔记为了以后对比理解,从原生fetch说起吧,前提假设有个后端服务,我用express来模拟,如下: v ...

  4. C++对象在继承情况下的内存布局

    1,C++ 中继承是非常重要的一个特性,本节课研究在继承的情形下,C++ 的对象模 型又有什么不同: 2,继承对象模型(最简单的情况下): 1,在 C++ 编译器的内部类可以理解为结构体: 2,子类是 ...

  5. Python字符串和正则表达式中的反斜杠('\')问题

    在Python普通字符串中 在Python中,我们用'\'来转义某些普通字符,使其成为特殊字符,比如 In [1]: print('abc\ndef') # '\n'具有换行的作用 abc defg ...

  6. HNUST-1148 ACM ranking rules(简单模拟)

    1148: ACM ranking rules 时间限制: 1 Sec  内存限制: 128 MB提交: 16  解决: 12[提交][状态][讨论版] 题目描述 ACM contests, like ...

  7. 两张超级大表join优化

    一个简单的两表关联,SQL跑了差不多一天一夜,这两个表都非常巨大,每个表都有几十个G,数据量每个表有20多亿,表的字段也特别多. 相信大家也知道SQL慢在哪里了,单个进程的PGA 是绝对放不下几十个G ...

  8. RabbitMQ入门教程(十六):RabbitMQ与Spring集成

    原文:RabbitMQ入门教程(十六):RabbitMQ与Spring集成 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...

  9. 097、如何实现Service伸缩?(Swarm04)

    参考https://www.cnblogs.com/CloudMan6/p/7885667.html   上一节部署了只有一个副本的Service,不过对于web服务,我们通常会运行多个实例,这样可以 ...

  10. vue——echarts更换主题

    链接:https://blog.csdn.net/Sunshine0508/article/details/90067437 //等配置安装好了以后 在main.js里引入echarts主题的js,一 ...