-
Notifications
You must be signed in to change notification settings - Fork 0
/
C. Arrow Path.cpp
134 lines (117 loc) · 3.33 KB
/
C. Arrow Path.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
#include <bits/stdc++.h>
#define int long long
#define vi vector<int>
#define vb vector<bool>
#define readInput(a) \
for (auto &i : a) \
cin >> i
#define printOutput(a) \
for (auto &i : a) \
cout << i << " "
#define endLine "\n"
#define space << " " <<
#define pushBack pb
#define allElements(a) a.begin(), a.end()
#define reverseAll(a) a.rbegin(), a.rend()
const int N = 1e6 + 2;
const int MOD = 1e9 + 7;
const int INF = LLONG_MAX;
using namespace std;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<string> s(2);
cin >> s[0] >> s[1];
vector<vector<int>> pos(2, vector<int>(n));
pos[0][0] = 1;
queue<pair<int, int>> qp;
qp.push({0, 0});
while (qp.size()) {
auto e = qp.front();
int x = e.first;
int y = e.second;
qp.pop();
if (x - 1 >= 0)
{
if (s[x - 1][y] == '>' && !pos[x - 1][y + 1])
{
// cout<<pos[x - 1][y + 1]<<" ";
pos[x - 1][y + 1] = 1;
qp.push({x - 1, y + 1});
}
}
if (x - 1 >= 0)
{
if (s[x - 1][y] == '<' && !pos[x - 1][y - 1])
{
// cout<<pos[x - 1][y - 1]<<" ";
pos[x - 1][y - 1] = 1;
qp.push({x - 1, y - 1});
}
}
if (x + 1 <= 1)
{
if (s[x + 1][y] == '>' && !pos[x + 1][y + 1])
{
// cout<<pos[x + 1][y + 1]<<" ";
pos[x + 1][y + 1] = 1;
qp.push({x + 1, y + 1});
}
}
if (x + 1 <= 1)
{
if (s[x + 1][y] == '<' && !pos[x + 1][y - 1])
{
// cout<<pos[x + 1][y - 1]<<" ";
pos[x + 1][y - 1] = 1;
qp.push({x + 1, y - 1});
}
}
if (y - 1 >= 0)
{
if (s[x][y - 1] == '<' && !pos[x][y - 2])
{
// cout<<pos[x][y - 2]<<" ";
pos[x][y - 2] = 1;
qp.push({x, y - 2});
}
}
if (y + 1 < n)
{
if (s[x][y + 1] == '>' && !pos[x][y + 2])
{
// cout<<pos[x][y + 2]<<" ";
pos[x][y + 2] = 1;
qp.push({x, y + 2});
}
}
// cout<<endl;
}
// for(int i=0;i<2;i++){
// for(int j=0;j<n;j++){
// cout<<pos[i][j]<<" ";
// }
// cout<<endl;
// }
if (pos[1][n - 1]) cout << "YES\n";
else cout << "NO\n";
}
}
/*
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
if not preorder or not inorder:
return None
root = TreeNode(preorder[0])
mid = inorder.index(preorder[0])
root. left = self.buildTree(preorder[1:mid + 1], inorder[:mid])
root.right = self.buildTree(preorder[mid + 1:], inorder[mid + 1:])
return root
*/