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.若当前访问的顶点的邻接顶点有未被访问的,则 ...
随机推荐
- web api返回格式小结
web api返回格式小结: 1.默认是返回xml格式数据,如果需要返回json格式,需要在Global.asax中加入: GlobalConfiguration.Configuration.Form ...
- 《Node.js+MongoDB+AngularJS Web开发》读书笔记及联想
总体介绍 <Node.js+MongoDB+AngularJS Web开发>,于2015年6月出版,是一本翻译过来的书,原书名为<Node.js,MongoDB and Angula ...
- 【转载】JS获取屏幕大小
前些日子需要给项目的弹窗上面罩,因为项目左侧是树形菜单,右侧嵌套的iframe ,iframe 的内容不是固定大小,那么,面罩的大小也就不是固定的 因此,用到了JQuery获取当前页面的窗口大小,于是 ...
- AssemblyInfo文件
程序生成版本信息 AssemblyInfo.cs主要用来设定生成的有关程序集的常规信息dll文件的一些参数 请看以下具体说明: //备注: [assembly:AssemblyDescription ...
- 织梦CMS的MVC体系
13年无意中翻看DedeCMS的代码,发现DedeCMS中是有了一个基本MVC框架的,在现有的版本中,主要是应用到了ask.book等模块上. 织梦这个东西,里面很多设计思想是非常优秀的,但整体代码的 ...
- Treap树的基础知识
原文 其它较好的的介绍:堆排序 AVL树 树堆,在数据结构中也称Treap(事实上在国内OI界常称为Traep,与之同理的还有"Tarjan神犇发明的"Spaly),是指有一个随 ...
- Java中检测字符串的编码格式
public static String getEncoding(String str) { String encode = "GB2312"; try { if (str.equ ...
- iOS UITableview
1. UITableView //去除tableviews的点击效果 cell.selectionStyle = UITableViewCellSelectionStyleNone; //隐藏t ...
- [python学习笔记]Day1
初识python 第一个python程序: print('Hello,Python') >>>Hello,Python python2与python3的一些主要的区别: 1.在pyt ...
- 最全面的jdbcUtils,总有一种适合你
附加jar包,TxQueryRunner.java文件,dbconfig.properties配置文件(点击链接下载): http://files.cnblogs.com/files/xiaoming ...