UVa 314 - Robot (bfs)

题意

一个机器人,要从起点走到终点,每一秒能左转、右转、前进1~3步。
求最短时间。

思路

今天的弱校连萌太凶残了,没办法才来做这题。。

随便搜就行。
这里要注意一下障碍物,因为是正方形的,而且机器人很肥,所以要判断一下每一步的上、左上、左是不是存在障碍物。如果存在的话,就算这一步没障碍物,机器人还是不能走的。同理,机器人不能走地图边缘。

代码

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(p, num) memset(p, num, sizeof(p))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid+1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 50+10;
const int MOD = 1e9+7;
const int dir[][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
const int seed = 131;
int cases = 0;
typedef pair<int, int> pii;

struct POINT
{
    int x, y, dir, time;
    bool operator < (const POINT &a) const
    {
        return time > a.time;
    }
    POINT(int _x, int _y, int _dir, int _time): x(_x), y(_y), dir(_dir), time(_time) {}
};

const int MAXR = 50, MAXC = 50;
const int check_dir[][2] = { {0, -1}, {-1, 0}, {-1, -1} };
priority_queue<POINT> Q;
int vis[MAXR][MAXC][4], mp[MAXN][MAXN];
pii st, ed;
int ini_dir, n, m;

bool check(int x, int y, int d)
{
    if (x <= 0 || y <= 0 || x >= n || y >= m || mp[x][y]) return false;
    for (int i = 0; i < 3; i++)
    {
        int xx = x + check_dir[i][0], yy = y + check_dir[i][1];
        if (xx < 0 || yy < 0 || xx >= n || yy >= m) return false;
        if (mp[xx][yy]) return false;
    }
    return true;
}

int bfs()
{
    Q.push(POINT(st.X, st.Y, ini_dir, 0));
    while (!Q.empty())
    {
        POINT u = Q.top(); Q.pop();
        int x = u.x, y = u.y, cur_dir = u.dir, cur_time = u.time;
        if (x == ed.X && y == ed.Y)
            return cur_time;
        //go straight
        for (int step = 1; step < 4; step++)
        {
            int xx = x + step * dir[cur_dir][0], yy = y + step * dir[cur_dir][1];
            if (!check(xx, yy, cur_dir)) break;
            if (vis[xx][yy][cur_dir]) continue;
            vis[xx][yy][cur_dir] = 1;
            Q.push(POINT(xx, yy, cur_dir, cur_time+1));
        }
        //turn around
        int new_dir = (cur_dir-1+4) % 4;
        if (!vis[x][y][new_dir])
        {
            vis[x][y][new_dir] = 1;
            Q.push(POINT(x, y, new_dir, cur_time+1));
        }
        new_dir = (cur_dir+1) % 4;
        if (!vis[x][y][new_dir])
        {
            vis[x][y][new_dir] = 1;
            Q.push(POINT(x, y, new_dir, cur_time+1));
        }
    }
    return -1;
}

void init()
{
    while (!Q.empty()) Q.pop();
    MS(vis, 0);
}

int main()
{
    //ROP;
    map<string, int> smp;
    smp["north"] = 0; smp["east"] = 1; smp["south"] = 2; smp["west"] = 3;
    while (scanf("%d%d", &n, &m), n+m)
    {
        init();
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++) scanf("%d", &mp[i][j]);
        scanf("%d%d%d%d", &st.X, &st.Y, &ed.X, &ed.Y);
        string tmp;
        cin >> tmp;
        ini_dir = smp[tmp];
        vis[st.X][st.Y][ini_dir] = 1;
        printf("%d\n", bfs());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值