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 ...
随机推荐
- android stadio 打开别人的工程 一直在编译中
这是因为,他工程的gradle 配置,在你本地找不到,所以,会去网上下.然后解压,使用.这是一个很漫长的过程. *那么怎么做呢 修改项目工程的gradle/wrapper/gradle-wrapper ...
- Android stadio 关联源码
有时候,你想在Android stadio 里看源码, 然后Android stadio 会提示你去下载. 但是下载完了之后,有时候stadio 还是不能看源码.后来,参考这位博客,搞完了. http ...
- LinkedHashMap和HashMap的比较使用 详解
由于现在项目中用到了LinkedHashMap,并不是太熟悉就到网上搜了一下. import java.util.HashMap; import java.util.Iterator; import ...
- 2.栅格的类中同时设置col-md-* col-sm-*的作用
1.一般设定成这样的话,在小屏幕上会堆叠在一起 <div class="row"> <div class="col-md-4 ">COL ...
- PyCharm 解决有些库(函数)没有代码提示
问题描述: 如图,当输入 im. 没有智能提示第三库相应的函数或其他提示. 解决方案: python是动态强类型语言,IDE无法判断Image.open("panda.png")的 ...
- JMeter学习笔记(七) 导出文件接口测试
导出文件接口,其实跟下载文件接口的测试类似,主要就是执行接口导出文件后保存到本地. 下载文件接口测试,参考文档:https://www.cnblogs.com/xiaoyu2018/p/1017830 ...
- python基础实践(二)
-*-越简单越快乐-*-# -*- coding:utf-8 -*-# Author:sweeping-monkQuestion_1 = "python中的整数运算"Method_ ...
- 孤荷凌寒自学python第四十八天通用同一数据库中复制数据表函数最终完成
孤荷凌寒自学python第四十八天通用同一数据库中复制数据表函数最终完成 (完整学习过程屏幕记录视频地址在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 今天经过反复折腾,最终基本上算 ...
- ironic的自动化脚本
# -*- coding:utf-8 -*- import json import subprocess import os import time import random trunk_start ...
- Python数据分析-Pandas(Series与DataFrame)
Pandas介绍: pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. Pandas的主要功能: 1)具备对其功能的数据结构DataFrame.Series 2)集成时间序 ...