-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path318.cpp
More file actions
91 lines (79 loc) · 1.75 KB
/
Copy path318.cpp
File metadata and controls
91 lines (79 loc) · 1.75 KB
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
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
short dx[4] = { 0, 1, 0, -1 };
short dy[4] = { -1, 0, 1, 0 };
bool done[130][130];
int n;
struct point {
int x,y,dist;
int data;
void toString() {
cout << "(" << x << "," << y << ") data: " << data << " dist: " << dist << " ";
}
};
struct compare {
bool operator()(const point & a, const point & b) {
return a.dist > b.dist;
}
};
int main() {
point graph[130][130];
int prob = 0;
while (1) {
prob++;
memset(done, 0, sizeof(done));
priority_queue <point, vector<point>, compare> edge;
cin >> n;
if (n == 0) {
break;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> graph[i][j].data;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
graph[i][j].x = i;
graph[i][j].y = j;
if (i == 0 && j == 0) {
graph[i][j].dist = graph[0][0].data;
}
else {
graph[i][j].dist = 2147483647;
}
edge.push(graph[i][j]);
}
}
/*
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
graph[i][j].toString();
}
cout << endl;
}
*/
while (!edge.empty()) {
point pt = edge.top();
edge.pop();
if (pt.x == n - 1 && pt.y == n - 1) {
cout << "Problem " << prob << ": " << pt.dist << endl;
break;
}
if (done[pt.x][pt.y]) {
continue;
}
done[pt.x][pt.y] = true;
for (int i = 0; i < 4; i++) {
if (pt.x + dx[i] >= 0 && pt.x + dx[i] < n && pt.y + dy[i] >= 0 && pt.y + dy[i] < n
&& pt.dist + graph[pt.x + dx[i]][pt.y + dy[i]].data < graph[pt.x + dx[i]][pt.y + dy[i]].dist) {
graph[pt.x + dx[i]][pt.y + dy[i]].dist = pt.dist + graph[pt.x + dx[i]][pt.y + dy[i]].data;
edge.push(graph[pt.x + dx[i]][pt.y + dy[i]]);
}
}
}
}
return 0;
}