forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(CODECHEF) C2 - Successor (Slightly Harder Version).cpp
117 lines (110 loc) · 2.42 KB
/
(CODECHEF) C2 - Successor (Slightly Harder Version).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
#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;
#define MP make_pair
#define pb push_back
#define FS first
#define SC second
#define INF 1LL<<60
#define all(v) v.begin(), v.end()
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef long long ll;
typedef pair<long long,int> pli;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set_int;
typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> ordered_set_ii;
// find_by_order() - returns an iterator to the k-th smallest element(0-based)
// order_of_key() - the number of items in a set that are strictly smaller than our item
int n;
char s[1005][1005];
int dp[1005][1005];
int ans;
void dfs(int u,int v)
{
if(s[u][v]=='D')
{
if(u+1>=n)
{
dp[u][v] = (u+v)%2;
if(dp[u][v]==0)
ans++;
}
else if(dp[u+1][v]!=-1)
{
if(dp[u+1][v]==0)
ans++;
}
else
{
dfs(u+1,v);
dp[u][v] = dp[u+1][v];
}
}
else if(s[u][v]=='L')
{
if(v-1<0)
{
dp[u][v] = (u+v)%2;
if(dp[u][v]==0)
ans++;
}
else if(dp[u][v-1]!=-1)
{
if(dp[u][v-1]==0)
ans++;
}
else
{
dfs(u,v-1);
dp[u][v] = dp[u][v-1];
}
}
else if(s[u][v]=='R')
{
if(v+1>=n)
{
dp[u][v] = (u+v)%2;
if(dp[u][v]==0)
ans++;
}
else if(dp[u][v+1]!=-1)
{
if(dp[u][v+1]==0)
ans++;
}
else
{
dfs(u,v+1);
dp[u][v] = dp[u][v+1];
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>s[i][j];
dp[i][j] = -1;
}
}
ans = 0;
for(int i=0;i<n;i++)
{
dfs(0,i);
}
cout << ans << "\n";
}
return 0;
}