【习题 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数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...
随机推荐
- SharePoint创建Alternate Access Mapping (AAM)备用訪问映射
SharePoint创建Alternate Access Mapping (AAM)备用訪问映射 SharePoint的仓库是SQL Server中的内容数据库.这些数据库储存着组织全 ...
- 有关c语言指针的总结
#include<stdio.h> void main() { int a[3]={1,3,5};//一维数组 int *num[3]={&a[0],&a[1],& ...
- rgba
正反两面展示效果 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
- 对Jscript操作注册表接口的一点不解
作者:朱金灿 来源:http://blog.csdn.net/clever101 要操作注册表需要通过ActiveX控件调用WScript.shell对象,通过该对象的一些方法来操作.Wshshell ...
- 利用Python网络爬虫抓取微信好友的签名及其可视化展示
前几天给大家分享了如何利用Python词云和wordart可视化工具对朋友圈数据进行可视化,利用Python网络爬虫抓取微信好友数量以及微信好友的男女比例,以及利用Python网络爬虫抓取微信好友的所 ...
- selenium+python 安装使用
一.序言 selenium官网selenium简单教程selenium完整教程python基础教程 二.安装python3 https://www.python.org/downloads/relea ...
- PHP获取一周后的时间戳
echo strtotime("now");//相当于将英文单词now直接等于现在的日期和时间,并把这个日期时间转化为unix时间戳.这个效果跟echo time();一样. ec ...
- AJAX 前后端交互 验证信息是否正确
1.前段: function checkPtCode(obj){ $.ajax({ type: "post", url: "xxxxxxx", data: {& ...
- PHP和js判断访问终端是否是微信浏览器
http://www.sucaihuo.com/php/813.html http://www.thinkphp.cn/extend/767.html http://blog.csdn.net/gf7 ...
- STL heap部分源代码分析
本文假设你已对堆排序的算法有主要的了解. 要分析stl中heap的源代码的独到之处.最好的办法就是拿普通的代码进行比較.话不多说,先看一段普通的堆排序的代码: //调整大顶堆.使得结构合理 void ...