-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot_in_a_grid.cpp
88 lines (73 loc) · 2.16 KB
/
Robot_in_a_grid.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
#include<vector>
#include<math.h>
#include<iostream>
#define _MAX(x,y) (x > y? x : y)
using namespace std;
const int ROW = 10;
const int COL = 10;
const int BLOCKER = 0;
typedef struct {
int row;
int col;
int value;
} path_node;
int a_grid[ROW][COL] = {
{1,2,5,6,3,1,5,6,4,5},
{3,2,5,6,0,0,5,6,4,5},
{1,2,5,0,3,0,5,6,4,5},
{1,2,5,6,0,1,5,6,4,5},
{1,2,5,0,3,2,5,6,4,5},
{1,2,5,0,3,3,5,6,4,5},
{1,2,5,6,0,6,5,6,4,5},
{1,2,5,6,3,9,5,0,4,5},
{1,2,5,6,3,9,0,6,0,5},
{1,2,5,0,3,0,5,6,4,5},
};
int compute_path_lengh(vector<path_node> list) {
vector<path_node>::const_iterator iter;
int max = 0;
for (iter = list.begin(); iter < list.end(); iter++) {
max += (*iter).value;
}
return max;
}
void print_path(vector<path_node> list) {
for (size_t i = 0; i < list.size(); i++) {
cout << i << ":" << list[i].row << ',' << list[i].col << ',' << list[i].value << "; ";
}
cout << endl;
}
bool has_end_point(vector<path_node> list, int n_r, int m_c) {
size_t size = list.size();
return size > 0 && (list[size - 1]).col == m_c - 1 && (list[size - 1]).row == n_r - 1;
}
vector<path_node> get_path(int a[ROW][COL], int n_r, int m_c, int r, int c) {
int max_r = 0;
int max_c = 0;
int max_t = 0;
vector<path_node> list_path_r;
vector<path_node> list_path_c;
vector<path_node> list_path;
if (c < m_c - 1 && a[r][c + 1] != BLOCKER) {
list_path_c = get_path(a, n_r, m_c, r, c + 1);
}
if (r < n_r - 1 && a[r + 1][c] != BLOCKER) {
list_path_r = get_path(a, n_r, m_c, r + 1, c);
}
max_r = has_end_point(list_path_r, n_r, m_c) ? compute_path_lengh(list_path_r) : 0;
max_c = has_end_point(list_path_c, n_r, m_c) ? compute_path_lengh(list_path_c) : 0;
max_t = a[r][c] + _MAX(max_r, max_c);
if ((max_r > 0 || max_c > 0) || (r == n_r - 1) && (c == m_c - 1)) {
list_path = max_c > max_r ? list_path_c : list_path_r;
list_path.insert(list_path.begin(), { r, c, a[r][c] });
}
//cout << "r:" << r << ";c:" << c << ";max_t:" << max_t << endl;
return list_path;
}
int robot_in_a_grid() {
vector<path_node> list_path = get_path(a_grid, 10, 10, 0, 0);
int max_l = compute_path_lengh(list_path);
print_path(list_path);
cout << endl << "max_l=" << max_l << endl;
return max_l;
}