【习题 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数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...
随机推荐
- Java接口源码--System和应用程序进程间通信
本文參考<Android系统源代码情景分析>.作者罗升阳 一.架构代码: ~/Android/frameworks/base/core/java/android/os ----IInter ...
- ddr sdram self-refresh & auto-refresh
以下是EDD5116AFTA数据手册的摘录.不过看过了还是不太明白二者的区别. self-refresh:Self-refresh entry [SELF]This command starts se ...
- 21.MFC进制转换工具
相关代码:链接:https://pan.baidu.com/s/1pKVVUZL 密码:e3vf #include <stdlib.h> #include <stdio.h> ...
- netflix turbine概述
1.turbine是什么?它的作用是什么? Turbine is a tool for aggregating streams of Server-Sent Event (SSE) JSON data ...
- Vuejs2.0构建一个彩票查询WebAPP(3)
整个工程的目录及截图如下,源码下载 使用心得: 1.了解Vue的生命周期很有必要,详情参见博文Vue2.0 探索之路——生命周期和钩子函数的一些理解 2.Vuex全局状态管理真是美味不可言 st ...
- 实例讲解Nginx下的rewrite规则 来源:Linux社区
一.正则表达式匹配,其中:* ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文件* ...
- Unity容器实现自动注册
如何创建Unity容器? 首先NuGet搜索Unity, 该示例中使用的版本为4.0.1 新建控制台程序 示例中使用常规操作, 创建一个IPay接口, 分别有两个实现类: ApplePay.Huawe ...
- RvmTranslator6.0 - AVEVA Marine Hull Model
eryar@163.com 1. Introduction RvmTranslator can translate the RVM file exported by AVEVA Plant(PDMS) ...
- vim 基础学习之重复
重复命令 .: 这个命令可以重复之前的操作.例如你执行了dd操作,然后. 就会删除当前行还有从进入插入模式到退出插入模式,之间的修改也算是一次操作.比如,你执行了i aaa <Esc>然后 ...
- android 图片特效处理之锐化效果
这篇将讲到图片特效处理的锐化效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理: 一.简单算法:分别获取当前像素点和八个周围像素点的RGB值,先求出当前像素点的RGB值与八个像素点RGB值的 ...