-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_wood.cpp
101 lines (71 loc) · 1.37 KB
/
max_wood.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
96
97
98
99
100
101
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct posn {
int row;
int col;
bool right;
posn() {
row = 0;
col = 0;
right = true;
}
};
struct garden {
char obj;
int m_wood;
garden() {
m_wood = -1;
}
};
int max_wood( garden arr[][220], int m, int n, posn g);
int value(garden arr[][220], posn g );
int main()
{
int m;
int n;
int t;
int dist;
cin >> t;
for ( int k = 0; k < t; k++ ) {
dist = 0;
cin >> m >> n;
garden arr[m][220];
posn g; // gardener
for ( int i = 0; i < m; i++ ) {
for ( int j = 0; j < n; j++ ) {
cin >> arr[i][j].obj;
}
}
dist = max_wood( arr, m, n, g);
cout<< dist << endl;
}
return 0;
}
int value(garden arr[][220], posn g ) {
return ( arr[g.row][g.col].obj == 'T' ) ? 1 : 0;
}
int max_wood( garden arr[][220], int m, int n, posn g ) {
int d1;
int d2;
if ( g.row >= m || g.col >= n || g.row < 0 || g.col < 0 || arr[g.row][g.col].obj == '#' ) {
return 0;
}
if ( arr[g.row][g.col].m_wood != -1 ) {
return arr[g.row][g.col].m_wood;
}
posn g1 = g;
posn g2 = g;
if ( g.right ) {
g1.col++;
} else {
g1.col--;
}
g2.row++;
g2.right = ( g2.right == true ) ? false : true;
d1 = value(arr, g ) + max_wood( arr, m, n, g1 );
d2 = value(arr, g) + max_wood( arr, m, n, g2 );
arr[g.row][g.col].m_wood = ( d1 > d2 ? d1 : d2 );
return ( d1 > d2 ? d1 : d2 );
}