【习题 7-9 UVA-1604】Cubic Eight-Puzzle
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
IDA*
保证这次移动的方格不和前一次重复。
然后加一个8数码的剪枝就行了。
->看看当前状态和目标状态有多少个地方是不一样的。
如果当前的步数加上它仍然比答案大。
显然可以剪枝。
因为不同的数目肯定小于等于要移动的数目;
(每次移动最多只能让一个方格还原)
(然后只要记录上面和前面的颜色就好了
剩下一个面的颜色能够通过其他两种颜色推出来。
对面的颜色是相等的。
(这个算法(剪枝)的效率真的超级高。。。
【代码】
#include <bits/stdc++.h>
#define ll long long
using namespace std;
//上 前
//0 1
//0 1 2 3
//E B W R
const int dx[4] = {-1,1,0,0};
const int dy[4] = {0,0,1,-1};
pair<int,int> a[5][5];
int goal[5][5],ans;
int other(int x,int y){
return 6-(x+y);
}
void rotate_left_right(pair<int,int> &x){
pair<ll,ll> temp = x;
x.first = other(temp.first,temp.second);
x.second = temp.second;
}
void rotate_up_down(pair<int,int> &x){
pair<ll,ll> temp = x;
x.first = temp.second;
x.second = temp.first;
}
int gujia(){
int cnt = 0;
for (int i = 1;i <= 3;i++)
for (int j = 1;j <= 3;j++)
if (a[i][j].first!=goal[i][j])
cnt++;
return cnt;
}
void dfs(int dep,int prex,int prey){
int differ = gujia();
if (differ==0){
if (dep < ans){
ans = dep;
return;
}
return;
}
if (dep + differ>ans) return;
int x,y;
for (int i = 1;i <= 3;i++)
for (int j = 1;j <= 3;j++)
if (a[i][j].first==0){
x = i,y = j;
}
for (int i = 0;i < 4;i++){
int tx = x + dx[i],ty = y + dy[i];
if (tx >=1 && tx<=3 && ty>=1 && ty <= 3 &&!(tx==prex && ty==prey)) {
pair<int,int> temp = a[tx][ty];
if (i>1)
rotate_left_right(a[tx][ty]);
else
rotate_up_down(a[tx][ty]);
swap(a[tx][ty],a[x][y]);
dfs(dep+1,x,y);
a[tx][ty] = temp;
a[x][y] = {0,0};
}
}
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int x,y;
while (cin >> x >> y){
swap(x,y);
if (x==0 && y==0) break;
for (int i = 1;i<=3;i++)
for (int j = 1;j<= 3;j++){
if (i==x && y==j)
a[i][j] = {0,0};
else
a[i][j] = {2,3};
}
for (int i = 1;i <= 3;i++)
for (int j = 1;j <= 3;j++){
char s[3];
cin >> s;
goal[i][j] = 0;
switch(s[0]){
case 'B':goal[i][j] = 1;break;
case 'W':goal[i][j] = 2;break;
case 'R':goal[i][j] = 3;break;
}
}
ans = 31;
dfs(0,-1,-1);
if (ans>=31){
cout << -1 << endl;
}else{
cout << ans << endl;
}
}
return 0;
}
【习题 7-9 UVA-1604】Cubic Eight-Puzzle的更多相关文章
- UVA 1604:Cubic Eight-Puzzle(模拟,BFS Grade C)
题意: 3*3方格,有一个是空的.其他的每个格子里有一个立方体.立方体最初上下白色,前后红色,左右蓝色.移动的方式为滚.给出初态空的位置,终态上面颜色情况,问最少多少步能到达.如果超过30步不能到达, ...
- UVA_Cubic Eight-Puzzle UVA 1604
Let's play a puzzle using eight cubes placed on a 3 x 3 board leaving one empty square.Faces of cube ...
- ACM训练计划建议(写给本校acmer,欢迎围观和指正)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
- ACM训练计划建议(转)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
- 动态规划 Dynamic Programming 学习笔记
文章以 CC-BY-SA 方式共享,此说明高于本站内其他说明. 本文尚未完工,但内容足够丰富,故提前发布. 内容包含大量 \(\LaTeX\) 公式,渲染可能需要一些时间,请耐心等待渲染(约 5s). ...
- UVa第五章STL应用 习题((解题报告))具体!
例题5--9 数据库 Database UVa 1592 #include<iostream> #include<stdio.h> #include<string.h&g ...
- uva 227 Puzzle
Puzzle A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...
- UVA 227 Puzzle - 输入输出
题目: acm.hust.edu.cn/vjudge/roblem/viewProblem.action?id=19191 这道题本身难度不大,但输入输出时需要特别小心,一不留神就会出问题. 对于输入 ...
- UVA_Digit Puzzle UVA 12107
If you hide some digits in an integer equation, you create a digit puzzle. The figure below shows tw ...
- UVa 1600 Patrol Robot (习题 6-5)
传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...
随机推荐
- C语言keywordstatic的绝妙用途
为什么要说static妙,它确实是妙,在软件开发或者单片机开发过程中,大家总以为static就是一个静态变量.在变量类型的前面加上就自己主动清0了.还有就是加上statickeyword的,无论是变量 ...
- Runtime类中的freeMemory,totalMemory,maxMemory等几个方法
最近在网上看到一些人讨论到Java.lang.Runtime类中的freeMemory(),totalMemory(),maxMemory ()这几个方法的一些题目,很多人感到很迷惑,为什么,在jav ...
- 79.express里面的app.configure作用
以下摘自 express 3.0 的 文档 app.configure([env], callback) Conditionally invoke callback when env matches ...
- 今天了解了些redis和memcached的知识
提取于http://www.cnblogs.com/wupeiqi/articles/5132791.html 感谢博主 使用Redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于Hash ...
- OPENCV(4) —— ImgProc
2D图像滤波器基础类BaseFilter:dst(x,y) = F(src(x,y), src(x+1,y)... src(x+wdith-1,y), src(y+1,x)... src(x+widt ...
- android对话框显示异常报错:You need to use a Theme.AppCompat theme (or descendant) with this activity.
今天写android实验碰到到一个问题,在用AlertDialog.Builder类构建一个对话框之后,调用Builder.show()方法时抛出异常如下: - ::-/xyz.qlrr.sqlite ...
- IDEA 开发工具在POM.XML文件中增加依赖
在POM.XML 中使用快捷键 ALT+INSERT 选择第一个,输入关键字即可 选择版本,确认,ok
- 免费css布局和模板集合
Internet 上有很多基于 (X)HTML/CSS 标记的模板.如果你是一个 Web 开发人员,你不希望把时间一次又一次地浪费在重复代码设计上面,这里提供了一个列表,提供了基于 CSS 的免费模板 ...
- 洛谷 P2355 团体操队形
P2355 团体操队形 题目背景 X中学要团体操比赛了哦.队形该怎样排呢? 题目描述 有n(n<=100000)个团体操队员编号分别为1~n,参加运动会开幕式的团体操表演.其基本队形(分连续队形 ...
- Fastboot线刷“复活”之刷机心得(三)——错误处理
在刷机的过程中可能不会是一帆风顺的.至少我是这种,总是会遇到这样或者那样的问题,下面是我为大家总结一些问题和解决办法,希望能对大家有所帮助. 一.电量问题 刷机和系统更新有一个共同的前 ...