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点能量,每次询问给一个起点一个终点,问起点到终点耗费的最小能量 ...
随机推荐
- 查找PHP的配置文件
查找PHP的配置文件 先写了一个 <?php phpinfo();?>然后在浏览器中浏览一下(之前我百度说在Configuration File 这个位置看) 结果竟然显示 Loaded ...
- PHP优化小结
1.echo 比 print 快,并且使用echo的多重参数(指用逗号而不是句点)代替字符串连接,比如echo $str1,$str2.如果使用echo $str1.$str2 就会需要 PHP 引擎 ...
- php curl模拟post请求提交数据
最近在做校园图书馆图书信息的采集程序,既然是图书馆图书的采集,肯定有提交搜索的页面,无非是post提交,让我想到了curl模拟提交,首先通过firebug进行抓包查询下post提交后的格式如下: tx ...
- 查看文章 mysql:表注释和字段注释[转]
1 创建表的时候写注释 create table test1 ( field_name int comment '字段的注释' )comment='表的注释'; 2 修改表的注释 alter tabl ...
- REST API之前端跨域访问
关键字:跨域访问,cross-origin, NodeJS, REST API, JavaScript, Access-Control-Allow-Origin 1.新建并运行一个 NodeJS的se ...
- osg学习笔记3 简单几何模型
osg::Geode (geometry node) osg::Geode类表示场景中的渲染几何叶节点,它包含了渲染用的几何信息,没有子节点. 要绘制的几何数据保存在osg::Geode管理的一组os ...
- 用19种编程语言写Hello World
用19种编程语言写Hello World 转载自:http://www.admin10000.com/document/394.html Hello World 程序是每一种编程语言最基本的程序,通常 ...
- IntelliJ IDEA配置缓存地址
idea的配置缓存路径是保存配置文件和插件的本地目录,对idea上的所有个人修改全都储存在那里 相比于eclipse的免安装.解压即用来说,一旦系统重装或者idea重装又没有进行备份的情况下,个人对i ...
- 这是从word发的第一篇博客。
喜欢做的事,怎么样都不会厌倦. 以前只知道office功能强大,但不太清楚到底还能干些啥,印象最深的是outlook了,自己也在用,挺好 今天偶然发现,word还能发布博客,真是太惊喜了 这算是一篇实 ...
- angularJS vs backbone
http://alistapart.com/article/javascript-mvc http://blog.nebithi.com/backbone-and-angular-demystifyi ...