算法指南白书

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

 #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. Linux下U盘的格式化

    一次系统装机带来的烦恼. 之前有一次装centos 系统 ,把一个centos4.8的系统刻录到了一个8G的U盘,之后是centos安装成功了 ,却发现电脑不认识U盘了,试了好多次也没有处理好,刚好今 ...

  2. php hook 之简单例子

    <?php// 应用单例模式// 建立相应的 plugins 文件夹,并建立 .php 文件放在里面class plugin{    public $actions;    public $fi ...

  3. 用PHP Session和Javascript实现文件上传进度条,处理进度条

    参考http://www.pureweber.com/article/php-upload-progress/ php显示处理进度条http://blog.csdn.net/boabc2304/art ...

  4. 网络ip

    国际规定:把所有的IP地址划分为 A,B,C,D,E A类地址:范围从0-127,0是保留的并且表示所有IP地址,而127也是保留的地址,并且是用于测试环回用的.因此 A类地址的范围其实是从1-126 ...

  5. Function Pointer in Delpni

    program Project1; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type TVoice = function(): Stri ...

  6. LeetCode【第217题】Contains Duplicate

    题目: ''' Given an array of integers, find if the array contains any duplicates. Your function should ...

  7. NULL, nil, Nil详解

    原文地址:http://blog.csdn.net/wzzvictory/article/details/18413519    感谢原作者 作者:wangzz 原文地址:http://blog.cs ...

  8. 简谈ArrayList和LinkedList区别

    对于ArrayList和LinkedList,他们都实现了List接口,他们的区别大致为: ArrayList LinkedList (1)底层是数组,可以以O(1)的时间复杂度对元素进行随机访问 以 ...

  9. my9.23(输入输出,写操作)

    #include <iostream> #include <fstream> using namespace std; void write(int data,int len) ...

  10. webserver<2>

    #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wai ...