-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathObstacle Course.cpp
More file actions
92 lines (85 loc) · 1.95 KB
/
Obstacle Course.cpp
File metadata and controls
92 lines (85 loc) · 1.95 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
92
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
#define MAXIMUM 126
#define INFINITY 1000000
#define CONFIRMED -1
int Q[MAXIMUM][MAXIMUM];
bool isFinished(int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (Q[i][j] != CONFIRMED) {
return false;
}
}
}
return true;
}
int main() {
int size;
int count = 0;
int input[MAXIMUM][MAXIMUM];
int output[MAXIMUM][MAXIMUM];
cin >> size;
while (size != 0) {
count++;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cin >> input[i][j];
}
}
for (int i = 0; i<size; i++)
for (int j = 0; j<size; j++)
Q[i][j] = INFINITY;
//memset(Q, INFINITY, sizeof(Q));
Q[0][0] = input[0][0];
while (!isFinished(size)) {
int x, y;
int tmp = INFINITY;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (Q[i][j] != CONFIRMED && Q[i][j] != INFINITY) {
if (Q[i][j] < tmp) {
tmp = Q[i][j];
x = i;
y = j;
}
}
}
}
output[x][y] = tmp;
Q[x][y] = CONFIRMED;
//relaxation
//left
if (x - 1 >= 0 && Q[x - 1][y] != CONFIRMED) {
int t = output[x][y] + input[x - 1][y];
Q[x - 1][y] = t<Q[x - 1][y] ? t : Q[x - 1][y];
}
//right
if (x + 1<size && Q[x + 1][y] != CONFIRMED) {
int t = output[x][y] + input[x + 1][y];
Q[x + 1][y] = t<Q[x + 1][y] ? t : Q[x + 1][y];
}
//up
if (y - 1 >= 0 && Q[x][y - 1] != CONFIRMED) {
int t = output[x][y] + input[x][y - 1];
Q[x][y - 1] = t<Q[x][y - 1] ? t : Q[x][y - 1];
}
//down
if (y + 1<size && Q[x][y + 1] != CONFIRMED) {
int t = output[x][y] + input[x][y + 1];
Q[x][y + 1] = t<Q[x][y + 1] ? t : Q[x][y + 1];
}
}
cout << "Problem " << count << ": " << output[size - 1][size - 1] << endl;
// for (int i = 0; i < size; i++) {
// for (int j = 0; j < size; j++){
// cout << output[i][j] ;
// }
// cout << endl;
// }
cin >> size;
}
return 0;
}