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 ...
随机推荐
- <s:submit> 指定的method方法不执行
很多文章在讲一个表单多个提交方法的时候都是在<s:submit>中通过method来指定,但是我在试验中怎么也不对,jsp页面代码如下 <%@page import="or ...
- IAR FOR STM8S 错误 An error occurred while retrieving GDI features: gdi-error [40201]解决方法
今早使用IAR调试编译调试一个工程,发现IAR竟然出现如下错误信息 An error occurred ]: Can't access configuration database 在网上查看了一下, ...
- android 事件传递机制
有三个方法: dispatchTouchEvent onInterceptTouchEvent onTouchEvent 首先:A的dispatchTouchEvent-A的onInterceptTo ...
- java 中实体Bean和Map互相转化
技术交流群: 233513714 // 将一个map对象转化为bean public static void transMap2Bean(Map<String, Object> map, ...
- javaWEB简单商城项目
javaWEB简单商城项目(一) 项目中使用到了上一篇博文的分页框架,还有mybatis,重点是学习mybatis.现在有些小迷茫,不知道该干啥,唉,不想那么多了,学就对了 一.项目功能结构 1.功能 ...
- PJSIP-PJMEDIA【使用pjmedia 播放wav格式的音乐】
应宝哥建议以及更好的交流学习,这篇开始使用中文,英语就先放一放吧! 要使用PJSIP中的PJMEDIA首先我们需要搭建好它所需要的环境. [环境搭建与调试] 1 在 工具 加入pjmedia所需要的包 ...
- 剑指Offer - 九度1368 - 二叉树中和为某一值的路径
剑指Offer - 九度1368 - 二叉树中和为某一值的路径2013-11-23 03:46 题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结 ...
- Pascal小游戏 不要消灭星星
不要消灭星星 Pascal小游戏 Chaobs改编自pascal吧 控制台小游戏嘛,就当是练习一下结构化的写法. program wxtw; uses crt; type zbdy=reco ...
- win 7 查看端口被占用
开始---->运行---->cmd,或者是window+R组合键,调出命令窗口 输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是4915 ...
- ssm项目中ueditor富文本编辑器的使用
一.下载 https://ueditor.baidu.com/website/index.html 将ueditor放到项目中合适的位置 二 . 配置文件上传路径 在utf8-jsp/jsp/conf ...