UVa 10047,独轮车
题目链接:https://uva.onlinejudge.org/external/100/10047.pdf
题目链接:http://vjudge.net/contest/132239#problem/B
《训练指南》P308
没什么好说的,学习一下刘汝佳的风格。
#include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f int R,C;
const int maxr = + ;
const int maxc = + ;
char maze[maxr][maxc];
int sr,sc,tr,tc;
int ans; 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[] = {-,,,};
const int dc[] = {,-,,};
int d[maxr][maxc][][],vis[maxr][maxc][][]; 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)
{
Q.push(st);
d[st.r][st.c][st.dir][st.color] = ;
vis[st.r][st.c][st.dir][st.color] = ;
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 cases = ;
//freopen("input.txt","r",stdin);
while(scanf("%d%d",&R,&C),R)
{
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(cases>) printf("\n");
printf("Case #%d\n",++cases);
if(ans==INF) printf("destination not reachable\n");
else printf("minimum time = %d sec\n", ans);
}
return ;
}
UVa 10047,独轮车的更多相关文章
- UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...
- UVA 10047 The Monocycle
大白图论第二题··· 题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色. 在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操 ...
- UVA 10047 - The Monocycle BFS
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 10047 The Monocycle (状态记录广搜)
Problem A: The Monocycle A monocycle is a cycle that runs on one wheel and the one we will be consi ...
- uva 10047 The Monocycle(搜索)
好复杂的样子..其实就是纸老虎,多了方向.颜色两个状态罢了,依旧是bfs. 更新的时候注意处理好就行了,vis[][][][]要勇敢地开. 不过这个代码交了十几遍的submission error,手 ...
- uva 10047 the monocyle (四维bfs)
算法指南白书 维护一个四维数组,走一步更新一步 #include<cstdio> #include<cstring> #include<queue> #includ ...
- UVa 10047 自行车 状态记录广搜
每个格子(x,y,drection,color) #include<iostream> #include<cstdio> #include<cstring> #in ...
- 1.1.1最短路(Floyd、Dijstra、BellmanFord)
转载自hr_whisper大佬的博客 [ 一.Dijkstra 比较详细的迪杰斯特拉算法讲解传送门 Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算 ...
- 最短路算法详解(Dijkstra/SPFA/Floyd)
新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...
随机推荐
- JAVA的JVM虚拟机工作原理.V.1.0.0
注意:一下内容纯属个人理解,如有错误,欢迎批评指正. (90度弯腰)谢谢. java在JVM上的运行过程: 1,编辑好的java代码(IDE无报错,测试运行无错误): 2,java源代码通过javac ...
- The golden ratio: 1.618
http://www.chinaz.com/design/2015/1109/467968_2.shtml The golden ratio: 1.618 a/b=b/(a+b) The Fibona ...
- [原创]java WEB学习笔记58:Struts2学习之路---Result 详解 type属性,通配符映射
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- android ANR问题
一.什么是ANR ANR:Application Not Responding: 具体请参考:http://blog.csdn.net/dadoneo/article/details/8270107
- Android studio删除工程项目,androidstudio
本新手最近学Android都是用的eclipse.其实个人觉得eclipse不错,可能接触Android不久,倒也不觉得它慢还是怎样.对于Google的Android studio也是早有耳闻,前两天 ...
- Hashing function
Hashing function (散列函式) 在网页应用中被广泛采用,从数码签署.错误检测.登入验证.到压缩储存空间,由于它的原理比较复杂,很多人把它跟加密函式混淆,对于如何运用hash funct ...
- 3D语音天气球(源码分享)——创建可旋转的3D球
开篇废话: 在9月份时参加了一个网站的比赛,比赛的题目是需要使用第三方平台提供的服务做出创意的作品. 于是我选择使用语音服务,天气服务,Unity3D,Android来制作一个3D语音天气预报,我给它 ...
- 关于header跳转之后的乱码问题
关于header跳转之后的乱码问题 http://www.360doc.com/content/11/0603/19/7052474_121495648.shtml 问题:不同网站的跳转出现乱码,不同 ...
- OpenGl And 视图
OpenGl And 视图 标签(空格分隔): game 简介 本文主要介绍坐标系的观念, 以及在openGL中的视图及其相关的变换. 大纲 视图.模型.投影变换概念 Opengl中对各种变换的支持 ...
- zw版_Halcon-delphi系列教程_卫星航拍精确打击目标自动识别
<zw版·Halcon-delphi系列原创教程> 卫星航拍精确打击目标自动识别 这几天,俄罗斯空军在叙利亚大显神威,美军有意见了,说俄罗斯是狂轰滥炸,不是精确打击. 不过,战斗 ...