-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22255.cpp
64 lines (55 loc) · 1.64 KB
/
22255.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
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
struct Node {
int weight, x, y, count;
};
struct Compare {
bool operator()(const Node& m1, const Node& m2) {
return m1.weight >= m2.weight;
}
};
int main()
{
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
int N, M; cin >> N >> M;
int SX, SY, EX, EY; cin >> SX >> SY >> EX >> EY;
int map[N+1][M+1];
for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) cin >> map[i][j];
int weights[N+1][M+1][3];
for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) for (int k = 0; k < 3; k++) weights[i][j][k] = 1e8;
queue<Node> q;
q.push({0, SX, SY, 1});
while (q.size()) {
Node n = q.front();
q.pop();
int xxxx[3][4] = {
{0, -1, 0, 1},
{-1, 1, 0, 0},
{0, 0, 0, 0}
};
int yyyy[3][4] = {
{1, 0, -1, 0},
{0, 0, 0, 0},
{1, -1, 0, 0}
};
int d = n.count % 3;
for (int i = 0; i < 4; i++) {
int nx = n.x + xxxx[d][i];
int ny = n.y + yyyy[d][i];
if (nx == n.x && ny == n.y) continue;
if (nx < 1 || nx > N || ny < 1 || ny > M || map[nx][ny] == -1) continue;
if (weights[nx][ny][d] > n.weight + map[nx][ny]) {
weights[nx][ny][d] = n.weight + map[nx][ny];
q.push({n.weight + map[nx][ny], nx, ny, n.count + 1});
}
}
}
int result = 1e8;
for (int i = 0; i < 3; i++) result = min(result, weights[EX][EY][i]);
cout << (result == 1e8 ? -1 : result);
return 0;
}