The Monocycle(BFS)

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),码迷,mamicode.com

时间: 2024-08-30 03:35:01

The Monocycle(BFS)的相关文章

uva 10047 - The Monocycle bfs

题目链接 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

uva 10047 uva live 2035 The Monocycle bfs

// uva 10047 uva live 2035 bfs // 求最短的嘛,肯定先尝试bfs啦 // 确定状态,首先状态里面得有坐标x,y // 还得有朝向,还得有颜色值 // // 这样就是一个状态里面有着三种属性 // 每个状态都只要经历一次,再经历是没有任何意义的 // 用一个que的思维数组记录就行了. // 按照方向爆搜,我先用f[i][j]记录的就是到 // 这一点的最小距离,但是怎么都过不了样例 // 突然明白了,如果只是这样记录最短距离,是不行的 // 因为每次从队列中取出的

UVA10047- The Monocycle(BFS)

题目链接 题意:一自行车的轮子被分成5个扇区,涂了5种不同颜色.自行车每1秒要么骑到下一个格子,要么左转或者右转90..一开始自行车面向北,颜色为绿,到达目标格时,必须触底颜色为绿,但朝向无限制.求到达目标格的最短时间. 思路:判重数组多加两维,分别为朝向和颜色,之后就可以用BFS求最少时间了. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <queue> #inc

【BFS】uva10047The Monocycle

/* 本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同, 会处于不同的状态::::::::: 把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;; ------------------------------------------------------------------------ 在方向上我们把前,左,右编号为0,1,2:::: 颜色,从蓝色开始编号为0,1,2,3:::::::::: ------------------

UVA The Monocycle(BFS 4种状态)

 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

UVA 10047 - The Monocycle(BFS)

题目链接:点击打开链接 题意:从起点到终点,每秒可以选择前进.向左.向右转, 每前进一格轮子转到下一个颜色, 一共5中颜色, 开始的时候绿色接触地面,朝北, 要求最后也绿色接触地面,求能否到达目标点以及最短时间. 思路:和普通BFS相比,多了两个附加条件,所以要将状态表示全面,也要对应加两维. 水题. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream&g

UVa (BFS) The Monocycle

题目不光要求要到达终点而且要求所走的步数为5的倍数,每个时刻有三个选择,前进,左转弯,右转弯. 所以在vis数组中新增加两个维度即可,vis[x][y][dir][color]表示在(x, y)格子方向朝dir与地面接触的扇形的颜色为color,这个状态是否到达过. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn =

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

这道题目真是非常坎坷啊,WA了很多次,但所有的思路都是奔着广搜去想的,一开始出现了比答案大的数据,才想到了应该是优先队列,再说加上也肯定不会错.一开始我读错了题意,以为旋转并且前行需要的时间跟其他一样,但是旋转的动作是需要额外计时的.我的第一种方法错误原因还没有找到,我在旋转以后就直接改动了位置,感觉没有什么本质区别,旋转了以后,肯定要走啊,我直接加上时间也没什么问题,我也把所能想到的测试用例都试过了,与AC代码完全一致,真是搞不懂,这要是CF就好了.于是,学习了别人的代码,改了改我原先的地方,

UVA - 10047 The Monocycle (BFS)

题目大意:有一个n*m的网格,网格上面有的地方有障碍物 现在有一个人,骑着独轮车,要求从一个地方到达另一个地方,骑独轮车时,只能直走,或者左拐,右拐,不能向后走 独轮车的轮子被分成了5部分,每部分都有对应的颜色,刚开始时是绿色向下,当经过一个格子时,颜色就会变换 问从起点出发到终点,到终点时独轮车的绿色颜色向下,需要多久 解题思路:暴力BFS #include <cstdio> #include <cstring> #include <algorithm> #inclu