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 ...
随机推荐
- js调用js的方法
a.js文件调用b.js文件时,需要在a.js的第一行添加代码 document.write(" <script language=\"javascript\" s ...
- shell之echo and printf
#!/bin/sh _________echo___________#read name #echo "$name It is a test" #read命令从标准的输入中读取一行 ...
- finally在return之后还是之前运行
finally在运行前打印出来是return的数据,finally是最后修改的数据,如果finally存在对返回值的修改,则以finally修改的值为准. 综上所述,finally最后运行.
- 孤荷凌寒自学python第二十天python的匿名函数与偏函数
孤荷凌寒自学python第二十天python的匿名函数与偏函数 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python为使函数的使用更加方便高效,可以使用两种特殊的函数简化语句书写. 一 ...
- 计算机图形学 opengl版本 第三版------胡事民 第三章更多的绘图工具
opengl 计算机图形学 第三版 第二部分 第三章更多的绘图工具 3.1 概述 第2章中 我们绘图使用的是屏幕窗口的基础坐标系 以像素为单位 屏幕坐标从左下角x从0延伸到scr ...
- Ipython\Jupyter数据分析工具
使用Python进行数据分析优点 1 Python大量的库为数据分析和处理提供了完整的工具集 2 比起R和Matlab等其他主要用于数据分析的编程语言,Python更全能 3 Python库一直在增加 ...
- Spring 笔记(四)AOP
前言 横切关注点 使用 @AspectJ 定义切面. 同时还需要在配置类上应用 @EnableAspectJAutoProxy 注解,启用 AOP 自动代理.(不添加它的话,@AspectJ 注解的类 ...
- [译]如何将docker日志重定向到单个文件里
原文来源: how-to-redirect-docker-logs-to-a-single-file 问题: 我想把某一个docker的log全部导出到一个文件里进行分析,我该怎么做? 其实不用那样操 ...
- java值转递?引用传递?
值传递是传递的是原值的副本,引用传递传递的是原值. 在Java中,如果是基本数据类型,传递的是该参数字面量值的拷贝.如果是引用数据类型,传递的是该参数所引用对象在堆中地址的拷贝. swap(int a ...
- HDU - 5919 Sequence II
题意: 给定长度为n的序列和q次询问.每次询问给出一个区间(L,R),求出区间内每个数第一次出现位置的中位数,强制在线. 题解: 用主席树从右向左的插入点.对于当前点i,如果a[i]出现过,则把原位置 ...