-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1766C.go
155 lines (146 loc) · 2.42 KB
/
1766C.go
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
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)
var in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
// source: https://codeforces.com/contest/1766/problem/C
func solve() {
n := _readInt()
c := []string{_readStr(), _readStr()}
type pos struct {
i, j int
}
queue := make([]pos, 0)
push := func(i, j int) {
queue = append(queue, pos{i, j})
}
pop := func() pos {
x := queue[0]
queue = queue[1:]
return x
}
need := strings.Count(c[0], "B") + strings.Count(c[1], "B")
for _, si := range []int{0, 1} {
sj := 0
if c[si][sj] != 'B' {
continue
}
visited := [][]bool{make([]bool, n), make([]bool, n)}
cnt := 0
push(si, sj)
for len(queue) > 0 {
p := pop()
visited[p.i][p.j] = true
cnt++
if p.i == 0 && c[1][p.j] == 'B' && !visited[1][p.j] {
push(1, p.j)
} else if p.i == 1 && c[0][p.j] == 'B' && !visited[0][p.j] {
push(0, p.j)
} else if p.j+1 < n && c[p.i][p.j+1] == 'B' && !visited[p.i][p.j+1] {
push(p.i, p.j+1)
}
}
if need == cnt {
_print("YES")
return
}
}
_print("NO")
}
func main() {
defer out.Flush()
var t int
for fmt.Fscanln(in, &t); t > 0; t-- {
solve()
}
}
// ====
func _readInt() int {
var x int
fmt.Fscan(in, &x)
return x
}
func _readStr() string {
var x string
fmt.Fscan(in, &x)
return x
}
func _readArr(x ...int) (int, []int) {
var n int
if len(x) == 0 {
fmt.Fscan(in, &n)
} else {
n = x[0]
}
var v = make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscan(in, &v[i])
}
return n, v
}
func _print(x ...any) {
fmt.Fprintln(out, x...)
}
func _max(x ...int) int {
mi := 0
for i := 1; i < len(x); i++ {
if x[i] > x[mi] {
mi = i
}
}
return x[mi]
}
func _min(x ...int) int {
mi := 0
for i := 1; i < len(x); i++ {
if x[i] < x[mi] {
mi = i
}
}
return x[mi]
}
func _sum(x ...int) int {
res := 0
for _, i := range x {
res += i
}
return res
}
func _abs(x int) int {
if x < 0 {
return -x
}
return x
}
func _sortStr(x string) string {
r := []rune(x)
sort.Slice(r, func(i, j int) bool {
return r[i] < r[j]
})
return string(r)
}
func _reverseInts(x []int) {
n := len(x)
for i := 0; i < n/2; i++ {
x[i], x[n-1-i] = x[n-1-i], x[i]
}
}
func _reverseStr(s string) string {
x := []rune(s)
n := len(x)
for i := 0; i < n/2; i++ {
x[i], x[n-1-i] = x[n-1-i], x[i]
}
return string(x)
}
func _makeMatrix(n, m int) [][]int {
x := make([][]int, n)
for i := range x {
x[i] = make([]int, m)
}
return x
}