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 ...
随机推荐
- iOS缓存使用的框架
MagicalRecord FMDB 都可以在gitHub上找到
- Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作
HDU_3487 题意:给出n和q,n代表1-n的序列,接下来q有两种操作,Cut a b c:表示把区间[a,b]截掉然后放在第c个数的后面,Flip a b 表示把区间[a,b]反转,经过一系列的 ...
- [原创]java WEB学习笔记84:Hibernate学习之路-- -映射 一对一关系 ,基外键的方式实现
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- ios blog
转得一个朋友的博客,大家可以看哈(主要时国外的) 主要分开发教程.示例项目.UI设计.问题解决几块. 开发教程: 即便过了入门阶段,还是要经常看看一些不错的实例教程. .http://mobile.t ...
- C# 实现 单例模式
http://blog.sina.com.cn/s/blog_75247c770100yxpb.html
- sql case when 速记
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- Oracle EBS环境下查找数据源(OAF篇)
在用户层设置预制文件:Personalize Self-Service Defn 的值为Yes 来启动个性化模式 参考:http://www.2cto.com/database/201109/1041 ...
- EBS登陆链接网址
SELECT home_url FROM icx_parameters; SELECT profile_option_value FROM fnd_profile_option_values WHER ...
- 夺命雷公狗---node.js---8url模块和util模块
我们先到手册上看看: 上面很明显就写着返回一个对象. 再来看看util模块, 废话不哦多说,先上一点代码: /** * Created by leigood on 2016/8/13. */ var ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON DirectFile
zw版[转发·台湾nvp系列Delphi例程]HALCON DirectFile unit Unit1;interfaceuses Windows, Messages, SysUtils, Varia ...