【例 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方向上可以拨动,问你最少拨动几次可以是中心图案的数字一致 解题思路:回溯法,剪枝 其中要把每次拨动的字母所代表的位置提前用数组表示: 然后在如果 ...
随机推荐
- C/C++(C++类型增强)
C++类型增强 类型检查更严格 把一个const类型的指针赋给非const类型的指针.c语言中可以通的过,但是在c++中则编不过去 const int a = 10; a = 100;//const修 ...
- 【搭建Saltstack运维工具】
目录 所谓Salt 开始搭建 配置接受密钥 salt命令 YAML详解 目标定位字符串 state模块定义主机状态 Salt采集静态信息之GrainsSalt @(Saltstack) *** 所谓S ...
- 直接修改Android软件数据库来改变软件设置实例一则
昨天把K860i刷了机,刷到了最新的CyanogenMod 10.1,使用起来各种流畅舒服,不过却由于内外置SD卡的挂载点的改变,造成了一些困扰,现记录如下. 平时里由于极少把手机连接 ...
- iOS基本UI控件总结
包括以下几类: //继承自NSObject:(暂列为控件) UIColor *_color; //颜色 UIImage *_image; //图像 //继承自UIView:只能相应手势UI ...
- windows下plsql 设置 里面timestamp显示的格式
http://blog.csdn.net/cwjcsu/article/details/9216981
- C# Arcgis Engine 获得鼠标按下位置的要素
public IFeature GetFeatureOnMouseDown(IPoint point) { try { ILayer layer = Common.GetLayerByName(mMa ...
- Python学习笔记3:简单文件操作
# -*- coding: cp936 -*- # 1 打开文件 # open(fileName, mode) # 參数:fileName文件名称 # mode打开方式 # w 以写方式打开. ...
- js插件---画图软件wePaint如何使用(插入背景图片,保存图片,上传图片)
js插件---画图软件wePaint如何使用(插入背景图片,保存图片,上传图片) 一.总结 一句话总结:万能的wPaint方法,通过不同的参数执行不同的操作.比如清空画布参数传"clear& ...
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disa
转自:https://blog.csdn.net/ouyangtianhan/article/details/6797999 Unable to find required classes (java ...
- LuoguP4016 负载平衡问题(费用流)
题目描述 G 公司有 n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入输出格式 输入格式: ...