Skip to content

Commit c05a6e5

Browse files
committed
推箱子游戏2+广度搜索
1 parent a019e79 commit c05a6e5

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*************************************************************************
2+
> File Name: OJ308推箱子游戏2.cpp
3+
> Author: ltw
4+
> Mail: 3245849061@qq.com
5+
> Github: https://github.com/hello-sources
6+
> Created Time: Wed 15 Jul 2020 09:41:15 AM CST
7+
************************************************************************/
8+
9+
#include <iostream>
10+
#include <queue>
11+
#include <algorithm>
12+
using namespace std;
13+
14+
struct node {
15+
int x, y, z;
16+
};
17+
18+
char c[2];
19+
int x, y, d[205][205][3], ans;
20+
int v[205][205][3];
21+
int nx[3][4] = {
22+
-2, 1, 0, 0,
23+
-1, 1, 0, 0,
24+
-1, 2, 0, 0
25+
};
26+
int ny[3][4] = {
27+
0, 0, -2, 1,
28+
0, 0, -1, 2,
29+
0, 0, -1, 1
30+
};
31+
int nz[3][4] = {
32+
2, 2, 1, 1,
33+
1, 1, 0, 0,
34+
0, 0, 2, 2
35+
};
36+
node temp, next;
37+
queue<node> q;
38+
39+
int calc(int x, int y) {
40+
int num = 0;
41+
if (x < 0 || x > 200 || y < 0 || y > 200) {
42+
if (x > 200) {
43+
int k = (x - 200) / 3 + 2;
44+
num += k * 2;
45+
x -= k * 3;
46+
}
47+
if (y > 200) {
48+
int k = (y - 200) / 3 + 2;
49+
num += k * 2;
50+
y -= k * 3;
51+
}
52+
}
53+
return num + d[x][y][0];
54+
}
55+
56+
int main() {
57+
v[100][100][0] = 1;
58+
q.push({100, 100, 0});
59+
while (!q.empty()) {
60+
node temp = q.front();
61+
q.pop();
62+
for (int i = 0; i < 4; i++) {
63+
int x = temp.x + nx[temp.z][i];
64+
int y = temp.y + ny[temp.z][i];
65+
if (x < 0 || x > 200 || y < 0 || y > 200) continue;
66+
if (v[x][y][nz[temp.z][i]]) continue;
67+
q.push({x, y, nz[temp.z][i]});
68+
v[x][y][nz[temp.z][i]] = 1;
69+
d[x][y][nz[temp.z][i]] = d[temp.x][temp.y][temp.z] + 1;
70+
}
71+
}
72+
while (cin >> c >> x >> y) {
73+
x += 100;
74+
y += 100;
75+
ans = (c[0] == 'U' ? calc(x, y) : 2100000000);
76+
if (c[0] == 'H') {
77+
for (int i = -2; i <= 2; i++) {
78+
ans = min(ans, calc(x + i, y - 1) + abs(i) + 1);
79+
ans = min(ans, calc(x + i, y + 2) + abs(i) + 1);
80+
}
81+
} else if (c[0] == 'V') {
82+
for (int i = -2; i <= 2; i++) {
83+
ans = min(ans, calc(x - 1, y + i) + abs(i) + 1);
84+
ans = min(ans, calc(x + 2, y + i) + abs(i) + 1);
85+
}
86+
}
87+
cout << ans << endl;
88+
}
89+
return 0;
90+
}

0 commit comments

Comments
 (0)