zoj 3865 Superbot(BFS)

Superbot

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Superbot is an interesting game which you need to control the robot on anN*M grid map.

Superbot是需要你在一个N*M的网格地图里控制机器人的有趣游戏。

As you see, it's just a simple game: there is a control panel with four direction left (1st position), right (2nd), up (3rd) and down (4th). For each second, you can do exact

如你所见,这仅仅只是一个简单的游戏:游戏有带有四个方向 左右上下 的控制板。每一秒,你能做下列操作中

 one of the following operations:

的一个操作。

  • Move the cursor to left or right for one position. If the cursor is on the 1st position and moves to left, it will move to 4th position; vice versa.
  • 可以向左或右移动光标一个位置。如果光标在第一个位置向左移的结果是第四个位置,反之亦然。
  • Press the button. It will make the robot move in the specific direction.
  • 按下按钮。这让机器人向指定的方向移动。
  • Drink a cup of hot coffee and relax. (Do nothing)
  • 休息休息,喝杯热咖啡(没做什么)

However, it's too easy to play. So there is a little trick: Every P seconds the panel will rotate its buttons right. More specifically, the 1st position moves to the 2nd position;

显然,这太容易玩了。因此这里设置了一些小陷阱:每隔P秒,控制板会旋转按钮的顺序。更具体地来说,第一

 the 2nd moves to 3rd; 3rd moves to 4th and 4th moves to 1st. The rotating starts at the beginning of the second.

个位置到第二个位置,第二个位置到第三个位置,第三个位置到第四个位置,第四个位置到第一个位置。从第一秒就开始旋转。

Please calculate the minimum time that the robot can get the diamond on the map.

请计算机器人最少用几秒能得到地图里的钻石。

At the beginning, the buttons on the panel are "left", "right", "up", "down" respectively from left to right as the picture above, and the cursor is pointing to "left".

刚开始,控制板上从左到右的按钮分别为 左右上下,并且光标指向‘左‘

’Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains three integers N, M (2 <= N,M <= 10) andP (1 <= P <= 50), which represent the height of the map, the width of the map and the period that the panel changes, respectively.

The following lines of input contains N lines with M chars for each line. In the map, "." means the empty cell, "*" means the trap which the robot cannot get in, "@" means the initial position of the robot and "$" means the diamond. There is exact one robot and one diamond on the map.

Output

For each test case, output minimum time that the robot can get the diamond. Output "YouBadbad" (without quotes) if it's impossible to get the diamond.

Sample Input
4
3 4 50
@...
***.
$...
5 5 2
.....
..@..
.*...
$.*..
.....
2 3 1
*.@
$.*
5 5 2
*****
..@..
*****
$....
.....
Sample Output

12
4
4
YouBadbad 



1.每一样例开始时,控制板的方向都是0,1,2,3

2.(x,y,pos,t,dd)代表一个状态,pos代表光标的位置,t代表时间,dd代表控制板第一个位置的方向


3.代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

struct node
{
    int x,y,pos,t;
    node() {}
    node(int a,int b,int c,int d):x(a),y(b),pos(c),t(d) {}
};

char map[15][15];
int n,m,p;
int sx,sy,ex,ey;
int dir[4][2];
int vis[12][12][4][1000][4];

void rotate()
{
    int tem0,tem1;
    tem0=dir[3][0];
    tem1=dir[3][1];
    dir[3][0]=dir[2][0];
    dir[3][1]=dir[2][1];
    dir[2][0]=dir[1][0];
    dir[2][1]=dir[1][1];
    dir[1][0]=dir[0][0];
    dir[1][1]=dir[0][1];
    dir[0][0]=tem0;
    dir[0][1]=tem1;
}

int BFS()
{
    //每次的初始方向的是0,1,2,3
    dir[0][0]=0;
    dir[0][1]=-1;
    dir[1][0]=0;
    dir[1][1]=1;
    dir[2][0]=-1;
    dir[2][1]=0;
    dir[3][0]=1;
    dir[3][1]=0;
    queue<node> Q;
    memset(vis,0,sizeof(vis));
    Q.push(node(sx,sy,0,0));
    vis[sx][sy][0][0][0]=1;
    int tt=0;
    int ddd=0;//记录当前控制板的第一个位置的方向
    node a,b;
    while(!Q.empty())
    {
        a=Q.front();
        if(a.t==tt+1&&b.t%p==0)//rotate
        {
            rotate();
            ddd--;
            if(ddd==-1)
                ddd=3;

        }
        tt=a.t;//记录的是前一个状态的时间
        Q.pop();
        b=a;
        //do nothing
        b.t++;
        if(b.x==ex&&b.y==ey)
        {
            return b.t;
        }
        if(!vis[b.x][b.y][b.pos][b.t][ddd])
        {
            Q.push(b);
            vis[b.x][b.y][b.pos][b.t][ddd]=1;
        }
        //move right
        b=a;
        b.t++;
        b.pos++;
        if(b.pos==4)
            b.pos=0;
        if(b.x==ex&&b.y==ey)
        {
            return b.t;
        }
        if(!vis[b.x][b.y][b.pos][b.t][ddd])
        {
            Q.push(b);
            vis[b.x][b.y][b.pos][b.t][ddd]=1;
        }
        //move left
        b=a;
        b.t++;
        b.pos--;
        if(b.pos==-1)
            b.pos=3;
        if(b.x==ex&&b.y==ey)
        {
            return b.t;
        }
        if(!vis[b.x][b.y][b.pos][b.t][ddd])
        {
            Q.push(b);
            vis[b.x][b.y][b.pos][b.t][ddd]=1;
        }
        //press
        b=a;
        b.t++;
        b.x=a.x+dir[a.pos][0];
        b.y=a.y+dir[a.pos][1];
        if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&map[b.x][b.y]!='*')
        {
            if(b.x==ex&&b.y==ey)
            {
                return b.t;
            }
            if(!vis[b.x][b.y][b.pos][b.t][ddd])
            {
                Q.push(b);
                vis[b.x][b.y][b.pos][b.t][ddd]=1;
            }
        }
    }
    return -1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&p);
        for(int i=0; i<n; i++)
        {
            scanf("%s",map[i]);
            for(int j=0; j<m; j++)
            {
                if(map[i][j]=='@')
                {
                    sx=i;
                    sy=j;
                }
                if(map[i][j]=='$')
                {
                    ex=i;
                    ey=j;
                }
            }
        }
        int ans=BFS();
        if(ans==-1)
            printf("YouBadbad\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值