forked from MukulCode/CodingClubIndia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMNT.cpp
235 lines (208 loc) · 6.92 KB
/
GMNT.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*/ Author: iamshubh69 /*/
// #pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace std;
using namespace __gnu_pbds;
template<class T>
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
template<class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
template<class T>
using min_heap = priority_queue<T,vector<T>,greater<T> >;
/*/---------------------------IO(Debugging)----------------------/*/
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
istream& operator >> (istream &is, T_container &v) {
for(T &x : v) is >> x; return is;
}
#ifdef __SIZEOF_INT128__
ostream& operator << (ostream &os, __int128 const& value){
static char buffer[64];
int index = 0;
__uint128_t T = (value < 0) ? (-(value + 1)) + __uint128_t(1) : value;
if (value < 0)
os << '-';
else if (T == 0)
return os << '0';
for(; T > 0; ++index){
buffer[index] = static_cast<char>('0' + (T % 10));
T /= 10;
}
while(index > 0)
os << buffer[--index];
return os;
}
istream& operator >> (istream& is, __int128& T){
static char buffer[64];
is >> buffer;
size_t len = strlen(buffer), index = 0;
T = 0; int mul = 1;
if (buffer[index] == '-')
++index, mul *= -1;
for(; index < len; ++index)
T = T * 10 + static_cast<int>(buffer[index] - '0');
T *= mul;
return is;
}
#endif
template<typename A, typename B>
ostream& operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream& operator << (ostream &os, const T_container &v) {
os << '{'; string sep;
for (const T &x : v) os << sep << x, sep = ", ";
return os << '}';
}
template<class P, class Q = vector<P>, class R = less<P> > ostream& operator << (ostream& out, priority_queue<P, Q, R> const& M){
static priority_queue<P, Q, R> U;
U = M;
out << "{ ";
while(!U.empty())
out << U.top() << " ", U.pop();
return (out << "}");
}
template<class P> ostream& operator << (ostream& out, queue<P> const& M){
static queue<P> U;
U = M;
out << "{"; string sep;
while(!U.empty()){
out << sep << U.front(); sep = ", "; U.pop();
}
return (out << "}");
}
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
int count_open = 0, len = 1;
for(int k = 1; ; ++k){
char cur = *(names + k);
count_open += (cur == '(' ? 1 : (cur == ')' ? -1: 0));
if (cur == ',' && count_open == 0){
const char* comma = names + k;
cerr.write(names, len) << " : " << arg1 << " | ";
__f(comma + 1, args...);
return;
}
len = (cur == ' ' ? len : k + 1);
}
}
#else
#define trace(...) 1
#endif
/*/---------------------------RNG----------------------/*/
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int64_t random_long(long long l = LLONG_MIN,long long r = LLONG_MAX){
uniform_int_distribution<int64_t> generator(l,r);
return generator(rng);
}
struct custom_hash { // Credits: https://codeforces.com/blog/entry/62393
static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
template<typename L, typename R>
size_t operator()(pair<L,R> const& Y) const{
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(Y.first * 31ull + Y.second + FIXED_RANDOM);
}
};
/*/---------------------------Defines----------------------/*/
#define endl "\n"
#define all(v) (v).begin(),(v).end()
/*/-----------------------------Code begins----------------------------------/*/
void solve(){
int n, m;
cin >> n >> m;
vector<string> matr(n);
cin >> matr;
vector<vector<int>> dist(n, vector<int>(m, n + m + 10));
vector<array<int, 2>> bfs;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if (matr[i][j] == 'R'){
bfs.push_back({i, j});
dist[i][j] = 0;
}
}
}
const vector<int> dx = {-1, 1, 0, 0};
const vector<int> dy = {0, 0, -1, 1};
for(int i = 0; i < bfs.size(); ++i){
auto [x, y] = bfs[i];
int d = dist[x][y];
for(int k = 0; k < 4; ++k){
int nx = x + dx[k], ny = y + dy[k];
if (nx < 0 || nx >= n || ny < 0 || ny >= m)
continue;
if (dist[nx][ny] > d + 1){
dist[nx][ny] = d + 1;
bfs.push_back({nx, ny});
}
}
}
// trace(dist);
vector<vector<long long>> dp(n, vector<long long>(m));
// dp[0][0] = dist[0][0];
for(int i = n - 1; i >= 0; --i){
for(int j = m - 1; j >= 0; --j){
if (i == n - 1 && j == m - 1){
dp[i][j] = dist[i][j];
continue;
}
int z = (i + j) & 1;
if (i == n - 1){
dp[i][j] = dp[i][j + 1] + dist[i][j];
}
else if (j == m - 1){
dp[i][j] = dp[i + 1][j] + dist[i][j];
}
else if (z == 0){
dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]) + dist[i][j];
}
else{
dp[i][j] = min(dp[i + 1][j], dp[i][j + 1]) + dist[i][j];
}
}
}
// trace(dist[0]);
// trace(dist[1]);
// trace(dist[2]);
// trace(dp[0]);
// trace(dp[1]);
// trace(dp[2]);
// trace(dp[1][0], dp[2][1]);
cout << dp[0][0] << endl;
}
int main(){
// Use "set_name".max_load_factor(0.25);"set_name".reserve(512); with unordered set
// Or use gp_hash_table<X,null_type>
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cout << fixed << setprecision(25);
cerr << fixed << setprecision(10);
auto start = std::chrono::high_resolution_clock::now();
int t = 1;
cin >> t;
while (t--) {
solve();
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
// cerr << "Time taken : " << ((long double)duration.count())/((long double) 1e9) <<"s "<< endl;
}