题意:

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)的更多相关文章

  1. UVA 1594:Ducci Sequence (模拟 Grade E)

    题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include < ...

  2. UVA 1593: Alignment of Code(模拟 Grade D)

    题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdli ...

  3. 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 ...

  4. UVa 439骑士的移动(BFS)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. [Uva 10085] The most distant state (BFS)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. UVA 10714 Ants 蚂蚁 贪心+模拟 水题

    题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...

  7. hdu 5012 模拟+bfs

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 模拟出骰子四种反转方式,bfs,最多不会走超过6步 #include <cstdio> #in ...

  8. UVA-1604 Cubic Eight-Puzzle (双向BFS+状态压缩+限制搜索层数)

    题目大意:立体的八数码问题,一次操作是滚动一次方块,问从初始状态到目标状态的最少滚动次数. 题目分析:这道题已知初始状态和目标状态,且又状态数目庞大,适宜用双向BFS.每个小方块有6种状态,整个大方格 ...

  9. 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 ...

随机推荐

  1. java练习题——数组

    上述代码可以顺利通过编译,并且输出一个“很奇怪”的结果:[Ljava.lang.Object;@2a139a55 为什么会出现这种情况? 直接输出object的对象,系统会输出地址,如果想要输出其中的 ...

  2. 让菜鸡讲一讲费用流(EK)

    让我再讲一个故事吧. 又有一些小精灵要准备从银月城(S)迁徙到Nibel山(T). 这两个地方之间的道路构成了一个网络. 每个道路都有它自己的容量,这决定了每天有多少小精灵可以同时从这儿通过. 和上一 ...

  3. Eclipse 工作空间(Workspace)---Eclipse教程第07课

    Eclipse 工作空间(Workspace) eclipse 工作空间包含以下资源: 项目 文件 文件夹 项目启动时一般可以设置工作空间,你可以将其设置为默认工作空间,下次启动后无需再配置: 工作空 ...

  4. MyEclipse - 问题集 - maven update project 后,项目jdk的版本变化

    解决方法: 进入maven安装根目录,conf/settings.xml <profiles> <profile> <id>jdk-1.7</id> & ...

  5. 《Cracking the Coding Interview》——第8章:面向对象设计——题目1

    2014-04-23 17:32 题目:请设计一个数据结构来模拟一副牌,你要如何用这副牌玩21点呢? 解法:说实话,扑克牌的花样在于各种花色.顺子.连对.三带一.炸弹等等,如果能设计一个数据结构,让判 ...

  6. python实现单链表的反转

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/usr/bin/env python #coding = utf-8 ...

  7. 【非原创】tomcat 安装时出现 Failed to install Tomcat7 service

    tomcat 安装时出现 Failed to install Tomcat7 service 今天在安装tomcat时提示 Failed to install Tomcat7 service了,花了大 ...

  8. Python学习-day17 jQuery method and demo

    一:介绍: jQuery:是DOM和js的封装.jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).现在大多数的pc端的网站都 ...

  9. express常用代码片段

    请求模块: var express = require('express'); var router = express.Router(); // 拿到express框架的路由 var mongoos ...

  10. Spring2集成iBatis2

    从数据库中查询一条记录,演示Spring与iBatis的集成 1 编写sqlmaps与Domain对象 <?xml version="1.0" encoding=" ...