-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlices_Adventure_in_Chess.cpp
More file actions
257 lines (237 loc) · 8.54 KB
/
Alices_Adventure_in_Chess.cpp
File metadata and controls
257 lines (237 loc) · 8.54 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// બધા માટે રામ રામ, ભગવાનનું નામ લો અને તમારું કાર્ય શરૂ કરો.
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Fast I/O setup
struct FastIO {
FastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
} fast_io_setup;
// Binary Search Function
int binary_search(vector<int>& arr, int target) {
int left = 0, right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
// Custom Power Function (Efficient Modular Exponentiation)
ll power(ll base, ll exp, ll mod = 1e9+7) {
ll result = 1;
while (exp > 0) {
if (exp % 2 == 1) result = (result * base) % mod;
base = (base * base) % mod;
exp /= 2;
}
return result;
}
// Sieve of Eratosthenes for Prime Numbers
vector<int> sieve(int n) {
vector<int> is_prime(n + 1, 1);
is_prime[0] = is_prime[1] = 0;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i) is_prime[j] = 0;
}
}
vector<int> primes;
for (int i = 2; i <= n; i++) {
if (is_prime[i]) primes.push_back(i);
}
return primes;
}
// Factorization to Find Prime Divisors of a Number
vector<int> prime_factors(int n) {
vector<int> factors;
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
factors.push_back(i);
n /= i;
}
}
if (n > 1) factors.push_back(n);
return factors;
}
// Square Root Calculation (Integer)
int integer_sqrt(int n) {
int left = 0, right = n, ans = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (mid * mid == n) return mid;
if (mid * mid < n) {
ans = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
}
void simulate(string &s, ll &x, ll& y){
for(char c : s){
if (c == 'N')
y += 1;
else if (c == 'S')
y -= 1;
else if (c == 'W')
x -= 1;
else
x += 1;
}
}
// bool check_if_reachable(string &s, ll start_x, ll start_y, ll target_x, ll target_y, ll threshold_x, ll threshold_y) {
// ll x = start_x, y = start_y;
// while (true) {
// for (char move : s) {
// // Check if we’ve reached the target
// if (x == target_x && y == target_y) return true;
// // Move according to the character
// if (move == 'N') y += 1;
// else if (move == 'S') y -= 1;
// else if (move == 'E') x += 1;
// else if (move == 'W') x -= 1;
// // Check if we’ve exceeded the threshold in the quadrant direction
// if ((target_x > 0 && target_y > 0) && (x > target_x + threshold_x || y > target_y + threshold_y)) return false;
// if ((target_x > 0 && target_y < 0) && (x > target_x + threshold_x || y < target_y + threshold_y)) return false;
// if ((target_x < 0 && target_y > 0) && (x < target_x + threshold_x || y > target_y + threshold_y)) return false;
// if ((target_x < 0 && target_y < 0) && (x < target_x + threshold_x || y < target_y + threshold_y)) return false;
// }
// }
// }
int main() {
ll testcases;
cin >> testcases;
for(ll testcase = 0; testcase < testcases; testcase++){
ll n, a, b;
cin >> n >> a >> b;
string s;
cin >> s;
ll current_x = 0, current_y = 0, threshold_x = 0, threshold_y = 0;
simulate(s, current_x, current_y);
threshold_x = current_x;
threshold_y = current_y;
// now just go till x + threshold x and y + threshold y if (a, b) is in 1st quadrant
bool printed = false;
// if(a > 0){
// if(b > 0){
// while(current_x < a + threshold_x && current_y < b + threshold_y){
// // simulate but check if we have reached the location then print YES else NO
// if(printed)
// break;
// for(char c : s){
// if(current_x == a && current_y == b){
// cout << "YES\n";
// printed = true;
// break;
// }
// else{
// if(c == 'N') current_y += 1;
// else if(c == 'S') current_y -= 1;
// else if(c == 'E') current_x += 1;
// else if(c == 'W') current_x -= 1;
// }
// }
// }
// }
// else{
// while(current_x < a + threshold_x && current_y > b + threshold_y){
// // simulate but check if we have reached the location then print YES else NO
// if(printed)
// break;
// for(char c : s){
// if(current_x == a && current_y == b){
// cout << "YES\n";
// printed = true;
// break;
// }
// else{
// if(c == 'N') current_y += 1;
// else if(c == 'S') current_y -= 1;
// else if(c == 'E') current_x += 1;
// else if(c == 'W') current_x -= 1;
// }
// }
// }
// }
// }
// else{
// if(b > 0){
// while(current_x > a + threshold_x && current_y < b + threshold_y){
// // simulate but check if we have reached the location then print YES else NO
// if(printed)
// break;
// for(char c : s){
// if(current_x == a && current_y == b){
// cout << "YES\n";
// printed = true;
// break;
// }
// else{
// if(c == 'N') current_y += 1;
// else if(c == 'S') current_y -= 1;
// else if(c == 'E') current_x += 1;
// else if(c == 'W') current_x -= 1;
// }
// }
// }
// }
// else{
// while(current_x > a + threshold_x && current_y > b + threshold_y){
// // simulate but check if we have reached the location then print YES else NO
// if(printed)
// break;
// for(char c : s){
// if(current_x == a && current_y == b){
// cout << "YES\n";
// printed = true;
// break;
// }
// else{
// if(c == 'N') current_y += 1;
// else if(c == 'S') current_y -= 1;
// else if(c == 'E') current_x += 1;
// else if(c == 'W') current_x -= 1;
// }
// }
// }
// }
// }
current_x = 0, current_y = 0;
for (ll i = 0; i < 100; i++){
if(printed)
break;
for(char c : s){
if(current_x == a && current_y == b){
cout << "YES\n";
printed = true;
break;
}
else{
if(c == 'N')
current_y++;
else if(c == 'S')
current_y--;
else if(c == 'W')
current_x--;
else
current_x++;
}
}
}
if (!printed)
cout << "NO\n";
}
}
/*
----- ----- ----- ----
| - | | | |
| - ----- ----- |----
| - | | |
----- ----- ----- |
*/