The Monocycle

Time Limit: 3000MS
64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

Problem A: The Monocycle

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 mid­point 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 mid­point 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 mid­point 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)的更多相关文章

  1. UVA 10047 - The Monocycle BFS

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. UVa (BFS) The Monocycle

    题目不光要求要到达终点而且要求所走的步数为5的倍数,每个时刻有三个选择,前进,左转弯,右转弯. 所以在vis数组中新增加两个维度即可,vis[x][y][dir][color]表示在(x, y)格子方 ...

  3. UVA 10047-The Monocycle(队列bfs+保存4种状态)

    题意:给你一张地图,S代表起点,T代表终点,有一个轮盘,轮盘平均分成5份,每往前走一格恰好转1/5,轮盘只能往前进,但可以向左右转90°,每走一步或是向左向右转90° 要花费1单位的时间,问最少的时间 ...

  4. UVALive 2035 The Monocycle(BFS状态处理+优先队列)

    这道题目真是非常坎坷啊,WA了很多次,但所有的思路都是奔着广搜去想的,一开始出现了比答案大的数据,才想到了应该是优先队列,再说加上也肯定不会错.一开始我读错了题意,以为旋转并且前行需要的时间跟其他一样 ...

  5. The Monocycle(bfs)

    题目描述: 转载自:https://blog.csdn.net/h1021456873/article/details/54572767 题意: 给你一个转轮,有5种颜色,为了5中颜色的位置是确定的, ...

  6. UVA-10047 The Monocycle (图的BFS遍历)

    题目大意:一张图,问从起点到终点的最短时间是多少.方向转动也消耗时间. 题目分析:图的广度优先遍历... 代码如下: # include<iostream> # include<cs ...

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

  8. UVa10047 The Monocycle

    UVa10047 The Monocycle 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19491 (以上摘自htt ...

  9. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

随机推荐

  1. 自定义ConfigurationSection,创建多个嵌套的ConfigurationElementCollection节点

    由于接口地址都是固定的,所以想到使用自定义节点,来将接口都配置到web.config中. 很快,v1.0版本出炉: public class RequestConfigSection : Config ...

  2. (转) 关于在IE6下 无法跳转问题

    之前在项目,用到超链接,在ie下没有问题,但是到了ie6,居然发现点击事件不起作用, 真不可思议,以前都没注意到,后来网上搜了下,问题就出在这个void(0)上!现把网上的资料整理了下. <a  ...

  3. 安装xampp无法设置默认时间的坑

    xampp无法设置默认时间,修改了时间还是无效 [Date] ; Defines the default timezone used by the date functions ; http://ph ...

  4. 1秒30000QPS,前后端设计思路

    Q:现在有这样一个需求,在一秒中有3万的支付订单请求,有什么比较好的解决方案吗? PS:我们数据库用的是oracle 程序是java spring mybatis dubbo mq等技术,现在有这样一 ...

  5. 给我一个及时的问候——XMPP

    XMPP总的来说就是:基于XML数据结构,点对点的,及时通讯协议 是 Linux操作系统+Apache软件+mySql数据库 + php 编程语言 组成   开始时要导入 XMPPFrameWork框 ...

  6. ABAP:区别CALL SCREEN/SET SCREEN/LEAVE TO SCREEN

    1,CALL SCREEN XXXX将在Screen调用栈(CALL STACK)上面添加一层调用(进栈),调用XXXX的PBO和PAI,如果XXXX的Next Screen不为0,那么将继续其Nex ...

  7. andriod 动态显示当前时间

    <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android=&quo ...

  8. andriod 动态创建控件

    Button btNext=(Button)findViewById(R.id.next); btNext.setOnClickListener(new Button.OnClickListener( ...

  9. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q73-Q76)

    Question 73You create a Web Part that calls a function named longCall.You discover that longCall tak ...

  10. 使用tinypng优化Android的资源图片

    tinypng 是一个支持压缩png和jpg图片格式的网站,通过其独特的算法(通过一种叫“量化”的技术,把原本png文件的24位真彩色压缩为8位的索引演示,是一 种矢量压缩方法,把颜色值用数值123等 ...