【例 7-12 UVA - 1343】The Rotation Game
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
迭代加深搜索。
每次抽动操作最多只会让中间那一块的区域离目标的“距离”减少1.
以这个作为剪枝。
枚举最大深度。
就能过了。
【代码】
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 10;
int b[N][N] = { { 1, 3, 7, 12, 16, 21, 23 }, { 2, 4, 9, 13, 18, 22, 24 }, { 11, 10, 9, 8, 7, 6, 5 }, { 20, 19, 18, 17, 16, 15, 14 } };
int fan[N];
int a[30];
int c[N + 5] = { 7, 8, 9, 12, 13, 16, 17, 18 };
int maxdep, ans;
vector <int> v, vans;
void init(){
for (int i = 0; i < 4; i++){
int temp;
if (i & 1) temp = 3; else temp = 5;
int j = i + temp;
fan[i] = j;
fan[j] = i;
}
for (int i = 4; i < 8; i++){
int pre = fan[i];
int now = 0;
for (int j = 6; j >= 0; j--){
b[i][now] = b[pre][j];
now++;
}
}
}
int ok(){
int mi = 8;
for (int i = 1; i <= 3; i++){
int cnt = 0;
for (int j = 0; j < 8; j++) if (a[c[j]] != i) cnt++;
mi = min(mi, cnt);
}
return mi;
}
void Move(int idx){
int temp = a[b[idx][0]];
for (int i = 0; i < 6; i++){
int x = b[idx][i], y = b[idx][i + 1];
a[x] = a[y];
}
a[b[idx][6]] = temp;
}
void dfs(int dep){
if (ans != 0) return;
int now = ok();
if (dep == maxdep){
if (now == 0){
vans = v;
ans = a[c[0]];
}
return;
}
int rest = maxdep - dep;
if (rest < now) return;
for (int i = 0; i < 8; i++){
v.push_back(i);
Move(i);
dfs(dep + 1);
v.pop_back();
Move(fan[i]);
}
}
int main()
{
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0), cin.tie(0);
init();
while (cin >> a[1] && a[1]){
for (int i = 2; i <= 24; i++) cin >> a[i];
if (ok() == 0){
cout << "No moves needed" << endl;
cout << a[c[0]] << endl;
continue;
}
ans = 0;
for (maxdep = 1;; maxdep++){
dfs(0);
if (ans != 0) break;
}
for (int i = 0; i<(int)vans.size(); i++){
cout << (char)(vans[i] + 'A');
}
cout << endl;
cout << ans << endl;
}
return 0;
}
【例 7-12 UVA - 1343】The Rotation Game的更多相关文章
- UVa 1343 The Rotation Game (状态空间搜索 && IDA*)
题意:有个#字型的棋盘,2行2列,一共24个格. 如图:每个格子是1或2或3,一共8个1,8个2,8个3. 有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面. 求:使 ...
- UVA 1343 The Rotation Game
题意: 给出图,往A-H方向旋转,使中间8个格子数字相同.要求旋转次数最少,操作序列字典序尽量小. 分析: 用一维数组存24个方格.二维数组代表每个方向对应的7个方格.IDA*剪枝是当8-8个方格中重 ...
- UVA - 1343 The Rotation Game (BFS/IDA*)
题目链接 紫书例题. 首先附上我第一次bfs+剪枝TLE的版本: #include<bits/stdc++.h> using namespace std; typedef long lon ...
- UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...
- 【UVa】1343 The Rotation Game(IDA*)
题目 题目 分析 lrj代码.... 还有is_final是保留字,害的我CE了好几发. 代码 #include <cstdio> #include <algorit ...
- UVA_Rotation Game<旋转游戏> UVA 1343
The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The ...
- UVA 1379 - Pitcher Rotation(DP + 贪心)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4125" rel="nofo ...
- 数据库系统概论(第5版) P262 例8.12
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <Windows.h& ...
- uva 1343 非原创
uva1343 原作者 题目题意是:给你的棋盘,在A-H方向上可以拨动,问你最少拨动几次可以是中心图案的数字一致 解题思路:回溯法,剪枝 其中要把每次拨动的字母所代表的位置提前用数组表示: 然后在如果 ...
随机推荐
- 【Henu ACM Round#16 A】 Bear and Game
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 看看什么时候t[i]-t[i-1]>15. 输出t[i-1]+15就好. 不存在这样的i就输出min(t[n]+15,90) ...
- cocos2dx 3.0正式版 在mac上新建项目
触碰科技确定2.x版本号不会再更新了,会一直维护3.x的版本号.于是赶紧看看3.0的,简单浏览一下.类的使用方法和原来的几乎相同,仅仅是 表达的写法变了下,. . . . 以后肯定有非常多变化,速度熟 ...
- (诡异Floyd&自环)MZ Training 2014 #15 E题(POJ 2240)
你们见过这么诡异的FLOYD吗? 先上题. [Description] 货币的汇率存在差异.比如,如果1美元购买0.5英镑,1英镑买10法郎.而1法国法郎买0.21美元.然后,通过转换货币,一个聪明的 ...
- php中str_repeat函数
php中str_repeat函数 一.作用 用于repeat str 二.实例:输出菱形 代码: <!DOCTYPE html> <html lang="en"& ...
- es7 --- 新特性
ES7只有2个特性: includes() 指数操作符 不使用ES7 使用indexOf()验证数组中是否存在某个元素,这时需要根据返回值是否为-1来判断: let arr = ['react', ' ...
- 10.cocos2d坐标系
一.笛卡儿坐标系 OpenGl坐标系为笛卡儿右手系.x向右,y向上,z向外.在cocos2d-lua中坐标系原点在屏幕的左下角,x向右,y向上,z则是指的zorder(层级). 二.世界坐标系,本地坐 ...
- jQuery简单tab按钮切换
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Kinect 开发 —— 图片浏览
总体思路 首先运用WPF编写一个简单的支持多点触控的图片浏览程序,这方面您可以参看MSDN上的这篇文章,上面有代码,可能需要FQ才能下载.中文的话,您可以参考Gnie同学关于在WPF上面多点触屏(Mu ...
- 51Nod 圆与三角形
给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是否相交.相交输出"Yes",否则输出"No".(三角形的面积大于0). Input 第1行:一个数T ...
- 三分钟上手Highcharts简易甘特图
根据业务需求,找到了这个很少使用的图形,话不多说,看看该如何使用.首先要引入支持文件:可根据链接下载. exporting.js:https://img.hcharts.cn/highcharts/m ...