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 ...
随机推荐
- 2的m次方 内存对齐
在存储的时候,为了提高效率,一般都会让偏移量落在2的m次方的位置上,而且常有向上取整和向下取整两种需求.向下取整PALIGN_DOWN(x,align) (x & (- align)) 这样 ...
- MD5和DES加密方法
/// <summary> /// MD5加密 /// </summary> /// <param name=&q ...
- Lintcode: Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...
- Lintcode: Maximum Subarray III
Given an array of integers and a number k, find k non-overlapping subarrays which have the largest s ...
- http://codeforces.com/contest/555/problem/B
比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...
- m球求n盒子问题
球同盒同可空盒问题 #include <bits/stdc++.h> using namespace std; const int N = 25; int dp[N][N]; int ma ...
- 找区间连续值(HDU5247)
找连续数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 【IOS】3. OC 类声明和实现
.h文件 @interface NewClassName:ParentClassName { 实例变量://基本类型和指针类型 不能在这里初始化,系统默认会初始化 系统初始化遵循: 实例变量类型 ...
- adb devices 显示error
1.adb kill-server 2.adb start-server
- ARM 寄存器的介绍
ARM 寄存器 31个通用, 32个程序状态寄存器 怎么算的呢: (R0--R15) 16 + 7 + 8 =31 通用 程序状态寄存器: 6 个 共 37 个. 不分组寄存器: ...