UVA 10047 - The Monocycle BFS
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=988
题目大意:
独轮车的车轮被分为5个扇形,分别涂上一种不同的颜色,现在有一个人行驶在M*N的玩个平面上。每个格子的大小刚好为一个扇形。有些格子有障碍,骑车的人从S出发要到达T,途中,在任何一个格子的时候他要么骑到下一个格子,要么左转或者右转90度,初始他面朝北,并且绿色格子贴着地面,要求到终点时候也是绿色格子贴着地面。
思路:
半年前觉得挺难的题目现在看起来好简单。哈哈哈哈哈哈哈哈哈~
BFS。。
状态需要记录方向和此时的颜色。
代码略丑。主要是BFS过程中我是直接枚举的。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=30;
char map[MAXN][MAXN];
bool vis[MAXN][MAXN][5][4];//x,y,color,dir
int m,n;
struct state
{
int x,y;
int step;
int dir;//direction :north:0 south:1 east:2 west:3
int color;//开始为0,终点也要为0
bool operator <(const state &a)const {
return step > a.step;
}
state(){;}
state(int x,int y,int step,int dir,int color) :x(x) ,y(y),step(step), dir(dir), color(color){}
}start,fin;
int bfs()
{
memset(vis,0,sizeof(vis));
priority_queue<state> q;
q.push(start);
vis[start.x][start.y][start.color][start.dir]=true;
while(!q.empty())
{
state cur=q.top();
q.pop();
if(cur.x==fin.x && cur.y==fin.y && cur.color==0)//找到答案了!
return cur.step; int x=cur.x,y=cur.y,step=cur.step,color=cur.color,dir=cur.dir;
switch(cur.dir)
{
case 0: //北
if(!vis[x-1][y][(color+1) % 5][dir] && map[x-1][y]!='#')
{
vis[x-1][y][(color+1) % 5][dir]=1;
q.push(state(x-1,y,step+1,dir,(color+1) % 5));
}
if(!vis[x][y][color][2]){
vis[x][y][color][2]=1; q.push(state(x,y,step+1,2,color));
}
if(!vis[x][y][color][3]) {
vis[x][y][color][3]=1; q.push(state(x,y,step+1,3,color));
}
break;
case 1: //南
if(!vis[x+1][y][(color+1) % 5][dir] && map[x+1][y]!='#')
{
vis[x+1][y][(color+1) % 5][dir]=1;
q.push(state(x+1,y,step+1,dir,(color+1) % 5));
}
if(!vis[x][y][color][2]){
vis[x][y][color][2]=1; q.push(state(x,y,step+1,2,color));
}
if(!vis[x][y][color][3]) {
vis[x][y][color][3]=1; q.push(state(x,y,step+1,3,color));
}
break;
case 2: //东
if(!vis[x][y+1][(color+1) % 5][dir] && map[x][y+1]!='#')
{
vis[x][y+1][(color+1) % 5][dir] =1;
q.push(state(x,y+1,step+1,dir,(color+1) % 5));
}
if(!vis[x][y][color][0]){
vis[x][y][color][0]=1; q.push(state(x,y,step+1,0,color));
}
if(!vis[x][y][color][1]){
vis[x][y][color][1]=1; q.push(state(x,y,step+1,1,color));
}
break;
case 3: //西
if(!vis[x][y-1][(color+1) % 5][dir] && map[x][y-1]!='#')
{
vis[x][y-1][(color+1) % 5][dir]=1;
q.push(state(x,y-1,step+1,dir,(color+1) % 5));
}
if(!vis[x][y][color][0]){
vis[x][y][color][0]=1; q.push(state(x,y,step+1,0,color));
}
if(!vis[x][y][color][1]){
vis[x][y][color][1]=1; q.push(state(x,y,step+1,1,color));
}
break;
}
}
return -1;
}
int main()
{
int kase=1;
while(~scanf("%d%d",&m,&n),m||n)
{
for(int i=1;i<=m;i++)
scanf("%s",map[i]+1);
for(int i=0;i<=n+1;i++) //最外面加上一层围墙,等下过程就可以减少判断。
map[0][i]=map[m+1][i]='#';
for(int i=0;i<=m+1;i++)
map[i][0]=map[i][n+1]='#';
/*
for(int i=0;i<=m+1;i++)
{
for(int j=0;j<=n+1;j++)
printf("%c",map[i][j]);
puts("");
}
*/
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(map[i][j]=='S')
{
state temp(i,j,0,0,0);
start=temp;
map[i][j]='.';
}
else if(map[i][j]=='T')
{
state temp(i,j,0,0,0);
fin=temp;
map[i][j]='.';
}
}
}
int ans=bfs();
if(kase!=1)
printf("\n");
printf("Case #%d\n",kase++);
if(ans==-1)
printf("destination not reachable\n");
else
printf("minimum time = %d sec\n",ans);
}
return 0;
}
UVA 10047 - The Monocycle BFS的更多相关文章
- 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 (状态记录广搜)
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,独轮车
题目链接:https://uva.onlinejudge.org/external/100/10047.pdf 题目链接:http://vjudge.net/contest/132239#proble ...
- The Monocycle(BFS)
The Monocycle Time Limit: 3000MS64bit IO Format: %lld & %llu [Submit] [Go Back] [Status] Des ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
- UVA 439 Knight Moves(BFS)
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
随机推荐
- button按钮怎么实现超链接
button按钮怎么实现超链接 一.总结 1.我的按钮实现超链接是通过button内嵌a标签来实现的 <button class="am-btn am-btn-default am-b ...
- Kinect 开发 —— 骨骼数据与彩色影像和深度影像的对齐
在显示彩色影像和深度影像时最好使用WriteableBitmap对象: 要想将骨骼数据影像和深度影像,或者彩色影像叠加到一起,首先要确定深度影像的分辨率和大小,为了方便,这里将深度影像数据和彩色影像数 ...
- vs2010 Visula C++ 把CString 转换为string 类型
CString strSomeCstring ("This is a CString Object"); // Use ANSI variant CStringA to conve ...
- JeeSite信息化快速开发平台
平台简介 JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的开源Java EE快速开发平台. JeeSite是您快速完成项目的最佳基础平台解决方案,JeeSite是您 ...
- 【Codeforces Round #456 (Div. 2) C】Perun, Ult!
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] set1 < pair < int,int > > set1;记录关键点->某个人怪物永远打不死了,第 ...
- maven 遇到failOnMissingWebXml有关问题解决方法
(转自) http://blog.csdn.net/liuvlun/article/details/50218507
- ubuntu-12.04工作区内容变换所属工作区
最近一直纠结于ubuntu12.04窗口更改所属工作区问题,今天在网上看到了方法.记录下来 主要就是利用快捷键. 1.打开你想移动的窗口 2.使用快捷键Shift + Ctrl + Alt + Dow ...
- Java学习笔记二.1
和其他高级语言类似,Java也具有以下部分 1.关键字:见下表,注意Java严格区分大小写,关键字都是小写 2.标识符:见下图 3.注释.有两种://(单行注释)和/**/(多行注释).还有一种文档注 ...
- HDU 2988 Dark roads(kruskal模板题)
Dark roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Codefroces 784 愚人节题目(部分)
A. Numbers Joke time limit per test 2 seconds memory limit per test 64 megabytes input standard inpu ...