【例 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方向上可以拨动,问你最少拨动几次可以是中心图案的数字一致 解题思路:回溯法,剪枝 其中要把每次拨动的字母所代表的位置提前用数组表示: 然后在如果 ...
随机推荐
- Django transaction 误用之后遇到的一个问题与解决方法
今天在调试项目开发好的一个模块的时候,发现了一个很诡异的现象,最后追踪发现是因为在项目中事务处理有误所致.这个问题坑了我好一会,所以记录一下,以免再踩坑.下面开始详述. 我们都知道 Django 框架 ...
- rev---将文件中的每行内容以字符为单位反序输出
rev命令将文件中的每行内容以字符为单位反序输出,即第一个字符最后输出,最后一个字符最先输出,依次类推.
- vue踩坑- 报错npm ERR! cb() never called!
在vue项目中引入饿了么elementUI组件的步骤之中,出现以下的错误: D:\my-project-first>npm i element-ui -S Unhandled rejection ...
- UVA 12493 Stars (欧拉函数--求1~n与n互质的个数)
pid=26358">https://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&cate ...
- 变量get、set设置
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Glide二次封装库的使用
更多代码可以查询本人GitHub:欢迎阅读,star点起来. Glide二次封装库源码 前言 为什么选择Glide? Glide 轻量级 速度快 可以根据所需加载图片的大小自动适配所需分辨率的图 支持 ...
- 玲珑学院 1050 - array
1050 - array Time Limit:3s Memory Limit:64MByte Submissions:494Solved:155 DESCRIPTION 2 array is an ...
- 用Vue+axios写一个实时搜索
刚刚在学vue,试着写了一个实时搜索文件. 思路:1.input 通过v-model绑定.2.通过watch检测输入结果变化.3根据结果变化从api调用不同的数据. 代码如下: <!DOCTYP ...
- 【2017 Multi-University Training Contest - Team 3】RXD and math
[Link]: [Description] [Solution] 发现1010mod(109+7)=999999937; 猜测答案是nk 写个快速幂; 注意对底数先取模; [NumberOf WA] ...
- Codeforces 441 B. Valera and Fruits
B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...