我是靠谱客的博主 文静柜子,这篇文章主要介绍UVa 314 - Robot (bfs),现在分享给大家,希望可以做个参考。

题意

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

思路

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

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

代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部