Skip to content

Commit 0722bdb

Browse files
Add files via upload
1 parent 7108654 commit 0722bdb

1 file changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
const int MAX_L = 25;
7+
8+
int rows, columns;
9+
vector <string> colour;
10+
vector <string> S;
11+
vector <vector <int> > step(MAX_L);
12+
vector <int> visited;
13+
14+
int label(int x, int y)
15+
{
16+
return x*columns + y;
17+
}
18+
19+
int is_bit_set(int n, int bit)
20+
{
21+
return ((n&(1 << bit)) != 0);
22+
}
23+
24+
int get_next(int x, int y)
25+
{
26+
switch(S[x][y])
27+
{
28+
case 'U' : return label(x - 1, y);
29+
case 'R' : return label(x, y + 1);
30+
case 'L' : return label(x, y - 1);
31+
case 'D' : return label(x + 1, y);
32+
}
33+
34+
return 0;
35+
}
36+
37+
void dfs(int v)
38+
{
39+
if(visited[v])
40+
{
41+
return;
42+
}
43+
44+
visited[v] = true;
45+
46+
int x = v/columns, y = v%columns;
47+
48+
int next = get_next(x, y);
49+
//cout << "At " << v << " " << x << "," << y << " Next = " << next << "\n";
50+
step[0][v] = next;
51+
52+
dfs(next);
53+
}
54+
55+
void solve()
56+
{
57+
cin >> rows >> columns;
58+
59+
colour.resize(rows);
60+
for(int i = 0; i < rows; i++)
61+
{
62+
cin >> colour[i];
63+
}
64+
65+
S.resize(rows);
66+
for(int i = 0; i < rows; i++)
67+
{
68+
cin >> S[i];
69+
}
70+
71+
visited.resize(rows*columns, false);
72+
for(int i = 0; i < rows*columns; i++)
73+
{
74+
visited[i] = false;
75+
}
76+
77+
for(int i = 0; i < MAX_L; i++)
78+
{
79+
step[i].resize(rows*columns);
80+
}
81+
82+
for(int v = 0; v < rows*columns; v++)
83+
{
84+
if(!visited[v])
85+
{
86+
dfs(v);
87+
}
88+
}
89+
90+
for(int l = 1; l < MAX_L; l++)
91+
{
92+
for(int i = 0; i < rows*columns; i++)
93+
{
94+
int previous_step = step[l - 1][i];
95+
96+
//cout << "L = " << l << "Start at " << i/columns << "," << i%columns << " previous = " << previous_step/columns << "," << previous_step%columns <<" and finish at " << step[l - 1][previous_step]/columns << " " << step[l - 1][previous_step]%columns << "\n";
97+
98+
step[l][i] = step[l - 1][previous_step];
99+
}
100+
}
101+
102+
vector <int> black_starts(rows*columns);
103+
vector <int> white_starts(rows*columns);
104+
int total_steps = rows*columns;
105+
for(int i = 0; i < rows*columns; i++)
106+
{
107+
int finish = i;
108+
109+
for(int bit = MAX_L - 1; bit >= 0; bit--)
110+
{
111+
if(is_bit_set(total_steps, bit))
112+
{
113+
finish = step[bit][finish];
114+
}
115+
}
116+
117+
const char BLACK = '0', WHITE = '1';
118+
int start_x = i/columns, start_y = i%columns;
119+
if(colour[start_x][start_y] == BLACK)
120+
{
121+
black_starts[finish]++;
122+
}
123+
else
124+
{
125+
white_starts[finish]++;
126+
}
127+
128+
//cout << "Start at " << start_x << "," << start_y << " and finish at " << finish/columns << " " << finish%columns << "\n";
129+
}
130+
131+
int total = 0, black = 0;
132+
for(int i = 0; i < rows; i++)
133+
{
134+
for(int j = 0; j < columns; j++)
135+
{
136+
if(black_starts[label(i, j)] > 0)
137+
{
138+
total++;
139+
black++;
140+
}
141+
else if(white_starts[label(i, j)] > 0)
142+
{
143+
total++;
144+
}
145+
}
146+
}
147+
148+
cout << total << " " << black << "\n";
149+
}
150+
151+
int main()
152+
{
153+
int no_of_test_cases;
154+
cin >> no_of_test_cases;
155+
156+
while(no_of_test_cases--)
157+
solve();
158+
159+
return 0;
160+
}

0 commit comments

Comments
 (0)