The Monocycle(BFS)
Time Limit: 3000MS
64bit IO Format: %lld & %llu
Description

A monocycle is a cycle that runs on one wheel and the one we will be considering is a bit more special. It has a solid wheel colored with five different colors as shown in the figure:

The colored segments make equal angles (72o) at the center. A monocyclist rides this cycle on an
grid of square tiles. The tiles have such size that moving forward from the center of one tile to that of the next one makes the wheel rotate exactly 72o around its own center. The effect is shown in the above figure. When the wheel is at the center of square 1, the midpoint of the periphery of its blue segment is in touch with the ground. But when the wheel moves forward to the center of the next square (square 2) the midpoint of its white segment touches the ground.

Some of the squares of the grid are blocked and hence the cyclist cannot move to them. The cyclist starts from some square and tries to move to a target square in minimum amount of time. From any square either he moves forward to the next square or he remains in the same square but turns 90o left or right. Each of these actions requires exactly 1 second to execute. He always starts his ride facing north and with the midpoint of the green segment of his wheel touching the ground. In the target square, too, the green segment must be touching the ground but he does not care about the direction he will be facing.
Before he starts his ride, please help him find out whether the destination is reachable and if so the minimum amount of time he will require to reach it.
Input
The input may contain multiple test cases.
The first line of each test case contains two integers M and N (
,
) giving the dimensions of the grid. Then follows the description of the grid inM lines of N characters each. The character `#' will indicate a blocked square, all other squares are free. The starting location of the cyclist is marked by `S' and the target is marked by `T'. The input terminates with two zeros for M and N.
Output
For each test case in the input first print the test case number on a separate line as shown in the sample output. If the target location can be reached by the cyclist print the minimum amount of time (in seconds) required to reach it exactly in the format shown in the sample output, otherwise, print ``destination not reachable".
Print a blank line between two successive test cases.
Sample Input
1 3
S#T
10 10
#S.......#
#..#.##.##
#.##.##.##
.#....##.#
##.##..#.#
#..#.##...
#......##.
..##.##...
#.###...#.
#.....###T
0 0
Sample Output
Case #1
destination not reachable Case #2
minimum time = 49 sec
题目大意:给一个迷宫,你骑着独轮车在迷宫行进,每前进一格轮子与地面接触的颜色都会变化,总共有5种
颜色依次循环,在一个格子中,可以沿前进方向走一步,左转和右转,每个动作耗时1秒,起始向北,给定
入口和出口,要求到出口时,轮胎与地面接触的颜色与初始时相同,有路径则求最短路径,否则输出不可能
如果没有颜色限制,那么就是简单的最短路,直接BFS,但是这道题,到每个格子的接触地面的扇形颜色、朝向都
可能不同,那我们就可以把一个格子看成多个点,对于同一个格子与地面接触的扇形颜色(5种)、朝向(4个)不同
看成不同的点,那么题目就可以转化为给定起点(朝向,颜色)和终点,求最短路。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
#define next(a) ((a+1)%5)//下一种颜色
const int N=26;
char g[N][N];
bool vis[N][N][4][5];//值为1表示某点是否遍历过,点的要素x,y,direction,color;
int n,m,cas=1;
int dx[4]={-1,0,1,0};//4个方向,本人0代表方向想上故dx[0]=-1,dy[0]=0,其它类推。。表示因为方向问题debug好久
int dy[4]={0,1,0,-1}; struct node{
int x,y,dir,col,step;
node() {}
node(int a,int b,int c,int d,int e):x(a),y(b),dir(c),col(d),step(e) {}
}; bool is_ok(int x,int y){
return x>=0&&x<n&&y>=0&&y<m&&g[x][y]!='#';
} void bfs()
{
queue<node >q;
memset(vis,0,sizeof(vis));
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
{
if(g[i][j]=='S') q.push(node(i,j,0,0,0)),vis[i][j][0][0]=1;
}
while(!q.empty())
{
node e=q.front(); q.pop();
if(g[e.x][e.y]=='T'&&e.col==0){
printf("minimum time = %d sec\n",e.step);
return;
}
//下面分别是向左右转,前进,至于为什么不向后转(向左转两次),
//因为BFS是按时间递增(1s)来遍历,向后转耗时两秒,遍历出来不一定是最优解
int x=e.x,y=e.y,c=e.col;
int d=(e.dir+1)%4;
if(!vis[x][y][d][c])//向右转
{
vis[x][y][d][c]=1;
q.push(node(x,y,d,c,e.step+1));
}
d=(e.dir+3)%4;
if(!vis[x][y][d][c])//向左转
{
vis[x][y][d][c]=1;
q.push(node(x,y,d,c,e.step+1));
}
d=e.dir;
x+=dx[d],y+=dy[d],c=next(c);
if(is_ok(x,y)&&!vis[x][y][d][c]){//前进
q.push(node(x,y,d,c,e.step+1));
vis[x][y][d][c]=1;
}
}
puts("destination not reachable");
} int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)>0&&(n|m))
{
if(cas>1) printf("\n");
for(int i=0; i<n; i++) scanf("%s",g[i]);
printf("Case #%d\n",cas++);
bfs();
}
return 0;
}
The Monocycle(BFS)的更多相关文章
- UVA 10047 - The Monocycle BFS
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa (BFS) The Monocycle
题目不光要求要到达终点而且要求所走的步数为5的倍数,每个时刻有三个选择,前进,左转弯,右转弯. 所以在vis数组中新增加两个维度即可,vis[x][y][dir][color]表示在(x, y)格子方 ...
- UVA 10047-The Monocycle(队列bfs+保存4种状态)
题意:给你一张地图,S代表起点,T代表终点,有一个轮盘,轮盘平均分成5份,每往前走一格恰好转1/5,轮盘只能往前进,但可以向左右转90°,每走一步或是向左向右转90° 要花费1单位的时间,问最少的时间 ...
- UVALive 2035 The Monocycle(BFS状态处理+优先队列)
这道题目真是非常坎坷啊,WA了很多次,但所有的思路都是奔着广搜去想的,一开始出现了比答案大的数据,才想到了应该是优先队列,再说加上也肯定不会错.一开始我读错了题意,以为旋转并且前行需要的时间跟其他一样 ...
- The Monocycle(bfs)
题目描述: 转载自:https://blog.csdn.net/h1021456873/article/details/54572767 题意: 给你一个转轮,有5种颜色,为了5中颜色的位置是确定的, ...
- UVA-10047 The Monocycle (图的BFS遍历)
题目大意:一张图,问从起点到终点的最短时间是多少.方向转动也消耗时间. 题目分析:图的广度优先遍历... 代码如下: # include<iostream> # include<cs ...
- 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 ...
- UVa10047 The Monocycle
UVa10047 The Monocycle 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19491 (以上摘自htt ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
随机推荐
- 从P6 EPPM 8 R3 到P6 EPPM 16 R1 有哪些改变?
Product 特征 First Release for Feature P6 EPPM 通过编辑活动标识替换关系.当你需要修改一个关系,你不需要删除现有的关系,并作出一个新的,你可以简单地编辑活动的 ...
- 重温html5的新增的标签和废除的标签
HTML5已经盛行有段时间了,对于标签的使用,按照规范,哪些该用,哪些不该用,你是否都掌握了呢.今天我在这里详细列举下: 新增的结构标签 section元素 表示页面中的一个内容区 块,比如章节.页眉 ...
- C++之函数重载
函数重载定义: 如果同一作用域内的几个函数名字相同但形参列表不同; 重载与const形参: Record (Phone); = Record(const Phone); Record(Phone*) ...
- ASP.NET HttpRuntime.Cache缓存类使用总结
1.高性能文件缓存key-value存储—Redis 2.高性能文件缓存key-value存储—Memcached 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地 ...
- Java集合 Json集合之间的转换
1. Java集合转换成Json集合 关键类:JSONArray jsonArray = JSONArray.fromObject(Object obj); 使用说明:将Java集合对象直接传进JSO ...
- CSS Hack(转)
做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...
- 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
错误的写法: if (this.GridView1.Rows.Count > 0) { string style = @"<style& ...
- 转:jQuery 常见操作实现方式
http://www.cnblogs.com/guomingfeng/articles/2038707.html 一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备 ...
- R Graphics Cookbook 第3章 – Bar Graphs
3.1 基本条形图 library(ggplot2) library(gcookbook) pg_mean #这是用到的数据 group weight 1 ctrl 5.032 2 tr ...
- oracle断电重启之ORA-01033和ORA-01172
参考文献: ORA-01033:解决方法 数据库掉电后 ORA-01172 磁盘坏块解决方法 --尝试连接数据库prjdb C:\Documents and Settings\Administrato ...