-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_gold_halfway.cpp
More file actions
66 lines (60 loc) · 1.69 KB
/
collect_gold_halfway.cpp
File metadata and controls
66 lines (60 loc) · 1.69 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
#include <bits/stdc++.h>
using namespace std;
// ‘#’ means blocked path
// ‘.’ means walkable path
// ‘*’ means points that you have to collect
void dfs(vector<vector<char>> &A, int x, int y, bool down, int coin, int &max_coin){
int n = A.size(), m = A[0].size(), x1, y1;
if(down && x == n-1 && y == n-1){
max_coin = max(max_coin, coin);
}
if(!down && x == 0 && y == 0){
max_coin = max(max_coin, coin);
}
// if(x < 0 && x >= n && y < 0 && y >= m) return;
int xs[2];
int ys[2];
if(down == true){ // (i,j) -> (i+1, j) && (i, j+1)
xs[0] = 1, xs[1] = 0;
ys[0] = 0, ys[1] = 1;
}
else{// (i,j) -> (i-1, j) && (i, j-1)
xs[0] = -1, xs[1] = 0;
ys[0] = 0, ys[1] = -1;
}
x1 = x+xs[0], y1 = y+ys[0];
if(x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && A[x1][y1] != '#'){
if(A[x1][y1] == '*') coin++;
A[x1][y1] = '#';
dfs(A, x1, y1, down, coin, max_coin);
}
x1 = x+xs[1], y1 = y+ys[1];
if(x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && A[x1][y1] != '#'){
if(A[x1][y1] == '*') coin++;
A[x1][y1] = '#';
dfs(A, x1, y1, down, coin, max_coin);
}
}
int collect_gold(vector<vector<char>> A, int n, int m){
int c1 = 0, c2 = 0;
dfs(A, 0, 0, true, 0, c1);
dfs(A, n-1, m-1, false, 0, c2);
cout<<c1<<" "<<c2<<endl;
return c1+c2;
}
int main()
{
int test;
cin>>test;
while(test--){
int n, m;
cin>>n>>m;
vector<vector<char>> A(n, vector<char>(m));
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>A[i][j];
}
}
cout<<collect_gold(A, n, m);
}
}