概述
题意
一个机器人,要从起点走到终点,每一秒能左转、右转、前进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("%dn", bfs());
}
return 0;
}
最后
以上就是文静柜子为你收集整理的UVa 314 - Robot (bfs)的全部内容,希望文章能够帮你解决UVa 314 - Robot (bfs)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复