-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_game.cpp
75 lines (62 loc) · 1.27 KB
/
maze_game.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
#include<bits/stdc++.h>
using namespace std;
int maze(int n, int m, int** ans, int i, int j)
{
int l = INT_MAX, r = INT_MAX, u = INT_MAX, d = INT_MAX;
if (i - 1 >= 0) l = ans[i - 1][j];
if (i + 1 < n) r = ans[i + 1][j];
if (j - 1 >= 0) u = ans[i][j - 1];
if (j + 1 < m) d = ans[i][j + 1];
int c = min(l, min(r, min(u, d)));
return c;
}
int main() {
int t;
cin >> t;
while (t--)
{
int n, m;
cin >> n >> m;
int** arr = new int*[n];
int** ans = new int*[n];
for (int i = 0; i < n; i++)
{
arr[i] = new int[m];
ans[i] = new int[m];
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
ans[i][j] = INT_MAX;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (i == 0 && j == 0)
{
ans[i][j] = arr[i][j];
}
else
{
ans[i][j] = arr[i][j] + maze(n, m, ans, i, j);
}
}
}
cout << ans[n - 1][m - 1] << endl;
}
return 0;
}
/**
3
4 5
0 3 11 2 9
7 3 4 9 9
1 7 5 13 3
2 3 4 2 5
1 6
0 1 2 3 4 5
2 3
0 5 9
7 3 2
**/