uva 10047 the monocyle (四维bfs)
算法指南白书
维护一个四维数组,走一步更新一步
#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)的更多相关文章
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- UVA 10047 - The Monocycle BFS
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10047,独轮车
题目链接:https://uva.onlinejudge.org/external/100/10047.pdf 题目链接:http://vjudge.net/contest/132239#proble ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- uva 11234 Expressions 表达式 建树+BFS层次遍历
题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include < ...
- UVA 10047 The Monocycle
大白图论第二题··· 题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色. 在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操 ...
- UVA 810 - A Dicey Problem(BFS)
UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...
- UVA 11573 - Ocean Currents(BFS+优先队列)
UVA 11573 - Ocean Currents 题目链接 题意:给定一个海面.数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量,每次询问给一个起点一个终点,问起点到终点耗费的最小能量 ...
随机推荐
- vs的watch使用
VC调试器高级应用----WATCH窗口篇 一.格式化数据和表达式赋值语句. 常用变量格式化符(表达式的值后跟逗号,接格式化符,如"(int)0xFFFF,d"):d :有符号的 ...
- PHP——图片上传
图片上传 Index.php文件代码: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- Linux下Docker安装
1 在 CentOS 6.4 上安装 docker docker当前官方只支持Ubuntu,所以在 CentOS 安装Docker比较麻烦(Issue #172). docker官方文档说要求 ...
- js禁止高频率连续点击思路
1.类似react的数据流,点击之后立即设置值为空,当返回值后才可以点击 2.设置定时器,每次进入之前先清空掉定时器,然后开启定时器 <main> <div id="me& ...
- postgres 约束 多个条件 联合 约束
ADD CONSTRAINT xxx CHECK ( (col1 = 0.0) = (col2 IS NOT NULL)); ## 相当于check (true = ture)
- 方便mac os 10.9系统中phpstorm配置php运行环境
自己安装php,不用mac安装,这样就有php开发环境了. 安装很简单,直接运行一个命令, 需要几分钟,请慢慢等待. curl -s http://php-osx.liip.ch/install.sh ...
- 例行性工作排程 (crontab)
1. 什么是例行性工作排程 1.1 Linux 工作排程的种类: at, crontab 1.2 Linux 上常见的例行性工作2. 仅运行一次的工作排程 2.1 atd 的启动与 at 运行的方式: ...
- 【转】MyBatis中Like语句使用方式
http://www.cnblogs.com/littleCode/p/3727476.html oracle数据库: SELECT * FROM user WHERE name like CONCA ...
- Java NIO之Selector
选择器是JavaNIO重磅推出的一个概念:在旧有的系统中为了跟踪多端口消息,需要为每一个端口配备一个线程做监听:但是有了selector就不需要了,一个Selector可以管理一众渠道(channel ...
- iBatis系列一
XML iBatis可以使用xml来作为参数输入以及结果返回:这个功能的优势在于某些特定的场景:还有可以通过DOM方式来作为参数传递:但是这个方式应用的比较少,如果服务器是xml服务器可以采用这种方式 ...