ZOJ - 3865 Superbot 【BFS】

题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865

思路

一个迷宫题 但是每次的操作数和普通的迷宫题不一样

0.按下按钮 即向当前方向盘的方向前进一格
1.往左移动方向盘
2.往右移动方向盘
3.不动

然后记录时间

易知,可以从四个方向到达终点,最后从这四个方向找最小值就是答案

当然要判断 不能通达的结果

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = acos(-1);
const double E = exp(1);
const double eps = 1e-30;

const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 5;
const int MOD = 1e9 + 7;

char dis[4][4] =
{
    'l', 'r', 'u', 'd',
    'd', 'l', 'r', 'u',
    'u', 'd', 'l', 'r',
    'r', 'u', 'd', 'l',
};

map <char, int> M;

int Move[4][2]
{
    -1, 0, // up
    1, 0, //down
    0,-1, // left
    0, 1, //right
};

char G[15][15];

int n, m, p;

int dp[12][12][4];
int ans[12][12][4];

struct Node
{
    int x, y, d;
}tmp;

queue <Node> q;

int sx, sy, ex, ey;

bool ok(int x, int y, int d)
{
    if (x < 0 || x >= n || y < 0 || y >= m)
        return false;
    if (G[x][y] == '*' || dp[x][y][d] != -1)
        return false;
    return true;
}

void bfs()
{
    while (!q.empty())
    {
        int x = q.front().x;
        int y = q.front().y;
        int d = q.front().d;
        q.pop();
        if (x == ex && y == ey && ans[x][y][d] == -1)
            ans[x][y][d] = dp[x][y][d];
        int nx = x + Move[M[dis[dp[x][y][d] / p % 4][d]]][0];
        int ny = y + Move[M[dis[dp[x][y][d] / p % 4][d]]][1];
        // press botton
        if (ok(nx, ny, d))
        {
            dp[nx][ny][d] = dp[x][y][d] + 1;
            tmp.x = nx;
            tmp.y = ny;
            tmp.d = d;
            q.push(tmp);
        }

        // move left
        int nd = d - 1;
        if (nd < 0)
            nd = 3;
        if (ok(x, y, nd))
        {
            dp[x][y][nd] = dp[x][y][d] + 1;
            tmp.x = x;
            tmp.y = y;
            tmp.d = nd;
            q.push(tmp);
        }

        // move right
        nd = d + 1;
        if (nd > 3)
            nd = 0;
        if (ok(x, y, nd))
        {
            dp[x][y][nd] = dp[x][y][d] + 1;
            tmp.x = x;
            tmp.y = y;
            tmp.d = nd;
            q.push(tmp);
        }

        // stay
        if (dp[x][y][d] <= 200)
        {
            dp[x][y][d]++;
            tmp.x = x;
            tmp.y = y;
            tmp.d = d;
            q.push(tmp);
        }
    }
}

void init()
{
    M['u'] = 0;
    M['d'] = 1;
    M['l'] = 2;
    M['r'] = 3;
}

int main()
{
    init();
    int T;
    cin >> T;
    while (T--)
    {
        memset(dp, -1, sizeof(dp));
        memset(ans, -1, sizeof(ans));
        scanf("%d%d%d", &n, &m, &p);
        int x = -1, y = -1;
        for (int i = 0; i < n; i++)
        {
            scanf("%s", &G[i]);
            for (int j = 0; j < m; j++)
            {
                if (G[i][j] == '@')
                {
                    sx = i;
                    sy = j;
                }
                else if (G[i][j] == '$')
                {
                    ex = i;
                    ey = j;
                }
            }
        }
        dp[sx][sy][0] = 0;
        tmp.x = sx;
        tmp.y = sy;
        tmp.d = 0;
        q.push(tmp);
        bfs();
        int Ans = INF;
        for (int i = 0; i < 4; i++)
        {
            if (ans[ex][ey][i] != -1)
                Ans = min(Ans, ans[ex][ey][i]);
        }
        if (Ans != INF)
            printf("%d\n", Ans);
        else
            printf("YouBadbad\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值