算法指南白书

维护一个四维数组,走一步更新一步

 #include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int INF = ;
const int maxr = + ;
const int maxc = + ;
int R, C, sr, sc, tr, tc;
char maze[maxr][maxc]; struct State {
int r, c, dir, color;
State(int r, int c, int dir, int color):r(r),c(c),dir(dir),color(color) {}
}; const int dr[] = {-,,,}; // north, west, south, east
const int dc[] = {,-,,};
int d[maxr][maxc][][], vis[maxr][maxc][][];//横坐标,纵坐标,方向,颜色 int ans;
queue<State> Q; void update(int r, int c, int dir, int color, int v) {
if(r < || r >= R || c < || c >= C) return; // 不能走出边界
if(maze[r][c] == '.' && !vis[r][c][dir][color]) {
Q.push(State(r, c, dir, color));
vis[r][c][dir][color] = ;
d[r][c][dir][color] = v;
if(r == tr && c == tc && color == ) ans = min(ans, v); // 更新答案
}
} void bfs(State st) {
d[st.r][st.c][st.dir][st.color] = ;
vis[st.r][st.c][st.dir][st.color] = ;
Q.push(st);
while(!Q.empty()) {
st = Q.front();
Q.pop();
int v = d[st.r][st.c][st.dir][st.color] + ;
update(st.r, st.c, (st.dir+)%, st.color, v); // 左转
update(st.r, st.c, (st.dir+)%, st.color, v); // 右转
update(st.r+dr[st.dir], st.c+dc[st.dir], st.dir, (st.color+)%, v); // 前进
}
} int main() {
int kase = ;
while(scanf("%d%d", &R, &C) == && R && C) {
for(int i = ; i < R; i++) {
scanf("%s", maze[i]);
for(int j = ; j < C; j++)
if(maze[i][j] == 'S') {
sr = i;
sc = j;
} else if(maze[i][j] == 'T') {
tr = i;
tc = j;
}
}
maze[sr][sc] = maze[tr][tc] = '.';
ans = INF;
memset(vis, , sizeof(vis));
bfs(State(sr, sc, , )); if(kase > ) printf("\n");
printf("Case #%d\n", ++kase);
if(ans == INF) printf("destination not reachable\n");
else printf("minimum time = %d sec\n", ans);
}
}

uva 10047 the monocyle (四维bfs)的更多相关文章

  1. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  2. UVA 816 -- Abbott's Revenge(BFS求最短路)

     UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...

  3. UVA 10047 - The Monocycle BFS

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

  4. UVa 10047,独轮车

    题目链接:https://uva.onlinejudge.org/external/100/10047.pdf 题目链接:http://vjudge.net/contest/132239#proble ...

  5. UVa 11624,两次BFS

    题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...

  6. uva 11234 Expressions 表达式 建树+BFS层次遍历

    题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include < ...

  7. UVA 10047 The Monocycle

    大白图论第二题··· 题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色. 在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操 ...

  8. UVA 810 - A Dicey Problem(BFS)

    UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...

  9. UVA 11573 - Ocean Currents(BFS+优先队列)

    UVA 11573 - Ocean Currents 题目链接 题意:给定一个海面.数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量,每次询问给一个起点一个终点,问起点到终点耗费的最小能量 ...

随机推荐

  1. (转)iOS学习之 plist文件的读写

    在做iOS开发时,经常用到到plist文件, 那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ...

  2. leetcode problem 31 -- Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  3. Wap站总结一

    前段时间负责了公司的wap站前端工作,目前wap站的基础及较为复杂的几张页面都已经出来,现根据自己的一些经验,贴出部分心得,希望对现在或者以后可能会接触到Wap站的一些人有些帮助 一.本次WAP网站的 ...

  4. js 中如何通过提示框跳转页面

    通过提示框跳转页面 <!doctype html> <html lang="en"> <head> <meta charset=" ...

  5. 使用Yii框架中遇到的三个问题

    以下由我们在信易网络公司开发项目的时候终结出的一些经验 使用Yii框架中遇到的三个问题 1.main.php文件中欲引入全局变量的问题 还原一下此问题:在Yii框架中,main.php一般会作为整个应 ...

  6. Ubuntu 13.10 PHP 5.5.x mcrypt missing – Fatal Error: Undefined function mcrypt_encrypt()!

    [原文]http://www.tuicool.com/articles/goto?id=myM7veR I had updgraded my Ubuntu from 13.04 to 13.10 la ...

  7. 阿里云主机建立SWAP分区脚本

    工具:add_swap.sh    所有执行的脚本都需要root身份来执行,执行方法:以root身执行命令:bash xxx.sh 功能:自动检测系统swap分区大小,交换分区大小不合理则自动新增并挂 ...

  8. WPF窗体置于桌面最底层

    在WPF中设置窗体的Topmost属性可以将窗体永远置于顶部,但是没有提供Bottommost属性将窗体置底.若果要将窗体置于桌面的最底部,就需要使用Windows API来实现了.解决方案如下: 1 ...

  9. App - 版本控制

    /**  版本判断 ***/ NSString *versionKey = @"CFBundleVersion"; // 上一次使用版本号(存储在沙盒中的版本号) NSString ...

  10. 预处理命令#define #undef #if #endif 的基本用法

    C#的预处理命令其实还是蛮有用的,但是真正使用过得人不多,这个介绍一下平时用的比较多的预处理命令中的几个:#define,#undef ,#if,#endif.除此之外还有一些预处理命令#warnin ...