zoukankan      html  css  js  c++  java
  • ZOJ校赛——BFS——Superbot

    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)

    #include<cstdio>
    #include<cstring>
    #include<queue>
    char map[12][12];
    int n, m, P, sx, sy, ex, ey;
    int dp[12][12][4];
    int dp2[12][12][4];
    int dir[4][4][2] = {
    {{0, -1},{0, 1},{-1, 0},{1, 0}},
    {{1, 0},{0, -1},{0, 1},{-1, 0}},
    {{-1, 0},{1, 0},{0, -1},{0, 1}},
    {{0, 1},{-1, 0},{1, 0},{0, -1}}
    };
    std::queue<int>q;
    bool ok(int x, int y, int pos){
        if(x < 0 || x >= n ||
           y < 0 || y >= m ||
           map[x][y] == '*'||
           dp[x][y][pos] != -1)
           return false;
        else
        return true;   
    }
    void bfs(){
        int x, y, nx, ny, pos, npos;
        q.push(sx);
        q.push(sy);
        q.push(0);
        while(!q.empty()){
            x = q.front();
            q.pop();
            y = q.front();
            q.pop();
            pos = q.front();
            q.pop();
            if(dp2[x][y][pos] == -1){
                dp2[x][y][pos] = dp[x][y][pos];
            }
            nx = x + dir[dp[x][y][pos] / P % 4][pos][0];
            ny = y + dir[dp[x][y][pos] / P % 4][pos][1];
            if(ok(nx, ny, pos)){
                dp[nx][ny][pos] = dp[x][y][pos] + 1;
                q.push(nx);
                q.push(ny);
                q.push(pos);
            }
            npos = pos + 1;
            if(npos >= 4)
                npos = 0;
            if(ok(x, y, npos)){
                dp[x][y][npos] = dp[x][y][pos] + 1;
                q.push(x);
                q.push(y);
                q.push(npos);
            }
            npos = pos - 1;
            if(npos < 0)
                npos = 3;
            if(ok(x, y, npos)){
                dp[x][y][npos] = dp[x][y][pos] + 1;
                q.push(x);
                q.push(y);
                q.push(npos);
            }
            if(dp[x][y][pos] <= 200){
                dp[x][y][pos]++;
                q.push(x);
                q.push(y);
                q.push(pos);
            }
        }
        return ;
    }
    int main()
    {
        int T, min;
        scanf("%d", &T);
        while(T--){
            scanf("%d%d%d", &n , &m, &P);
            memset(dp, -1, sizeof(dp));
            memset(dp2, -1, sizeof(dp2));
            for(int i = 0; i < n; i++){
                scanf("%s", map[i]);
            }
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    if(map[i][j] == '@'){
                        sx = i;
                        sy = j;
                    }
                    else if(map[i][j] == '$'){
                        ex = i;
                        ey = j;
                    }
                }
            }
            dp[sx][sy][0] = 0;
            bfs();
            min = 0x3f3f3f3f;
            for(int i = 0; i < 4; i++){
                if(dp2[ex][ey][i] == -1)
                    continue;
                min = min < dp2[ex][ey][i] ? min : dp2[ex][ey][i];
            }
            if(min == 0x3f3f3f3f){
                printf("YouBadbad
    ");
            }
            else{
                printf("%d
    ", min);
            }
        }
    }
    View Code
  • 相关阅读:
    [Swift通天遁地]七、数据与安全-(10)文件的加密压缩和解压加密压缩
    [Swift通天遁地]七、数据与安全-(9)文件的压缩和解压
    [Swift]LeetCode406. 根据身高重建队列 | Queue Reconstruction by Height
    [Swift]LeetCode403. 青蛙过河 | Frog Jump
    [Swift]LeetCode402. 移掉K位数字 | Remove K Digits
    [Swift]LeetCode399. 除法求值 | Evaluate Division
    [Swift通天遁地]七、数据与安全-(8)创建普通PDF文档和加密PDF文档
    Singleton单例模式
    Could not read from remote repository.
    Hadoop Web项目--Friend Find系统
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4422837.html
Copyright © 2011-2022 走看看