UVA 1604:Cubic Eight-Puzzle(模拟,BFS Grade C)
题意:
3*3方格,有一个是空的。其他的每个格子里有一个立方体。立方体最初上下白色,前后红色,左右蓝色。移动的方式为滚。给出初态空的位置,终态上面颜色情况,问最少多少步能到达。如果超过30步不能到达,-1。
思路:
模拟。
另外再加了一个A*优化。就是估计一下。应该还能优化的。感觉像二进制上可以优化。实在不想写了。
代码:
//16:50
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
using namespace std; int heng[] = {,,,,,};
int shu[] = {,,,,,}; int go[][] = {{,},{,},{-,},{,-}}; #define N 4
int ch2num[]; int que[];
char step[]; int encodeState(int graph[N][N]) {
int res = ;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
res = (res<<) + graph[i][j];
}
}
return res;
} void decodeState(int state, int graph[N][N]) {
for (int i = ; i >= ; i--) {
for (int j = ; j >= ; j--) {
graph[i][j] = state&;
state>>=;
}
}
} int initState(int x, int y) {
int g[N][N];
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
g[i][j] = ;
}
}
g[x][y] = ;
return encodeState(g);
} int graph[N][N];
int endgraph[N][N]; int h(int state) {
int res = ;
for (int i = ; i >= ; i--) {
for (int j = ; j >= ; j--) {
if ( ((state>>)&) != endgraph[i][j] ) res++;
state >>= ;
}
}
return res-;
} int endState;
#define CLEAR_EVERY_3(A) ((A)&0x36db6db)
#define ISEND(S) (CLEAR_EVERY_3((S)>>1) == endState)
void initEnd() {
endState = ;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
endState = (endState<<)+endgraph[i][j];
}
}
} int main() {
ch2num['W'] = ;
ch2num['B'] = ;
ch2num['R'] = ;
ch2num['E'] = ; int x, y;
while (scanf("%d%d", &x, &y) != EOF) {
if (x == && y == ) break;
x--;y--;
swap(x,y);
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
char tmp[];
scanf("%s", tmp);
endgraph[i][j] = ch2num[tmp[]];
}
}
initEnd(); bool finded = false;
int ans = ; int state = initState(x,y);
do{
if (ISEND(state)) {
finded = true;
ans = ;
break;
} int hd = , tl = ;
que[tl++] = state; memset(step,,sizeof(step));
step[state] = ; while (hd < tl) {
int now = que[hd++];
if (step[now]- >= ) break; if (h(now)+step[now]- > ) continue; decodeState(now, graph); int si=-, sj;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
if (graph[i][j] == ) {
si = i;
sj = j;
break;
}
}
if (si != -) break;
}
if (si == -) puts("error"); for (int i = ; i < ; i++) {
int nx = si + go[i][];
int ny = sj + go[i][]; if (!( <= nx && nx < && <= ny && ny < )) continue; swap(graph[nx][ny], graph[si][sj]);
if (go[i][]) {
graph[si][sj] = shu[graph[si][sj]];
} else {
graph[si][sj] = heng[graph[si][sj]];
} int nstate = encodeState(graph);
if (ISEND(nstate)) { finded = true; ans = step[now]; break; } if (go[i][]) {
graph[si][sj] = shu[graph[si][sj]];
} else {
graph[si][sj] = heng[graph[si][sj]];
}
swap(graph[nx][ny], graph[si][sj]); if (step[nstate] != ) continue;
step[nstate] = step[now]+;
que[tl++] = nstate;
}
if (finded) break;
}
}while();
if (finded) printf("%d\n", ans);
else printf("-1\n");
}
return ;
}
UVA 1604:Cubic Eight-Puzzle(模拟,BFS Grade C)的更多相关文章
- UVA 1594:Ducci Sequence (模拟 Grade E)
题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include < ...
- UVA 1593: Alignment of Code(模拟 Grade D)
题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdli ...
- 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 ...
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [Uva 10085] The most distant state (BFS)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 10714 Ants 蚂蚁 贪心+模拟 水题
题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...
- hdu 5012 模拟+bfs
http://acm.hdu.edu.cn/showproblem.php?pid=5012 模拟出骰子四种反转方式,bfs,最多不会走超过6步 #include <cstdio> #in ...
- UVA-1604 Cubic Eight-Puzzle (双向BFS+状态压缩+限制搜索层数)
题目大意:立体的八数码问题,一次操作是滚动一次方块,问从初始状态到目标状态的最少滚动次数. 题目分析:这道题已知初始状态和目标状态,且又状态数目庞大,适宜用双向BFS.每个小方块有6种状态,整个大方格 ...
- Codeforces Round #301 (Div. 2)A B C D 水 模拟 bfs 概率dp
A. Combination Lock time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- SpringMVC + MyBatis简单示例
该项目基于Maven开发,该项目中包含了MyBatis自动创建表的功能,具体实现查阅MyBatis---自动创建表 源码下载 配置 maven支持pom.xml <project xmlns=& ...
- js模板引擎之 Handlebars 基本用法
模板引擎比较久远的一个技术,常见的模板引擎有 baiduTemplate(百度)\artTemplate(腾讯)\juicer(淘宝)\doT\ tmpl\ handlebars\ easyTempl ...
- 《数据结构与算法分析:C语言描述》复习——第五章“堆”——二叉堆
2014.06.15 22:14 简介: 堆是一种非常实用的数据结构,其中以二叉堆最为常用.二叉堆可以看作一棵完全二叉树,每个节点的键值都大于(小于)其子节点,但左右孩子之间不需要有序.我们关心的通常 ...
- Mysql与Oracle之间的数据类型转换
MySQL Data Type Oracle Data Type BIGINT NUMBER(19, 0) BIT RAW BLOB BLOB, RAW CHAR CHAR DATE DATE DAT ...
- python 学习分享-实战篇高级的ftp
#server代码 import socketserver,os,hashlib Base_paht = os.path.dirname(os.path.dirname(os.path.abspath ...
- (转\整)UE4游戏优化 多人大地型游戏的优化(一)游戏线程的优化
施主分享随缘,评论随心,@author:白袍小道 小道暗语: 1.因为小道这里博客目录没自己整,暂时就用随笔目录结构,所以二级目录那啥就忽略了.标题格式大致都是(原or转) 二级目录 (标题) 2.因 ...
- 斐波那契数列的三种C++实现及时间复杂度分析
本文介绍了斐波那契数列的三种C++实现并详细地分析了时间复杂度. 斐波那契数列定义:F(1)=1, F(2)=1, F(n)=F(n-1) + F(n-2) (n>2) 如何计算斐波那契数 F( ...
- shell之基本语法
转: read 命令从 stdin 获取输入并赋值给 PERSON 变量,最后在 stdout 上输出: #!/bin/bash # Script follows here: echo " ...
- windows mobile 开发:让GPS一直在待机模式下也能运行
最近,遇到一个需求,就是每 30 秒更新一次 GPS 位置,在测试过程中,发现在系统待机后,更新 GPS 位置就不能正常运行了,搜索后,发现如下的解决方案,实际应用了之后,有效,赞!!! http:/ ...
- 写把proto函数搞清楚
在做blk层之前,先把proto搞清楚 ffi_lua metatype可以给函数加方法, lua中冒号是啥意思?冒号会传入self,但是点号不会传入self