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的更多相关文章

  1. UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

    很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M): UVA 11624 写的比较挫 #include <iostream> #include ...

  2. UVA 10047 The Monocycle

    大白图论第二题··· 题意:独轮车的轮子被均分成五块,每块一个颜色,每走过一个格子恰好转过一个颜色. 在一个迷宫中,只能向前走或者左转90度或右转90度(我曾天真的认为是向左走和向右走···),每个操 ...

  3. 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 ...

  4. uva 10047 The Monocycle(搜索)

    好复杂的样子..其实就是纸老虎,多了方向.颜色两个状态罢了,依旧是bfs. 更新的时候注意处理好就行了,vis[][][][]要勇敢地开. 不过这个代码交了十几遍的submission error,手 ...

  5. uva 10047 the monocyle (四维bfs)

    算法指南白书 维护一个四维数组,走一步更新一步 #include<cstdio> #include<cstring> #include<queue> #includ ...

  6. UVa 10047,独轮车

    题目链接:https://uva.onlinejudge.org/external/100/10047.pdf 题目链接:http://vjudge.net/contest/132239#proble ...

  7. The Monocycle(BFS)

    The Monocycle Time Limit: 3000MS64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Des ...

  8. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  9. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

随机推荐

  1. jQuery07源码 (3803 , 4299) attr() prop() val() addClass()等 : 对元素属性的操作

    var nodeHook, boolHook, rclass = /[\t\r\n\f]/g, rreturn = /\r/g, rfocusable = /^(?:input|select|text ...

  2. xml中控件调用构造方法

    系统控件view有三个构造方法,分别如下 public DrawView(Context context) { super(context); } public DrawView(Context co ...

  3. Linq聚合函数使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. How to install Armbian on Orange Pi Plus 2e

    bian on Orange Pi Plus 2e How to install Armbian on Orange Pi Plus 2e Armbian on the microSD You jus ...

  5. 10.axis实现webservices分布式通信

    转自:https://www.aliyun.com/jiaocheng/310112.html 分布式通信原理 基本原理:stub和skeleton作为客户端和服务端传输的中介,stub和skelet ...

  6. js简易留言板

      <!DOCTYPE html>   <html lang="en">   <head>   <meta charset="U ...

  7. canvas:画布

    画布有默认宽度,如果要自己设置宽带要写在属性上 列: <canvas id = "myCanvas" width = "600" height = &qu ...

  8. LuoguP2756 飞行员配对方案问题(最大流)

    题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外 ...

  9. PHP JSON的BUG

    将下面的数组进行 JSON 编码时出错,编码中丢掉了最后一维数组中的下标. Array ( [1] => Array ( [0] => Array ( [0] => Array ( ...

  10. cluster discovery概述及FaultDetection分析

    elasticsearch cluster实现了自己发现机制zen.Discovery功能主要包括以下几部分内容:master选举,master错误探测,集群中其它节点探测,单播多播ping.本篇会首 ...