10047 - The Monocycle(bfs,4级)

本文介绍了一种基于网格的单轮车(Monocycle)路径规划算法,该车在一个特殊的五色轮上运行,通过广度优先搜索(BFS)结合方向与颜色状态来寻找从起点到终点的最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


  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 $M \times N$ 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 ($1 \le M$, $N \le 25$) giving the dimensions of the grid. Then follows the description of the grid in M 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



 

Miguel Revilla
2000-12-26

 

思路:毫无疑问广搜啊,位置+颜色+方向为标志,如果就一步步搜的话TLE啊。因此可以搜上一两步,用优先队列。

10047The MonocycleAcceptedC++0.022

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int mm=30;
const int mod=5;
const int dx[]={-1,0,1,0};///NESW
const int dy[]={0,1,0,-1};
const int turn[]={1,3};
bool vis[mm][mm][5][4];
class node
{
  public:int x,y,dir,cor,step,num;
  bool operator < (const node&it)const
  {
    return step>it.step;
  }
};
int n,m;
node S,T;
char g[mm][mm];
priority_queue<node>q;
void out(node x)
{
  cout<<x.x<<" "<<x.y<<" "<<x.dir<<" "<<x.cor<<" "<<x.step<<endl;
}
int aabs(int x)
{
  if(x<0)return -x;
  else return x;
}
int num;
int bfs()
{ memset(vis,0,sizeof(vis));
  while(!q.empty())q.pop();
  q.push(S);node z,t;S.num=0;
  vis[S.x][S.y][S.cor][S.dir]=1;
  while(!q.empty())
  {
    z=q.top();q.pop();
    for(int i=0;i<4;++i)
    {
      t=z;t.x+=dx[i];t.y+=dy[i];
      if(t.x<0||t.x>=n||t.y<0||t.y>=m||g[t.x][t.y]=='#')continue;
      int yes=0;
      switch(aabs(t.dir-i))///变方向
      {
        case 0:t.step++;yes=1;break;
        case 1:t.step+=1;break;
        case 2:t.step+=2;break;
        case 3:t.step+=1;break;
      }
      if(yes)
      {
        t.cor=(t.cor+1)%5;t.num++;
      }
      else
      {
        t.x=z.x;t.y=z.y;
      }
      t.dir=i;
      if(g[t.x][t.y]=='T'&&t.cor==0)
      {num=t.num;return t.step;}
      if(!vis[t.x][t.y][t.cor][t.dir])
      { vis[t.x][t.y][t.cor][t.dir]=1;
        q.push(t);
      }
    }
  }
  return -1;
}

int main()
{ int cas=0;
  while(~scanf("%d%d",&n,&m))
  { if(n==0&&m==0)break;
    for(int i=0;i<n;++i)
      scanf("%s",g[i]);
    int ans;
    for(int i=0;i<n;++i)
      for(int j=0;j<m;++j)
      if(g[i][j]=='S')
        {S.x=i,S.y=j,S.dir=0,S.cor=0,S.step=0;
         ans=bfs();
        }
       /// cout<<num<<endl;
       if(cas)printf("\n");
        printf("Case #%d\n",++cas);
    if(ans>0)printf("minimum time = %d sec\n",ans);
    else printf("destination not reachable\n");
  }
  return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值