-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumberMaze_UVA929_AC.cpp
95 lines (81 loc) · 2.19 KB
/
numberMaze_UVA929_AC.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
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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("numberMaze_UVA929.in");
ofstream fout("numberMaze_UVA929.out");
const int INF = 1000000;
struct node
{
int y;
int x;
int w;
bool operator<(const node & temp) const
{
return w > temp.w;
}
};
bool g(int y, int x, int n, int m)
{
return y >= 0 && y <= n - 1 && x >= 0 && x <= m - 1;
}
int main()
{
vector<vector<int>> move = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int T;
fin >> T;
for (int t = 1; t <= T; ++t)
{
int n, m, first;
fin >> n >> m;
vector<vector<bool>> visit(n, vector<bool>(m, false));
vector<vector<int>> dp(n, vector<int>(m, INF)), a(n, vector<int>(m));
for (int y = 0; y <= n - 1; ++y)
{
for (int x = 0; x <= m - 1; ++x)
{
fin >> a[y][x];
}
}
dp[0][0] = a[0][0];
priority_queue<node> q;
node start{0, 0, a[0][0]};
q.push(start);
while (!q.empty())
{
node now = q.top();
q.pop();
if (visit[now.y][now.x] == false)
{
visit[now.y][now.x] = true;
for (int i = 0; i <= 3; ++i)
{
node next{now.y + move[i][0], now.x + move[i][1], 0};
if (g(next.y, next.x, n, m) == true)
{
if (visit[next.y][next.x] == false)
{
next.w = dp[now.y][now.x] + a[next.y][next.x];
if (next.w < dp[next.y][next.x])
{
dp[next.y][next.x] = next.w;
q.push(next);
}
}
}
}
}
}
fout << dp[n - 1][m - 1] << '\n';
}
return 0;
}