Superbot ZOJ - 3865 (BFS)

Superbot

ZOJ - 3865

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

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; 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) and P (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

Hint

For the first example:
0s: start
1s: cursor move right (cursor is at "right")
2s: press button (robot move right)
3s: press button (robot move right)
4s: press button (robot move right)
5s: cursor move right (cursor is at "up")
6s: cursor move right (cursor is at "down")
7s: press button (robot move down)
8s: press button (robot move down)
9s: cursor move right (cursor is at "left")
10s: press button (robot move left)
11s: press button (robot move left)
12s: press button (robot move left)

For the second example:
0s: start
1s: press button (robot move left)
2s: press button (robot move left)
--- panel rotated ---
3s: press button (robot move down, without changing cursor)
4s: press button (robot move down)

For the third example:
0s: start
1s: press button (robot move left)
--- panel rotated ---
2s: press button (robot move down)
--- panel rotated ---
3s: cursor move left (cursor is at "right")
--- panel rotated ---
4s: press button (robot move left)


有t组输入

每组输入n, m, p。表示一个n*m的地图,每p秒按键会右移一次(这个等会儿再讲)。

然后是地图的输入。其中'@'为起点,'$'为终点,'.'为通路,'*'为不通。

问从起点到终点最少需要多久?

注意!

每一秒有4种操作,注意,不是三种,1. 光标左移,2. 光标右移,3. 按照光标方向行走,4. 不动(尤其是这一个)。

所谓光标左移,右移,因为题目中给出了光标(在图上,共4个方向),每次改变的移动方向只能是当前光标选择的方向的左右两个。而行走只能按照光标所指的方向。

另外每过p秒,方向会变化(初始四个方向的顺序为左右上下,p秒后变成右上下左,再过p秒后变为上下左右)。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 20;
struct node{
    int x,y,dir,t;
};
int go[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
int n,m,P;
char mp[N][N];
bool vis[N][N];
bool vvis[N][N][5];
int change(int a){
    switch(a){
    case 0:
        return 3;
    case 1:
        return 0;
    case 2:
        return 1;
    case 3:
        return 2;
    }
}
void bfs(){
    node p;
    bool flag = false;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(mp[i][j] == '@'){
                p.x = i;
                p.y = j;
                flag = true;
                break;
            }
        }
        if(flag) break;
    }
    p.dir = 0;
    p.t = 0;
    vvis[p.x][p.y][p.dir] = 1;
    queue<node>que;
    que.push(p);
    while(!que.empty()){
        p = que.front();
        que.pop();
        int xx = p.x + go[p.dir][0];
        int yy = p.y + go[p.dir][1];
        if(xx >= 0 && xx < n && yy >= 0 && yy < m){
            if(!vis[xx][yy]){
                if(mp[xx][yy] == '.'){
                    vis[xx][yy] = 1;
                    node q;
                    q.x = xx;
                    q.y = yy;
                    q.t = p.t + 1;
                    q.dir = p.dir;
                    if(q.t % P == 0){
                        q.dir = change(q.dir);
                        vvis[xx][yy][q.dir] = 1;
                    }
                    que.push(q);
                }
                else if(mp[xx][yy] == '$'){
                    printf("%d\n",p.t + 1);
                    return;
                }
             }
        }
        if((p.t + 1) % P == 0)
            p.dir = change(p.dir);
        node q;
        q.x = p.x;
        q.y = p.y;
        q.t = p.t + 1;

        q.dir = (p.dir + 1) % 4;
        if(!vvis[q.x][q.y][q.dir]){
            que.push(q);
            vvis[q.x][q.y][q.dir] = 1;
        }
        q.dir = (p.dir + 3) % 4;
        if(!vvis[q.x][q.y][q.dir]){
            que.push(q);
            vvis[q.x][q.y][q.dir] = 1;
        }
        q.dir = p.dir;
        if(!vvis[q.x][q.y][q.dir]){
            que.push(q);
            vvis[q.x][q.y][q.dir] = 1;
        }
    }
    printf("YouBadbad\n");
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        memset(vis,0,sizeof(vis));
        memset(vvis,0,sizeof(vvis));
        scanf("%d%d%d",&n,&m,&P);
        for(int i = 0; i < n; i++){
            scanf("%s",mp[i]);
        }
        bfs();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/80085624
ZOJ