forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrasshopper.cpp
55 lines (45 loc) · 1.55 KB
/
grasshopper.cpp
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int INF = (1 << 31);
bool inrange(int w, int h, int x, int y) {
return (x >= 0 && y >= 0 && x < w && y < h);
}
int main() {
int x, y, startx, starty, endx, endy;
while(cin >> x && cin >> y && cin >> startx && cin >> starty && cin >> endx && cin >> endy) {
vector<vector<int>> v;
v.resize(x, vector<int>(y, INF));
v[startx-1][starty-1] = 0;
queue<pair<int, int>> q;
q.push({startx-1, starty-1});
while(!q.empty()) {
int currx = q.front().first;
int curry = q.front().second;
int currval = v[currx][curry];
q.pop();
vector<pair<int, int>> modifiers;
modifiers.push_back({-2, -1});
modifiers.push_back({-1, -2});
modifiers.push_back({+2, -1});
modifiers.push_back({+1, -2});
modifiers.push_back({-2, +1});
modifiers.push_back({-1, +2});
modifiers.push_back({+2, +1});
modifiers.push_back({+1, +2});
for(auto m : modifiers) {
if(inrange(x, y, currx+m.first, curry+m.second) && v[currx+m.first][curry+m.second] == INF) {
v[currx+m.first][curry+m.second] = currval+1;
q.push({currx+m.first, curry+m.second});
}
}
}
if(v[endx-1][endy-1] == INF) {
cout << "impossible" << endl;
}
else {
cout << v[endx-1][endy-1] << endl;
}
}
}