UVA 10047 The Monocycle (BFS)

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



  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

给出一张地图(‘#’为障碍)和一个轮,要求从S走到T,轮上染有5种颜色,每走一格转到下一个颜色且花费1秒,每次可以往相邻的东南西北4个方向的格子走,向左/右转弯要额外花1秒,旋转180度额外花费2秒,起初在S点轮为绿色且向北,要求最终到T点时轮也为绿色但方向可以任意,格子可以多次进入,求最短时间。


通常求从S走到T的最短时间都直接用BFS,但那些都是无返回的情况,而且本题对于进入的格子还要考虑不同的状态(方向,颜色),这就和通常的BFS不太一样了。

我们知道BFS优先对时间最早的状态进行搜索,对于一般情况,队首元素就是时间最早的状态,但本题从当前状态往下走会出现t+1,t+2,t+3这三种情况,这样队首元素就不是时间最早的状态了,这样我们就要找出时间最早的状态,想一想,可以使用优先队列,这样堆顶元素就是时间最早的状态。

因为有返回,判重似乎有点麻烦,传统的判重根本做不了返回,但又不可能不判。想一想判重的实质,实际上判的是状态,这里的状态不光有位置,还有方向和颜色。判重的意义在于减少搜索不必要的状态并避免死循环(因为我们找的是最优解,已经走过的状态以后再进入必然不是我们想要的状态)。这里我们用vis[x][y][dir][col]来表示在位置(x,y)处方向为dir,颜色为col的状态,记录到达该状态的时间,如果时间t<vis[x][y][dir][col]就更新,这里不用传统的T/F来判断是因为早些的状态不一定是最早产生的,而我们要求的是最优解,这里不妨将vis初始化为INF,状态更新:if (t<vis) vis=t;push(status);


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define mm

using namespace std;

char MAP[30][30];
int vis[30][30][4][5];

int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};

int fx,fy,tx,ty,n,m;

struct record
{
    int x,y;
    int dir;
    int t;
    int col;

    record(){};
    record(int xx,int yy,int tt,int dd,int cc)
    {
        x=xx;y=yy;t=tt;dir=dd;col=cc;
    }
    record(const record &r)
    {
        x=r.x;y=r.y;t=r.t;
        dir=r.dir;col=r.col;
    }
    friend bool operator < (const record &x,const record &y)
    {
        return y.t<x.t;
    }
};

void bfs()
{
    int x,y;
    memset(vis,0x3f,sizeof vis);
    priority_queue<record> q;

    q.push(record(fx,fy,0,0,0));
    vis[fx][fy][0][0]=0;

    while (!q.empty())
    {
        record r=q.top();q.pop();

        if (r.x==tx && r.y==ty && (r.col%5)==0)
        {
            printf("minimum time = %d sec\n",r.t);
            return ;
        }

        for (int i=0;i<4;i++)
        {
            x=r.x+dx[i];y=r.y+dy[i];

            if (MAP[x][y]=='.')
            {

                if (i==r.dir)
                {
                    if (vis[x][y][i][(r.col+1)%5]>r.t+1)
                    {
                        q.push(record(x,y,r.t+1,i,(r.col+1)%5));
                        vis[x][y][i][(r.col+1)%5]=r.t+1;
                    }
                    continue;
                }
                if (abs(i-r.dir)==2)
                {
                    if (vis[x][y][i][(r.col+1)%5]>r.t+3)
                    {
                        q.push(record(x,y,r.t+3,i,(r.col+1)%5));
                        vis[x][y][i][(r.col+1)%5]=r.t+3;
                    }
                    continue;
                }
                if (vis[x][y][i][(r.col+1)%5]>r.t+2)
                {
                    q.push(record(x,y,r.t+2,i,(r.col+1)%5));
                    vis[x][y][i][(r.col+1)%5]=r.t+2;
                }

            }

        }
    }

    puts("destination not reachable");
    return ;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    int tt=0;

    while (scanf("%d%d",&n,&m),n||m)
    {
        char c;
        getchar();

        if (tt) puts("");
        printf("Case #%d\n",++tt);

        memset(MAP,'#',sizeof(MAP));

        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=m;j++)
            {
                c=getchar();
                if (c!='#')
                    MAP[i][j]='.';

                if (c=='S')
                {
                    fx=i;fy=j;
                }
                if (c=='T')
                {
                    tx=i;ty=j;
                }
            }
            getchar();
        }

        bfs();

    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值