-
Notifications
You must be signed in to change notification settings - Fork 1
/
dropping_ball.cpp
170 lines (137 loc) · 2.93 KB
/
dropping_ball.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <bits/stdc++.h>
typedef long long LL;
typedef std::vector<std::string> Grid;
typedef std::vector<std::vector<bool>> Matrix;
typedef std::vector<std::vector<LL>> Cost;
const char EMPTY = '.';
const char OBSTA = 'X';
const char RIGHT = 'R';
const char LEFT = 'L';
const char CONVE = '?';
struct Cell {
int r,c;
};
int R,C,K;
Grid g;
Matrix base;
Cost base_cost;
Cost curr_cost;
std::vector<LL> init_costs;
void bfs(int i) {
std::vector<std::vector<bool>> visited(R, std::vector<bool>(C));
std::queue<Cell> q;
q.push({0,i});
while (!q.empty()) {
Cell curr = q.front(); q.pop();
if (curr.r == R) {
base[i][curr.c] = true;
base_cost[i][curr.c] = init_costs[curr.c];
continue;
}
if (visited[curr.r][curr.c]) {
continue;
}
visited[curr.r][curr.c] = true;
switch (g[curr.r][curr.c]) {
case EMPTY:
q.push({curr.r+1, curr.c});
break;
case OBSTA:
break;
case RIGHT:
if (curr.c+1 < C) {
q.push({curr.r, curr.c+1});
}
break;
case LEFT:
if (curr.c > 0) {
q.push({curr.r, curr.c-1});
}
break;
case CONVE:
if (curr.c+1 < C) {
q.push({curr.r, curr.c+1});
}
if (curr.c > 0) {
q.push({curr.r, curr.c-1});
}
break;
}
}
}
void multiply(Matrix& a, Matrix& b, Matrix& out, Cost& c_a, Cost& c_b, Cost& c_out) {
for (int r = 0; r < C; ++r) {
for (int c = 0; c < C; ++c) {
bool val = false;
LL max = 0;
for (int k = 0; k < C; ++k) {
bool tmp = (a[r][k] & b[k][c]);
val = (val | tmp);
if (tmp) {
max = std::max(max, c_a[r][k] + c_b[k][c]);
}
}
out[r][c] = val;
c_out[r][c] = max;
}
}
}
Matrix to_return;
Cost c_to_return;
void pow(int k) {
if (k == 0) {
to_return.assign(C, std::vector<bool>(C));
for (int i = 0; i < C; ++i) to_return[i][i] = true;
c_to_return.assign(C, std::vector<LL>(C,0));
return;
} else if (k == 1) {
to_return = base;
c_to_return = base_cost;
return;
} else {
pow(k/2);
Matrix copy = to_return;
Cost c_copy = c_to_return;
multiply(copy, copy, to_return, c_copy, c_copy, c_to_return);
if (k%2) {
Matrix copy = to_return;
Cost c_copy = c_to_return;
multiply(copy, base, to_return, c_copy, base_cost, c_to_return);
}
return;
}
}
LL get_max_cost() {
LL ans = 0;
for (int r = 0; r < C; ++r) {
for(int c = 0; c < C; ++c) {
ans = std::max(ans, c_to_return[r][c]);
}
}
return ans;
}
int main() {
std::cin >> R >> C >> K;
base.assign(C, std::vector<bool>(C));
g.assign(R, "");
init_costs.assign(C,0);
base_cost.assign(C, std::vector<LL>(C));
for (int i = 0; i < R; ++i) {
std::cin >> g[i];
}
for (int i = 0; i < C; ++i) {
std::cin >> init_costs[i];
}
// Build the base matrix
for (int i = 0; i < C; ++i) {
bfs(i);
}
// test
Matrix res(C, std::vector<bool>(C));
Cost c_res(C, std::vector<LL>(C));
multiply(base, base, res, base_cost, base_cost, c_res);
pow(K);
LL ans = get_max_cost();
printf("%lld\n", ans);
return 0;
}