ZOJ 3865 Superbot(广搜)

浙大校赛的一道搜索题。

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865

Superbot

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 2ndmoves 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 NM (2 <= NM <= 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)


题目大意:一个机器人,需要找到从起点到终点的最短路径,一共有四个选项分别代表上下左右(初始值为1左2右3上4下)每隔p秒这四个选项会对应的串动一位,而机器人只能走光标对应的方向。每秒钟你可以有四种可选动作:

1.按下按键并向你当前光标所对应的方向走一步

2.左移一下光标(当光标为1st时,左移会指向4th),并在原地等待

3.右移一下光标,并在原地等待

4.仅在原地等待(但随着秒数的推移,选项可能会发生移动)

于是我们可以开一个四维的vis数组来避免状态重复,分别是x坐标,y坐标,当前光标所选择的方向以及秒数对p取模之后的进度。

剩下的暴搜即可。

AC代码:

#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <limits.h>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef double DB;
const int INF = INT_MAX;
int t, n, m, p;
int sx, sy, ex, ey;
char maze[20][20];
int vis[20][20][8][55];
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};
struct node {
    int x, y, d, block, step;
    node() {}
    node(int x1, int y1, int d1, int block1, int step1) {
        x = x1, y = y1, d = d1, block = block1, step = step1;
    }
};
int judgeD(int x, int y) {
    if(x == 0 && y == -1) return 0;//左
    else if(x == 0 && y == 1) return 1;//右
    else if(x == -1 && y == 0) return 2;//上
    else if(x == 1 && y == 0) return 3;//下
}
bool isok(int x, int y) {
    if(x > 0 && y > 0 && x <= n && y <= m && maze[x][y] != '*') return true;
    return false;
}
int BFS() {
    memset(vis, 0, sizeof(vis));
    queue<node> q;
    q.push(node(sx, sy, 0, 0, 0));
    vis[sx][sy][0][0] = 1;
    int cnt  = 0;
    while(!q.empty()) {
        node tmp = q.front();
        q.pop();
        if(tmp.x == ex && tmp.y == ey) return tmp.step;
        //点击确定,直走的情况
        node a;
        int nx = dir[(tmp.step / p) % 4][tmp.block][0];
        int ny = dir[(tmp.step / p) % 4][tmp.block][1];
        a.d = judgeD(nx, ny);
        a.x = tmp.x + nx, a.y = tmp.y + ny;
        a.step = tmp.step + 1;
        a.block = tmp.block;
        if(isok(a.x, a.y) && !vis[a.x][a.y][a.d][a.step%p]) {
            vis[a.x][a.y][a.d][a.step%p] = 1;
            q.push(a);
        }
        //左移一个块的情况
        node b;
        b.block = (4 + tmp.block - 1) % 4;
        nx = dir[(tmp.step / p) % 4][b.block][0];
        ny = dir[(tmp.step / p) % 4][b.block][1];
        b.d = judgeD(nx, ny);
        b.x = tmp.x, b.y = tmp.y;
        b.step = tmp.step + 1;
        if(!vis[b.x][b.y][b.d][b.step%p]) {
            vis[b.x][b.y][b.d][b.step%p] = 1;
            q.push(b);
        }

        //右移一个块的情况
        node c;
        c.block = (4 + tmp.block + 1) % 4;
        nx = dir[(tmp.step / p) % 4][c.block][0];
        ny = dir[(tmp.step / p) % 4][c.block][1];
        c.d = judgeD(nx, ny);
        c.x = tmp.x, c.y = tmp.y;
        c.step = tmp.step + 1;
        if(!vis[c.x][c.y][c.d][c.step%p]) {
            vis[c.x][c.y][c.d][c.step%p] = 1;
            q.push(c);
        }
        //原地等待
        node e = tmp;
        e.step = tmp.step + 1;
        nx = dir[(tmp.step / p) % 4][e.block][0];
        ny = dir[(tmp.step / p) % 4][e.block][1];
        e.d = judgeD(nx, ny);
        if(!vis[e.x][e.y][e.d][e.step%p]) {
            vis[e.x][e.y][e.d][e.step%p] = 1;
            q.push(e);
        }
    }
    return -1;
}
int main() {
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &n, &m, &p);
        for(int i = 1; i <= n; i++) {
            scanf("%s", maze[i]+1);
            for(int j = 1; j <= m; j++) {
                if(maze[i][j] == '@') {
                    sx = i, sy = j;
                } else if(maze[i][j] == '$') {
                    ex = i, ey = j;
                }
            }
        }
        int ans = BFS();
        if(ans == -1) puts("YouBadbad");
        else printf("%d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值