solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, k;
string wall[2];
bool mark[2][maxn];
bool bfs(int w = 0, int ind = 0) {
queue<pair<pair<int, int>, int>> q;
q.push({{w, ind}, 0});
mark[w][ind] = true;
while (!q.empty()) {
auto [tp, ut] = q.front();
auto [uw, uind] = tp;
if (uind >= n) return true;
q.pop();
if (ut > uind) continue;
int dw[] = {0, 0, 1};
int di[] = {-1, 1, k};
for (int i = 0; i < 3; i++) {
int nw = (uw + dw[i]) % 2, nind = uind + di[i];
if (nind >= 0 && (!mark[nw][nind]) && wall[nw][nind] == '-') {
q.push({{nw, nind}, ut + 1});
mark[nw][nind] = true;
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(16);
if (0) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
cin >> n >> k >> wall[0] >> wall[1];
for (int i = 0; i <= k; i++) {
wall[0] += '-';
wall[1] += '-';
}
cout << (bfs() ? "YES" : "NO") << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int best[100010][2];
int pode[100010][2];
char lef[100010], righ[100010];
int main() {
int n, k;
scanf("%d %d", &n, &k);
scanf("%s", lef);
scanf("%s", righ);
memset(pode, 0, sizeof(pode));
for (int i = 0; i < n; i++) {
pode[i][0] = (lef[i] == '-');
pode[i][1] = (righ[i] == '-');
}
for (int i = 0; i < n; ++i) best[i][0] = (1 << 29), best[i][1] = (1 << 29);
priority_queue<pair<int, pair<int, int> > > cola;
cola.push(make_pair(0, make_pair(0, 0)));
best[0][0] = 0;
while (!cola.empty()) {
int w = -cola.top().first;
int wall = cola.top().second.first;
int lado = cola.top().second.second;
cola.pop();
if (best[wall][lado] < w) continue;
if (wall > n) {
cout << "YES\n";
return 0;
}
if (wall + 1 >= n || wall + k >= n) {
cout << "YES\n";
return 0;
}
if (pode[wall + 1][lado] && w + 1 < best[wall + 1][lado]) {
best[wall + 1][lado] = w + 1;
cola.push(make_pair(-(w + 1), make_pair(wall + 1, lado)));
}
if (wall - 1 >= 0 && w + 1 <= wall - 1 && pode[wall - 1][lado] &&
w + 1 < best[wall - 1][lado]) {
best[wall - 1][lado] = w + 1;
cola.push(make_pair(-(w + 1), make_pair(wall - 1, lado)));
}
if (pode[wall + k][1 - lado] && w + 1 < best[wall + k][1 - lado]) {
best[wall + k][1 - lado] = w + 1;
cola.push(make_pair(-(w + 1), make_pair(wall + k, 1 - lado)));
}
}
cout << "NO\n";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
char s[2][102012];
queue<int> Q1, Q2;
int ans, n, k, i;
int f[2][102012];
int main() {
scanf("%d%d ", &n, &k);
gets(s[0]);
gets(s[1]);
for (i = 0; i < n; ++i) f[1][i] = f[0][i] = 1000002012;
Q1.push(0);
Q2.push(0);
f[0][0] = 0;
while (Q1.size()) {
int Now = Q1.front(), side = Q2.front();
Q1.pop();
Q2.pop();
if (Now + 1 >= n) {
ans = f[side][Now] + 1;
break;
} else if (s[side][Now + 1] != 'X' && f[side][Now + 1] > f[side][Now] + 1) {
f[side][Now + 1] = f[side][Now] + 1;
Q1.push(Now + 1);
Q2.push(side);
}
if (Now - 1 >= 0 && s[side][Now - 1] != 'X' && Now - 1 > f[side][Now] &&
f[side][Now - 1] > f[side][Now] + 1) {
f[side][Now - 1] = f[side][Now] + 1;
Q1.push(Now - 1);
Q2.push(side);
}
if (Now + k >= n) {
ans = f[side][Now] + 1;
break;
} else if (s[!side][Now + k] != 'X' &&
f[!side][Now + k] > f[side][Now] + 1) {
f[!side][Now + k] = f[side][Now] + 1;
Q1.push(Now + k);
Q2.push(!side);
}
}
if (ans == 0)
printf("NO");
else
printf("YES");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;
int t[2][100000];
int main() {
int n, k;
cin >> n >> k;
string s[2];
cin >> s[0] >> s[1];
queue<pair<int, int> > Q;
memset(t, -1, sizeof(t));
t[0][0] = 0;
Q.push(pair<int, int>(0, 0));
int dx[] = {-1, 1, k};
while (!Q.empty()) {
pair<int, int> p = Q.front();
Q.pop();
int lr = p.first;
int pos = p.second;
int tm = t[lr][pos];
for (int i = 0; i < (int)3; ++i) {
int xx = pos + dx[i];
if (xx < 0) continue;
if (xx >= n) {
cout << "YES" << endl;
return 0;
}
if (i == 2) lr = !lr;
if (s[lr][xx] == 'X') continue;
if (t[lr][xx] != -1) continue;
if (xx <= tm) continue;
t[lr][xx] = tm + 1;
Q.push(pair<int, int>(lr, xx));
}
}
cout << "NO" << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
char s[2][210000];
int dp[2][210000], n, k;
struct node {
int a, b, t;
node() {}
node(int _a, int _b, int _t) : a(_a), b(_b), t(_t) {}
};
void cmx(int &a, int b) {
if (a == -1 || b < a) a = b;
}
int BFS() {
queue<node> que;
que.push(node(0, 1, 0));
int mi = 0xFFFFFFF;
while (!que.empty()) {
node tmp = que.front();
que.pop();
int ta = tmp.a, tb = tmp.b, tt = tmp.t;
if (tb > n) {
mi = min(mi, tt);
continue;
}
if (s[ta][tb + 1] == '-' || tb + 1 > n) {
if (dp[ta][tb + 1] == -1 || tt + 1 < dp[ta][tb + 1]) {
dp[ta][tb + 1] = tt + 1;
que.push(node(ta, tb + 1, tt + 1));
}
}
if (s[!ta][tb + k] == '-' || tb + k > n) {
if (dp[!ta][tb + k] == -1 || tt + 1 < dp[!ta][tb + k]) {
dp[!ta][tb + k] = tt + 1;
que.push(node(!ta, tb + k, tt + 1));
}
}
if (tb > tt + 2) {
if (s[ta][tb - 1] == '-') {
if (dp[ta][tb - 1] == -1 || tt + 1 < dp[ta][tb - 1]) {
dp[ta][tb - 1] = tt + 1;
que.push(node(ta, tb - 1, tt + 1));
}
}
}
}
return mi;
}
int main() {
while (~scanf("%d%d", &n, &k)) {
scanf("%s", s[0] + 1);
scanf("%s", s[1] + 1);
memset(dp, -1, sizeof(dp));
int res = BFS();
if (res == 0xFFFFFFF)
puts("NO");
else
puts("YES");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k;
char s[2][100005];
int d[2][100005];
bool f[2][100005];
void work() {
int i, j, x, y, ans = 0;
scanf("%d%d", &n, &k);
scanf("%s%s", s[0] + 1, s[1] + 1);
queue<pair<int, int> > q;
q.push(make_pair(0, 1)), f[0][1] = 1, d[0][1] = 1;
while (!q.empty()) {
x = q.front().first, y = q.front().second, q.pop();
if (y + k <= n && s[!x][y + k] != 'X' && !f[!x][y + k])
f[!x][y + k] = 1, q.push(make_pair(!x, y + k)),
d[!x][y + k] = d[x][y] + 1;
if (y + 1 <= n && s[x][y + 1] != 'X' && !f[x][y + 1])
f[x][y + 1] = 1, q.push(make_pair(x, y + 1)), d[x][y + 1] = d[x][y] + 1;
if (y - 1 > 0 && y - 1 > d[x][y] && s[x][y - 1] != 'X' && !f[x][y - 1])
f[x][y - 1] = 1, q.push(make_pair(x, y - 1)), d[x][y - 1] = d[x][y] + 1;
}
for (i = n - k + 1; i <= n; ++i) ans |= f[0][i] | f[1][i];
puts(ans ? "YES" : "NO");
}
int main() {
work();
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, k;
cin >> n >> k;
string s1, s2;
cin >> s1 >> s2;
long long i, j;
i = n - 1;
long long c = 0LL;
long long f = 0LL;
char mat[2][n];
for (i = 0; i < 2; i++) {
for (j = 0; j < n; j++) {
if (i == 0) {
mat[0][j] = s1[i];
} else {
mat[1][j] = s2[j];
}
}
}
char fin[2][n];
for (i = 0; i < 2; i++) {
for (j = 0; j < n; j++) {
fin[i][j] = 'F';
}
}
queue<pair<long long, pair<long long, long long>>> q;
q.push({0, {0, 0}});
fin[0][0] = 'T';
while (q.size() > 0) {
long long level;
long long f1;
level = q.front().first;
f1 = q.front().second.first;
i = q.front().second.second;
q.pop();
level++;
if (f1 == 0) {
if (i - 1 >= 0 && i - 1 >= level && s1[i - 1] == '-' &&
fin[0][i - 1] == 'F') {
q.push({level, {0, i - 1}});
fin[0][i - 1] = 'T';
}
if (i + 1 >= n) {
cout << "YES";
return 0;
}
if (i + 1 < n && i + 1 >= level && s1[i + 1] == '-' &&
fin[0][i + 1] == 'F') {
q.push({level, {0, i + 1}});
fin[0][i + 1] = 'T';
}
if (i + k >= n) {
cout << "YES";
return 0;
}
if (i + k >= level && s2[i + k] == '-' && fin[1][i + k] == 'F') {
q.push({level, {1, i + k}});
fin[1][i + k] = 'T';
}
if (i - k >= 0 && i - k >= level && s2[i - k] == '-' &&
fin[1][i - k] == 'F') {
q.push({level, {1, i - k}});
fin[1][i - k] = 'T';
}
} else {
if (i - 1 >= 0 && i - 1 >= level && s2[i - 1] == '-' &&
fin[1][i - 1] == 'F') {
q.push({level, {1, i - 1}});
fin[1][i - 1] = 'T';
}
if (i + 1 >= n) {
cout << "YES";
return 0;
}
if (i + 1 < n && i + 1 >= level && s2[i + 1] == '-' &&
fin[1][i + 1] == 'F') {
q.push({level, {1, i + 1}});
fin[1][i + 1] = 'T';
}
if (i + k >= n) {
cout << "YES";
return 0;
}
if (i + k >= level && s1[i + k] == '-' && fin[0][i + k] == 'F') {
q.push({level, {0, i + k}});
fin[0][i + k] = 'T';
}
if (i - k >= 0 && i - k >= level && s1[i - k] == '-' &&
fin[0][i - k] == 'F') {
q.push({level, {1, i - k}});
fin[0][i - k] = 'T';
}
}
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, k;
string wall[2];
bool mark[2][maxn];
int t[2][maxn];
bool bfs() {
mark[0][0] = 1;
queue<pair<int, int>> q;
q.push({0, 0});
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if (t[x][y] > y) continue;
int my[] = {1, -1, k};
int mx[] = {0, 0, 1};
for (int i = 0; i < 3; i++) {
int newx = x + mx[i];
newx = newx % 2;
int newy = y + my[i];
if (newy >= n) return 1;
if (newy >= 0 && !mark[newx][newy] && wall[newx][newy] != 'X') {
q.push({newx, newy});
mark[newx][newy] = 1;
t[newx][newy] = t[x][y] + 1;
}
}
}
return 0;
}
int main() {
cin >> n >> k >> wall[0] >> wall[1];
if (bfs())
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
char s[3][400005];
int n, k, dis[400005], q[400005];
bool can[400005];
int geth(int x) {
if (x <= n)
return x;
else
return x - n;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= 2; ++i) scanf("%s", &s[i][1]);
for (int i = 1; i <= 2; ++i) {
for (int j = 1; j <= n; ++j) can[(i - 1) * n + j] = bool(s[i][j] == '-');
}
int h, t;
bool flag = 0;
for (int i = 1; i <= 2 * n; ++i) dis[i] = 214748364;
h = 0;
q[t = 1] = 1;
dis[1] = 0;
while (h != t) {
int x = q[++h];
if (geth(x) <= dis[x]) continue;
if (x == n || x == 2 * n || geth(x) > n - k) {
puts("YES");
flag = 1;
break;
}
if (can[x + 1] && dis[x + 1] > dis[x] + 1) {
dis[x + 1] = dis[x] + 1;
q[++t] = x + 1;
}
if (x != 1 && x != n + 1 && can[x - 1] && dis[x - 1] > dis[x] + 1) {
dis[x - 1] = dis[x] + 1;
q[++t] = x - 1;
}
if (x <= n) {
if (can[x + n + k] && dis[x + n + k] > dis[x] + 1) {
dis[x + n + k] = dis[x] + 1;
q[++t] = x + n + k;
}
} else {
if (can[x - n + k] && dis[x - n + k] > dis[x] + 1) {
dis[x - n + k] = dis[x] + 1;
q[++t] = x - n + k;
}
}
}
if (!flag) puts("NO");
return 0;
}
| 8 | CPP |
def bfs(n):
visited=[False]*(2*n)
queue=[]
queue2=[]
queue.append(0)
queue2.append(0)
visited[0]=True
flag=True
water=0
while queue and flag:
s=queue.pop(0)
water= queue2.pop(0)
for i in graph[s]:
if not visited[i]:
a=0
#print(water,i)
if i>=n:
a=i-n
else:
a=i
if a>water:
if i==n-1 or i==2*n-1:
flag=False
else:
queue.append(i)
queue2.append(water+1)
visited[i]=True
return not flag
n,k=map(int,input().split())
l=input()
r=input()
graph=[]
for i in range(n):
graph.append([])
if l[i]=='-':
if i+1<n:
if l[i+1]=='-':
graph[i].append(i+1)
if i-1>=0:
if l[i-1]=='-':
graph[i].append(i-1)
if i+k<n:
if r[i+k]=='-':
graph[i].append(n+i+k)
else:
graph[i].append(2*n-1)
for i in range(n):
graph.append([])
if r[i]=='-':
if i+1<n:
if r[i+1]=='-':
graph[n+i].append(n+i+1)
if i-1>=0:
if r[i-1]=='-':
graph[n+i].append(n+i-1)
if i+k<n:
if l[i+k]=='-':
graph[n+i].append(i+k)
else:
graph[n+i].append(n-1)
#print(graph)
if n==1:
print("YES")
elif bfs(n):
print("YES")
else:
print("NO")
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010, MOD = 1e9 + 7, sn = 500;
int n, k, wl;
bool vs[2][N];
char w[2][N];
void dfs(int wa, int bl) {
if (bl >= n) {
cout << "YES";
exit(0);
}
if (w[wa][bl] == 'X' || vs[wa][bl] || wl > bl) return;
vs[wa][bl] = 1;
wl++;
dfs(wa ^ 1, bl + k);
dfs(wa, bl + 1);
dfs(wa, bl - 1);
wl--;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> w[0][i];
for (int i = 0; i < n; i++) cin >> w[1][i];
dfs(0, 0);
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
enum Side { LEFT, RIGHT };
int main(int argc, char *argv[]) {
int height, jump;
cin >> height >> jump;
cin.ignore();
char left[height];
char right[height];
cin.getline(left, height + 1);
cin.getline(right, height + 1);
int curr_height = 0;
queue<int> choices;
queue<Side> sides;
queue<int> water_level;
vector<bool> left_visited(height);
vector<bool> right_visited(height);
Side side = LEFT;
int water;
choices.push(0);
sides.push(LEFT);
water_level.push(-1);
left_visited[0] = true;
while (!choices.empty()) {
curr_height = choices.front();
if (curr_height + jump >= height) {
cout << "YES" << endl;
return 0;
}
choices.pop();
side = sides.front();
sides.pop();
water = water_level.front();
water_level.pop();
if (curr_height == water) {
continue;
}
if (side == LEFT) {
if (!right_visited[curr_height + jump] &&
right[curr_height + jump] != 'X') {
choices.push(curr_height + jump);
right_visited[curr_height + jump] = true;
sides.push(RIGHT);
water_level.push(water + 1);
}
if (!left_visited[curr_height + 1] && left[curr_height + 1] != 'X') {
choices.push(curr_height + 1);
left_visited[curr_height + 1] = true;
sides.push(LEFT);
water_level.push(water + 1);
}
if (water < curr_height - 1 && !left_visited[curr_height - 1] &&
left[curr_height - 1] != 'X') {
choices.push(curr_height - 1);
left_visited[curr_height - 1] = true;
sides.push(LEFT);
water_level.push(water + 1);
}
} else {
if (!left_visited[curr_height + jump] &&
left[curr_height + jump] != 'X') {
choices.push(curr_height + jump);
left_visited[curr_height + jump] = true;
sides.push(LEFT);
water_level.push(water + 1);
}
if (!right_visited[curr_height + 1] && right[curr_height + 1] != 'X') {
choices.push(curr_height + 1);
right_visited[curr_height + 1] = true;
sides.push(RIGHT);
water_level.push(water + 1);
}
if (water < curr_height - 1 && !right_visited[curr_height - 1] &&
right[curr_height - 1] != 'X') {
choices.push(curr_height - 1);
right_visited[curr_height - 1] = true;
sides.push(RIGHT);
water_level.push(water + 1);
}
}
}
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long mod = 1e17 + 7;
const long long N = 1e5 + 5;
long long n, k, d[N][2], f = 0;
string a[2];
queue<pair<long long, long long>> q;
long long valid(long long x, long long y) {
if (x < 0) return 0;
if (a[y][x] == 'X') return 0;
if (d[x][y] != 0) return 0;
return 1;
}
void bfs() {
while (!q.empty()) {
pair<long long, long long> top = q.front();
q.pop();
long long x = top.first, y = top.second;
if (valid(x + 1, y)) {
d[x + 1][y] = d[x][y] + 1;
q.push(make_pair(x + 1, y));
}
if (valid(x - 1, y) && x + 1 > d[x][y] + 1) {
d[x - 1][y] = d[x][y] + 1;
q.push(make_pair(x - 1, y));
}
if (x + k >= n) {
f = 1;
return;
} else {
if (valid(x + k, 1 - y)) {
d[x + k][1 - y] = d[x][y] + 1;
q.push(make_pair(x + k, 1 - y));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
memset(d, 0, sizeof(d));
cin >> n >> k >> a[0] >> a[1];
d[0][0] = 1;
q.push(make_pair(0, 0));
bfs();
if (f)
cout << "YES";
else
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct pt {
int in;
int ti;
int w;
pt(int in = 0, int ti = 0, int w = 0) : in(in), ti(ti), w(w) {}
};
int used[2][100013];
int ground[2][100013];
int flag = 0;
int n, k;
void dfs(pt u) {
used[u.w][u.in] = 1;
if (flag || u.in <= u.ti) return;
if (u.in + k >= n || u.in + 1 >= n) {
flag = 1;
return;
}
if (!used[1 - u.w][u.in + k] && !ground[1 - u.w][u.in + k]) {
dfs(pt(u.in + k, u.ti + 1, 1 - u.w));
}
if (u.in >= 1 && !used[u.w][u.in - 1] && !ground[u.w][u.in - 1]) {
dfs(pt(u.in - 1, u.ti + 1, u.w));
}
if (!used[u.w][u.in + 1] && !ground[u.w][u.in + 1]) {
dfs(pt(u.in + 1, u.ti + 1, u.w));
}
}
int main() {
memset(used, 0, sizeof(used));
cin >> n >> k;
string s1, s2;
cin >> s1 >> s2;
for (int i = 0; i < n; i++) {
ground[0][i] = ((int)(s1[i] == 'X'));
ground[1][i] = ((int)(s2[i] == 'X'));
}
dfs(pt(0, -1, 0));
if (flag) {
cout << "YES";
} else {
cout << "NO";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 500;
bool Grid[2][N];
int dist[2][N];
queue<pair<bool, int> > q;
string L, R;
int n, k;
int main() {
cin >> n >> k;
cin >> L >> R;
for (int i = 0; i <= n + k; i++) {
if (i < n) {
Grid[0][i] = (L[i] == 'X');
Grid[1][i] = (R[i] == 'X');
}
dist[0][i] = dist[1][i] = 1e9;
}
dist[0][0] = 0;
q.push({0, 0});
int water = 0;
while (!q.empty()) {
pair<bool, int> u = q.front();
q.pop();
water = dist[u.first][u.second] + 1;
if (u.second - 1 >= 0 && u.second - 1 >= water) {
if (!Grid[u.first][u.second - 1] && dist[u.first][u.second - 1] == 1e9) {
dist[u.first][u.second - 1] = dist[u.first][u.second] + 1;
q.push({u.first, u.second - 1});
}
}
if (u.second + 1 >= 0) {
if (!Grid[u.first][u.second + 1] && dist[u.first][u.second + 1] == 1e9) {
dist[u.first][u.second + 1] = dist[u.first][u.second] + 1;
q.push({u.first, u.second + 1});
}
}
if (u.second + k >= 0) {
if (u.second + k >= n) {
cout << "YES" << endl;
exit(0);
}
if (!Grid[!u.first][u.second + k] &&
dist[!u.first][u.second + k] == 1e9) {
dist[!u.first][u.second + k] = dist[u.first][u.second] + 1;
q.push({!u.first, u.second + k});
}
}
water++;
if (u.second >= n) break;
}
bool flag = false;
for (int i = n - 1; i <= n + k; i++) {
if (dist[0][i] != 1e9 || dist[1][i] != 1e9) {
flag = true;
break;
}
}
flag ? puts("YES") : puts("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
vector<int> g[2][N];
deque<pair<int, int> > q;
long long int dis[2][N];
int main() {
ios::sync_with_stdio(0), cout.tie(0), cin.tie(0);
memset(dis[0], 63, sizeof dis[0]);
memset(dis[1], 63, sizeof dis[1]);
int n, k;
cin >> n >> k;
string s, t;
cin >> s >> t;
for (int i = 0; i < n - 1; i++) {
if (s[i] == '-' && s[i + 1] == '-') {
g[0][i + 1].push_back(i + 2);
g[0][i + 2].push_back(i + 1);
}
}
for (int i = 0; i < n - 1; i++) {
if (t[i] == '-' && t[i + 1] == '-') {
g[1][i + 1].push_back(i + 2);
g[1][i + 2].push_back(i + 1);
}
}
q.push_back({0, 1});
dis[0][1] = 0;
while (q.size()) {
int v = q.front().second;
int e = q.front().first;
q.pop_front();
for (int i = 0; i < g[e][v].size(); i++) {
if (g[e][v][i] > dis[e][v] + 1 && dis[e][v] + 1 < dis[e][g[e][v][i]]) {
dis[e][g[e][v][i]] = dis[e][v] + 1;
q.push_back({e, g[e][v][i]});
}
}
if (e == 0) {
if (v + k > t.size()) {
cout << "YES";
return 0;
}
if (t[v + k - 1] == '-' && dis[e][v] + 1 < dis[1 - e][v + k]) {
dis[1 - e][v + k] = dis[e][v] + 1;
q.push_back({1 - e, v + k});
}
} else {
if (v + k > s.size()) {
cout << "YES";
return 0;
}
if (s[v + k - 1] == '-' && dis[e][v] + 1 < dis[1 - e][v + k]) {
dis[1 - e][v + k] = dis[e][v] + 1;
q.push_back({1 - e, v + k});
}
}
}
for (int i = 0; i < 2; i++) {
for (int j = max(0, n - k + 1); j < N; j++) {
if (dis[i][j] < 10000000) {
cout << "YES";
return 0;
}
}
}
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, x, y, fi, fr, f[2][100100], qx[200200], qy[200200];
char s[2][100100];
int main() {
cin >> n >> k;
cin >> s[0];
cin >> s[1];
f[0][0] = 1;
for (fr = 1; fi < fr;) {
x = qx[fi];
y = qy[fi++];
if (y >= n - k) {
puts("YES");
return 0;
}
if (y > 0 && s[x][y - 1] == '-' && f[x][y - 1] == 0 && y > f[x][y]) {
f[x][y - 1] = f[x][y] + 1;
qx[fr] = x;
qy[fr++] = y - 1;
}
if (s[x][y + 1] == '-' && f[x][y + 1] == 0) {
f[x][y + 1] = f[x][y] + 1;
qx[fr] = x;
qy[fr++] = y + 1;
}
if (s[1 - x][y + k] == '-' && f[1 - x][y + k] == 0) {
f[1 - x][y + k] = f[x][y] + 1;
qx[fr] = 1 - x;
qy[fr++] = y + k;
}
}
puts("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool seen[200005];
struct data {
int node, h, water;
data(int a, int b, int c) : node(a), h(b), water(c) {}
};
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, k;
cin >> n >> k;
string left_wall, right_wall;
cin >> left_wall >> right_wall;
vector<vector<pair<int, int> > > G(2 * n + 1);
int free = 2 * n;
for (int i = 0; i < n; ++i) {
int nb = i + k;
if (left_wall[i] == '-') {
if (nb < n && right_wall[nb] == '-')
G[2 * i].push_back(make_pair(2 * nb + 1, k));
if (nb >= n) G[2 * i].push_back(make_pair(free, k));
if (i > 0 && left_wall[i - 1] == '-') {
G[2 * i].push_back(make_pair(2 * i - 2, -1));
G[2 * i - 2].push_back(make_pair(2 * i, 1));
}
}
if (right_wall[i] == '-') {
if (nb < n && left_wall[nb] == '-')
G[2 * i + 1].push_back(make_pair(2 * nb, k));
if (nb >= n) G[2 * i + 1].push_back(make_pair(free, k));
if (i > 0 && right_wall[i - 1] == '-') {
G[2 * i + 1].push_back(make_pair(2 * i - 1, -1));
G[2 * i - 1].push_back(make_pair(2 * i + 1, 1));
}
}
}
if (left_wall[n - 1] == '-') G[2 * n - 2].push_back(make_pair(free, k));
if (right_wall[n - 1] == '-') G[2 * n - 1].push_back(make_pair(free, k));
memset(seen, 0, sizeof seen);
seen[0] = 1;
queue<data> q;
q.push(data(0, 0, 0));
while (!q.empty()) {
data u = q.front();
q.pop();
for (int i = 0; i < G[u.node].size(); ++i) {
int v = G[u.node][i].first, w = G[u.node][i].second;
if (!seen[v]) {
seen[v] = true;
if (u.h + w <= u.water) continue;
if (u.h + w >= n) {
cout << "YES";
return 0;
}
q.push(data(v, u.h + w, u.water + 1));
}
}
}
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, used[100005][2], x, y, z;
string st[2];
queue<int> qx, qy, step;
int main() {
cin >> n >> m >> st[0] >> st[1];
for (i = 1; i <= m; i++) {
st[0] += "-";
st[1] += "-";
}
qx.push(0);
qy.push(0);
step.push(0);
while (!qx.empty()) {
x = qx.front();
y = qy.front();
z = step.front();
qx.pop();
qy.pop();
step.pop();
if (st[y][x] == 'X') continue;
if (x >= n - 1) {
cout << "YES";
return 0;
}
if (used[x][y]) continue;
if (z > x) continue;
used[x][y] = 1;
qx.push(x + 1);
qy.push(y);
step.push(z + 1);
qx.push(x - 1);
qy.push(y);
step.push(z + 1);
qx.push(x + m);
qy.push(y ^ 1);
step.push(z + 1);
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
char wall[2][100010];
int dp[100010][2];
int main(void) {
for (int i = 0; i < 100010; ++i) dp[i][0] = dp[i][1] = 1000000000;
dp[0][0] = 0;
int n, k;
scanf("%d %d ", &n, &k);
gets(wall[0]);
gets(wall[1]);
queue<pair<int, int> > q;
q.push(make_pair(0, 0));
while (!q.empty()) {
pair<int, int> tmp = q.front();
int curr = tmp.first;
int w = tmp.second;
q.pop();
if (dp[curr][w] > curr || wall[w][curr] == 'X') continue;
if (curr + k >= n) {
cout << "YES\n";
return 0;
}
if (dp[curr + 1][w] > dp[curr][w] + 1) {
dp[curr + 1][w] = dp[curr][w] + 1;
q.push(make_pair(curr + 1, w));
}
if (curr - 1 > -1 && dp[curr - 1][w] > dp[curr][w] + 1) {
dp[curr - 1][w] = dp[curr][w] + 1;
q.push(make_pair(curr - 1, w));
}
if (dp[curr + k][(w + 1) & 1] > dp[curr][w] + 1) {
dp[curr + k][(w + 1) & 1] = dp[curr][w] + 1;
q.push(make_pair(curr + k, (w + 1) & 1));
}
}
cout << "NO\n";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[2][200100];
bool vis[2][200100];
int main() {
scanf("%d%d", &n, &m);
scanf("%s", s[0]);
scanf("%s", s[1]);
queue<pair<int, pair<int, int> > > q;
q.push(make_pair(0, make_pair(0, 0)));
while (!q.empty()) {
int x = q.front().second.first;
int y = q.front().second.second;
int d = q.front().first;
q.pop();
if (y >= n) {
puts("YES");
return 0;
}
if ((d < y + m) && !vis[!x][y + m] && (y + m >= n || s[!x][y + m] == '-')) {
q.push(make_pair(d + 1, make_pair(!x, y + m)));
vis[!x][y + m] = 1;
}
if ((d < y + 1) && !vis[x][y + 1] && (y + 1 >= n || s[x][y + 1] == '-')) {
q.push(make_pair(d + 1, make_pair(x, y + 1)));
vis[x][y + 1] = 1;
}
if ((d < y - 1) && y > 0 && !vis[x][y - 1] && s[x][y - 1] == '-') {
q.push(make_pair(d + 1, make_pair(x, y - 1)));
vis[x][y - 1] = 1;
}
}
puts("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, q1, q2, x, y, l, f[2][111111], qx[333333], qy[333333], ql[333333];
char a[2][111111];
string ans = "NO";
void add(int x, int y, int l) {
if (y >= n) ans = "YES";
if (y >= n || a[x][y] == 'X' || f[x][y] || l > y) return;
f[x][y] = 1;
qx[q1] = x;
qy[q1] = y;
ql[q1++] = l;
}
int main() {
scanf("%d%d\n", &n, &k);
gets(a[0]);
gets(a[1]);
add(0, 0, 0);
while (q1 != q2) {
x = qx[q2];
y = qy[q2];
l = ql[q2++];
add(x, y + 1, l + 1);
add(x, y - 1, l + 1);
add(x ^ 1, y + k, l + 1);
}
cout << (ans) << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e6 + 100;
const long long maxM = 1e3 + 100;
const long long mod = 1e9 + 7;
const long long hmod = 1e16 + 7;
const long double PI = 3.141592653;
const long double eps = 1e-8;
const long long D = 1379;
const long long INF = 1e18 + 20;
const long long Inf = 1e9 + 140;
void NO() {
cout << "NO" << endl;
exit(0);
}
void YES() {
cout << "YES" << endl;
exit(0);
}
long long n, k;
bool mrk[2][maxN], mark[2][maxN];
void dfs(long long v, long long x = 0, long long W = 0) {
if (v > n) YES();
if (v <= 0 || mrk[x][v] || mark[x][v] || v <= W) return;
mark[x][v] = true;
W++;
dfs(v + k, x ^ 1, W);
dfs(v + 1, x, W);
dfs(v - 1, x, W);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (long long i = 1; i <= n; i++) {
char c;
cin >> c;
if (c == 'X') mrk[0][i] = true;
}
for (long long i = 1; i <= n; i++) {
char c;
cin >> c;
if (c == 'X') mrk[1][i] = true;
}
dfs(1);
NO();
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k;
string wall[2];
bool visit[2][100050];
bool escape(int s, int h, int t) {
if (h <= t || h < 0) return false;
if (h >= n) return true;
if (wall[s][h] != '-') return false;
if (visit[s][h]) return false;
visit[s][h] = true;
return max(max(escape(s, h + 1, t + 1), escape(s, h - 1, t + 1)),
escape(1 - s, h + k, t + 1));
}
int main() {
cin >> n >> k;
cin >> wall[0];
cin >> wall[1];
if (escape(0, 0, -1))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int arrayLeft[100010] = {0};
int arrayRight[100010] = {0};
class Walls {
public:
int height;
int pathLength;
int pos;
bool onLeft;
Walls() {
height = 1;
pathLength = 1000000;
pos = 1;
onLeft = true;
}
};
int main() {
int n, k;
scanf("%d%d", &n, &k);
Walls *wall = new Walls[2 * n];
char a[100010], b[100010];
a[0] = 'X';
b[0] = 'X';
scanf("%s%s", &a[1], &b[1]);
int initialLimit = 0, finalLimit = 1;
wall[0].pathLength = 0;
arrayLeft[0] = 0;
bool found = false;
int count = 0;
int waterHeight = 0;
count = finalLimit;
while (1) {
for (int i = initialLimit; i < finalLimit; i++) {
int pos = wall[i].pos;
int pathLength = wall[i].pathLength;
int height = wall[i].height;
if ((height + 1 > n || height + k > n) && pathLength + 1 <= n) {
found = true;
break;
}
if (wall[i].onLeft) {
if (arrayLeft[pos - 1] == 0 && a[pos - 1] == '-' &&
height - 1 > waterHeight + 1) {
arrayLeft[pos - 1] = 1;
wall[count].height = height - 1;
wall[count].pathLength = pathLength + 1;
wall[count].pos = pos - 1;
count++;
}
if (arrayLeft[pos + 1] == 0 && a[pos + 1] == '-' &&
height > waterHeight) {
arrayLeft[pos + 1] = 1;
wall[count].height = height + 1;
wall[count].pathLength = pathLength + 1;
wall[count].pos = pos + 1;
count++;
}
if (arrayRight[pos + k] == 0 && b[pos + k] == '-' &&
height + k > waterHeight + 1) {
arrayRight[pos + k] = 1;
wall[count].height = height + k;
wall[count].pathLength = pathLength + 1;
wall[count].pos = pos + k;
wall[count].onLeft = false;
count++;
}
} else {
if (arrayRight[pos - 1] == 0 && b[pos - 1] == '-' &&
height - 1 > waterHeight + 1) {
arrayRight[pos - 1] = 1;
wall[count].height = height - 1;
wall[count].pathLength = pathLength + 1;
wall[count].pos = pos - 1;
wall[count].onLeft = false;
count++;
}
if (arrayRight[pos + 1] == 0 && b[pos + 1] == '-' &&
height > waterHeight) {
arrayRight[pos + 1] = 1;
wall[count].height = height + 1;
wall[count].pathLength = pathLength + 1;
wall[count].pos = pos + 1;
wall[count].onLeft = false;
count++;
}
if (arrayLeft[pos + k] == 0 && a[pos + k] == '-' &&
height + k > waterHeight + 1) {
arrayLeft[pos + k] = 1;
wall[count].height = height + k;
wall[count].pathLength = pathLength + 1;
wall[count].pos = pos + k;
count++;
}
}
}
if (finalLimit == count) break;
if (found) break;
initialLimit = finalLimit;
finalLimit = count;
waterHeight++;
}
if (found) printf("YES\n");
if (!found) printf("NO\n");
}
| 8 | CPP |
#include <bits/stdc++.h>
const int MAXN = 500000;
using namespace std;
int n, k;
char s[2][MAXN];
int q[MAXN], d[MAXN];
void out(string verdict) {
cout << verdict << endl;
exit(0);
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s%s", s[0], s[1]);
q[0] = 0;
memset(d, -1, sizeof(d));
d[0] = 0;
for (int l = 0, r = 1; l < r; l++) {
int u = q[l] >> 1, v = q[l] & 1;
if (d[q[l]] > u) continue;
if (u + k >= n) out("YES");
if (d[(u + 1) * 2 + v] == -1 && s[v][u + 1] != 'X') {
d[(u + 1) * 2 + v] = d[q[l]] + 1;
q[r++] = (u + 1) * 2 + v;
}
if (u && d[(u - 1) * 2 + v] == -1 && s[v][u - 1] != 'X') {
d[(u - 1) * 2 + v] = d[q[l]] + 1;
q[r++] = (u - 1) * 2 + v;
}
if (d[(u + k) * 2 + (v ^ 1)] == -1 && s[v ^ 1][u + k] != 'X') {
d[(u + k) * 2 + (v ^ 1)] = d[q[l]] + 1;
q[r++] = (u + k) * 2 + (v ^ 1);
}
}
out("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 100;
int n;
int k;
vector<int> g[2 * N];
bool doz[2 * N];
string s;
bool pos[2 * N];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
pq;
int d[2 * N];
int p[2 * N];
bool NO;
void dijkstra() {
for (int i = 1; i <= 2 * n + 1; i++) {
d[i] = -1;
p[i] = -1;
}
d[1] = 1;
p[1] = 0;
pq.push(pair<int, int>(0, 1));
for (int i = 1; i <= 2 * n + 1; i++) {
int v = -1;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (!pos[u]) {
v = u;
pos[v] = true;
break;
}
}
if (v == -1) break;
pos[v] = true;
for (int k = 0; k < g[v].size(); k++) {
if (!pos[g[v][k]])
if (d[g[v][k]] == -1 || d[g[v][k]] > d[v] + 1) {
d[g[v][k]] = d[v] + 1;
p[g[v][k]] = v;
pq.push(pair<int, int>(d[g[v][k]], g[v][k]));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
cin >> s;
for (int i = 1; i <= n; i++)
if (s[i - 1] == '-') doz[i] = true;
cin >> s;
for (int i = 1; i <= n; i++)
if (s[i - 1] == '-') doz[n + i] = true;
doz[2 * n + 1] = true;
for (int i = 1; i < n; i++) {
if (!doz[i]) continue;
if (doz[i + 1]) g[i].push_back(i + 1);
if (doz[i - 1]) g[i].push_back(i - 1);
if (i > n - k)
g[i].push_back(2 * n + 1);
else if (doz[i + k + n])
g[i].push_back(i + k + n);
}
g[n].push_back(2 * n + 1);
for (int i = n + 1; i < 2 * n; i++) {
if (!doz[i]) continue;
if (i > n + 1)
if (doz[i - 1]) g[i].push_back(i - 1);
if (doz[i + 1]) g[i].push_back(i + 1);
if (i > 2 * n - k)
g[i].push_back(2 * n + 1);
else if (doz[i + k - n])
g[i].push_back(i + k - n);
}
g[2 * n].push_back(2 * n + 1);
dijkstra();
int itr = 2 * n + 1;
while (itr != 0) {
if (itr <= n) {
if (d[itr] > itr) {
NO = true;
break;
}
} else {
if (d[itr] > itr - n) {
NO = true;
break;
}
}
itr = p[itr];
}
if (NO)
cout << "NO" << endl;
else
cout << "YES" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int visited[2][100001];
string walls[2];
char a, b;
bool dfs(int x, int y, int t) {
if (y >= n) return true;
if (y < 0) return false;
if (walls[x][y] == 'X' || (visited[x][y] <= t && visited[x][y] != 0) || y < t)
return false;
visited[x][y] = t;
if (dfs(1 - x, y + k, t + 1)) return true;
if (dfs(x, y + 1, t + 1)) return true;
if (dfs(x, y - 1, t + 1)) return true;
return false;
}
int main() {
cin >> n >> k;
cin >> walls[0] >> walls[1];
if (dfs(0, 0, 0))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int mod = 998244353;
long long read() {
long long c = getchar(), Nig = 1, x = 0;
while (!isdigit(c)) c = getchar();
if (c == '-') Nig = -1, c = getchar();
while (isdigit(c)) x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
return Nig * x;
}
struct node {
int x, y, step;
friend bool operator<(node a, node b) { return a.step > b.step; }
};
char ma[2][maxn];
int n, vis[2][maxn];
bool bfs(int pos, int y, int k, int time) {
priority_queue<node> q;
node p, a, b;
b.x = pos;
b.y = y;
b.step = time;
q.push(b);
memset(vis, 0, sizeof vis);
vis[b.x][b.y] = 1;
while (!q.empty()) {
p = q.top();
q.pop();
if (p.y > n) return true;
if (ma[(p.x + 1) % 2][p.y + k] != 'X' && vis[(p.x + 1) % 2][p.y + k] == 0) {
vis[(p.x + 1) % 2][p.y + k] = 1;
a.x = (p.x + 1) % 2;
a.y = p.y + k;
a.step = p.step + 1;
if (a.step < a.y) {
q.push(a);
}
}
if (ma[p.x][p.y + 1] != 'X' && vis[p.x][p.y + 1] == 0) {
vis[p.x][p.y + 1] = 1;
a.x = p.x;
a.y = p.y + 1;
a.step = p.step + 1;
if (a.step < a.y) {
q.push(a);
}
}
if (ma[p.x][p.y - 1] != 'X' && vis[p.x][p.y - 1] == 0) {
vis[p.x][p.y - 1] = 1;
a.x = p.x;
a.y = p.y - 1;
a.step = p.step + 1;
if (a.step < a.y) {
q.push(a);
}
}
}
return false;
}
int main() {
n = read();
int k = read();
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) {
cin >> ma[i][j];
}
}
bool flag = bfs(0, 0, k, -1);
if (flag)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
char a[2][100005];
int visit[2][100005] = {0};
queue<int> q;
int n, k;
bool ans = false;
void chk(int x, int y, int time) {
if (y < time || y < 1) return;
if (y > n) {
ans = true;
return;
}
if (visit[x][y] || a[x][y] == 'X') return;
visit[x][y] = time;
q.push(x);
q.push(y);
}
int main() {
int x, y, t;
cin >> n >> k;
cin >> a[0] + 1;
cin >> a[1] + 1;
q.push(0);
q.push(1);
visit[0][1] = 1;
while (!q.empty()) {
x = q.front();
q.pop();
y = q.front();
q.pop();
t = visit[x][y];
chk(x, y + 1, t + 1);
chk(x, y - 1, t + 1);
chk(!x, y + k, t + 1);
}
cout << (ans ? "YES" : "NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k;
bool e = false;
bool **a;
int **ta;
void go(int q, int h, int t) {
if (!e) {
if (h >= n) {
e = true;
printf("YES");
return;
} else if (h >= 0)
if (!a[h][q] && t <= h && t < ta[h][q]) {
ta[h][q] = t;
go((q + 1) % 2, h + k, t + 1);
go(q, h + 1, t + 1);
go(q, h - 1, t + 1);
}
}
}
int main() {
scanf("%d%d", &n, &k);
scanf("\n");
a = new bool *[n];
ta = new int *[n];
for (int i = 0; i < n; i++) {
ta[i] = new int[2];
a[i] = new bool[2];
ta[i][1] = 2000000;
ta[i][0] = 2000000;
char c;
scanf("%c", &c);
if (c == 'X')
a[i][0] = true;
else
a[i][0] = false;
}
scanf("\n");
for (int i = 0; i < n; i++) {
char c;
scanf("%c", &c);
if (c == 'X')
a[i][1] = true;
else
a[i][1] = false;
}
go(0, 0, 0);
if (!e) printf("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T power(T a, T b) {
T x;
if (b == 0)
x = 1;
else
x = a;
for (size_t i = 1; i < b; i++) x *= a;
return x;
}
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
long long int n, m, a, b, c, ab, ba, x, y, z, avg, sum;
long long int arr2[100000], arr3[200000], arr4[200001], arr5[100001];
bool f, f1, f2;
string str, s1, s2, s3, s4;
set<int> seto;
double d, d1;
long long int x1, x2, y11, y2;
long long int mini = 100000000000000, maxi = 0;
map<char, long long int> mp;
vector<int> v, v1;
bool dp[100000][2];
void solve(long long int a = 0, long long int b = 0, bool wall = 0) {
int i = a;
int x = b;
if (i >= 0 && i < n) {
if (!wall) {
if (s1[i] == 'X' || x > i) return;
if (!dp[i][0]) {
dp[i][0] = 1;
solve(i - 1, x + 1, 0);
solve(i + m, x + 1, 1);
solve(i + 1, x + 1, 0);
}
} else {
if (s2[i] == 'X' || x > i) return;
if (!dp[i][1]) {
dp[i][1] = 1;
solve(i - 1, x + 1, 1);
solve(i + m, x + 1, 0);
solve(i + 1, x + 1, 1);
}
}
}
if (i >= n) {
cout << "YES";
exit(0);
}
}
int main() {
cin >> n >> m;
cin >> s1 >> s2;
solve();
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, x, y, fi, fr, f[2][100200], qx[200200], qy[200200];
char s[2][100100];
int main() {
cin >> n >> k;
cin >> s[0];
cin >> s[1];
f[0][0] = 1;
for (fr = 1; fi < fr;) {
x = qx[fi];
y = qy[fi++];
if (y >= n - k) {
cout << "YES";
return 0;
}
if (y > 0 && s[x][y - 1] == '-' && f[x][y - 1] == 0 && y > f[x][y]) {
f[x][y - 1] = f[x][y] + 1;
qx[fr] = x;
qy[fr++] = y - 1;
}
if (s[x][y + 1] == '-' && f[x][y + 1] == 0) {
f[x][y + 1] = f[x][y] + 1;
qx[fr] = x;
qy[fr++] = y + 1;
}
if (s[1 - x][y + k] == '-' && f[1 - x][y + k] == 0) {
f[1 - x][y + k] = f[x][y] + 1;
qx[fr] = 1 - x;
qy[fr++] = y + k;
}
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct S {
int x, y, time;
S() {}
S(int x, int y, int time) : x(x), y(y), time(time) {}
};
int main() {
int n, k;
string s[2];
while (cin >> n >> k >> s[0] >> s[1]) {
bool seen[2][100000];
memset(seen, 0, sizeof(seen));
queue<S> q;
q.push(S(0, 0, 0));
try {
while (!q.empty()) {
S m = q.front();
q.pop();
if (seen[m.y][m.x] || m.time > m.x) continue;
seen[m.y][m.x] = true;
if (m.x > 0 && !seen[m.y][m.x - 1] && s[m.y][m.x] == '-') {
q.push(S(m.x - 1, m.y, m.time + 1));
}
if (m.x + k >= n) throw 0;
if (!seen[m.y ^ 1][m.x + k] && s[m.y ^ 1][m.x + k] == '-') {
q.push(S(m.x + k, m.y ^ 1, m.time + 1));
}
if (m.x + 1 >= n) throw 0;
if (!seen[m.y][m.x + 1] && s[m.y][m.x + 1] == '-') {
q.push(S(m.x + 1, m.y, m.time + 1));
}
}
puts("NO");
} catch (int _) {
puts("YES");
}
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, k, f[2][maxn];
char a[2][maxn];
queue<pair<int, int> > myqueue;
bool bfs() {
for (int i = 1; i <= n; i++) f[0][i] = f[1][i] = 1000000007;
myqueue.push(pair<int, int>(0, 1));
f[0][1] = 0;
pair<int, int> u, v;
while (!myqueue.empty()) {
u = myqueue.front();
myqueue.pop();
v.first = u.first;
if (u.second > 1) {
v.second = u.second - 1;
if (a[v.first][v.second] != 'X' && v.second > f[u.first][u.second] + 1 &&
f[v.first][v.second] == 1000000007) {
f[v.first][v.second] = f[u.first][u.second] + 1;
myqueue.push(v);
}
}
v.second = u.second + 1;
if (v.second > n) return true;
if (a[v.first][v.second] != 'X' && v.second > f[u.first][u.second] + 1 &&
f[v.first][v.second] == 1000000007) {
f[v.first][v.second] = f[u.first][u.second] + 1;
myqueue.push(v);
}
v.first = 1 - u.first;
v.second = u.second + k;
if (v.second > n) return true;
if (a[v.first][v.second] != 'X' && v.second > f[u.first][u.second] + 1 &&
f[v.first][v.second] == 1000000007) {
f[v.first][v.second] = f[u.first][u.second] + 1;
myqueue.push(v);
}
}
return false;
}
int main() {
scanf("%d%d\n", &n, &k);
for (int i = 1; i <= n; i++) scanf("%c", &a[0][i]);
scanf("\n");
for (int i = 1; i <= n; i++) scanf("%c", &a[1][i]);
if (bfs())
puts("YES");
else
puts("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, wl[100005], wr[100005];
char l[100005], r[100005];
bool visL[100005], visR[100005];
bool bfs() {
queue<pair<int, char> > q;
q.push(make_pair(1, 'l'));
visL[1] = true;
wl[1] = 0;
while (!q.empty()) {
int x = q.front().first;
char d = q.front().second;
q.pop();
if (x + k > n) return true;
if (d == 'l') {
if (l[x + 1] == '-' && !visL[x + 1]) {
q.push(make_pair(x + 1, d));
visL[x + 1] = true;
wl[x + 1] = wl[x] + 1;
}
if (x != 1 && x - 1 > wl[x] + 1 && l[x - 1] == '-' && !visL[x - 1]) {
q.push(make_pair(x - 1, d));
visL[x - 1] = true;
wl[x - 1] = wl[x] + 1;
}
if (r[x + k] == '-' && !visR[x + k]) {
q.push(make_pair(x + k, 'r'));
visR[x + k] = true;
wr[x + k] = wl[x] + 1;
}
} else {
if (r[x + 1] == '-' && !visR[x + 1]) {
q.push(make_pair(x + 1, d));
visR[x + 1] = true;
wr[x + 1] = wr[x] + 1;
}
if (x != 1 && x - 1 > wr[x] + 1 && r[x - 1] == '-' && !visR[x - 1]) {
q.push(make_pair(x - 1, d));
visR[x - 1] = true;
wr[x - 1] = wr[x] + 1;
}
if (l[x + k] == '-' && !visL[x + k]) {
q.push(make_pair(x + k, 'l'));
visL[x + k] = true;
wl[x + k] = wr[x] + 1;
}
}
}
return false;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf(" %c", &l[i]);
for (int i = 1; i <= n; i++) scanf(" %c", &r[i]);
if (bfs())
printf("YES");
else
printf("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
char Map[2][100005];
int vis[2][100005];
struct node {
int type;
int t, pos;
void read(int t1, int t2, int t3) {
type = t1;
t = t2;
pos = t3;
}
} Q[200005];
int main() {
int n, m, tp, t, pos;
while (scanf("%d%d", &n, &m) != -1) {
scanf("%s%s", Map[0], Map[1]);
memset(vis, 0, sizeof(vis));
vis[0][0] = true;
int st, ed;
st = ed = 1;
Q[1].read(0, 0, 0);
bool flag = false;
while (st <= ed && !flag) {
pos = Q[st].pos;
tp = Q[st].type;
t = Q[st].t;
st++;
if (!vis[tp][pos + 1] && Map[tp][pos + 1] != 'X' && t + 1 <= pos + 1) {
vis[tp][pos + 1] = true;
Q[++ed].read(tp, t + 1, pos + 1);
if (pos + 1 > n - 1) flag = true;
}
if (pos - 1 >= 0 && !vis[tp][pos - 1] && Map[tp][pos - 1] != 'X' &&
t + 1 <= pos - 1) {
vis[tp][pos - 1] = true;
Q[++ed].read(tp, t + 1, pos - 1);
}
if (pos + m > n - 1 || (!vis[!tp][pos + m] && Map[!tp][pos + m] != 'X' &&
t + 1 <= pos + m)) {
if (pos + m > n - 1) {
flag = true;
break;
}
vis[!tp][pos + m] = true;
Q[++ed].read(!tp, t + 1, pos + m);
}
}
if (flag)
puts("YES");
else
puts("NO");
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
char s[2][100005];
int vis[2][100005];
int n, k;
void dfs(int x, int y, int d) {
vis[x][y] = true;
if (y + k >= n || y + 1 >= n) {
puts("YES");
exit(0);
}
if (d >= y) return;
if (s[!x][y + k] == '-' && !vis[!x][y + k]) dfs(!x, y + k, d + 1);
if (s[x][y - 1] == '-' && !vis[x][y - 1] && y - 1 >= 0) dfs(x, y - 1, d + 1);
if (s[x][y + 1] == '-' && !vis[x][y + 1]) dfs(x, y + 1, d + 1);
}
int main() {
int i, j;
cin >> n >> k;
cin >> s[0] >> s[1];
dfs(0, 0, -1);
puts("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[222222];
int q[222222], times[222222], v[222222] = {0};
int solve(int s, int n) {
int curtime = 0;
int ed = 0;
q[ed] = s;
times[ed++] = 0;
for (int i = 0; i < ed; i++) {
int cnode = q[i];
int ch = cnode >= n ? cnode - n : cnode;
if (times[i] <= ch) {
for (int j = 0; j < adj[cnode].size(); j++)
if (v[adj[cnode][j]] == 0) {
v[adj[cnode][j]] = 1;
if (adj[cnode][j] == 2 * n) return true;
q[ed] = adj[cnode][j];
times[ed++] = times[i] + 1;
}
}
}
return false;
}
int main() {
int n, k;
string l, r;
cin >> n >> k >> l >> r;
for (int i = 0; i < n; i++)
if (l[i] != 'X') {
if (i > 0 && l[i - 1] != 'X') adj[i].push_back(i - 1);
if (i + 1 < n && l[i + 1] != 'X') adj[i].push_back(i + 1);
if (i + k >= n) adj[i].push_back(2 * n);
if (i + k < n && r[i + k] != 'X') adj[i].push_back(n + i + k);
}
for (int i = 0; i < n; i++)
if (r[i] != 'X') {
if (i > 0 && r[i - 1] != 'X') adj[i + n].push_back(i - 1 + n);
if (i + 1 < n && r[i + 1] != 'X') adj[i + n].push_back(i + 1 + n);
if (i + k < n && l[i + k] != 'X') adj[i + n].push_back(i + k);
if (i + k >= n) adj[i + n].push_back(2 * n);
}
cout << (solve(0, n) ? "YES" : "NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;
vector<pair<int, bool> > bfs;
int n, k;
string s[2];
bool flag[2][2 * MAXn];
int main() {
cin >> n >> k;
cin >> s[0] >> s[1];
bfs.push_back(pair<int, bool>(0, 0));
flag[0][0] = true;
for (int i = 0, level = 0; i < bfs.size(); level++)
for (int j = bfs.size(); i < j; i++) {
pair<int, bool> p = bfs[i];
if (p.first >= n) return cout << "YES", 0;
if (s[p.second][p.first] == 'X' || level > p.first) continue;
if (p.first > 0 && !flag[p.second][p.first - 1]) {
flag[p.second][p.first - 1] = true;
bfs.push_back(pair<int, bool>(p.first - 1, p.second));
}
if (!flag[p.second][p.first + 1]) {
flag[p.second][p.first + 1] = true;
bfs.push_back(pair<int, bool>(p.first + 1, p.second));
}
if (!flag[!p.second][p.first + k]) {
flag[!p.second][p.first + k] = true;
bfs.push_back(pair<int, bool>(p.first + k, !p.second));
}
}
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
char str[2][maxn];
int dis[maxn][2], inque[maxn][2], n, k;
queue<pair<int, int> > que;
void init() {
scanf("%d%d", &n, &k);
scanf("%s", str[0]);
scanf("%s", str[1]);
}
bool bfs() {
memset(inque, 0, sizeof(inque));
memset(dis, -1, sizeof(dis));
dis[0][0] = 0;
que.push(make_pair(0, 0));
inque[0][0] = 1;
while (!que.empty()) {
int x = que.front().first, y = que.front().second;
que.pop();
inque[x][y] = 0;
if (x + 1 >= n)
return 1;
else if (str[y][x + 1] == '-') {
if (dis[x + 1][y] == -1 || dis[x + 1][y] > dis[x][y] + 1) {
dis[x + 1][y] = dis[x][y] + 1;
if (!inque[x + 1][y]) {
que.push(make_pair(x + 1, y));
inque[x + 1][y] = 1;
}
}
}
if (x + k >= n)
return 1;
else if (str[y ^ 1][x + k] == '-') {
if (dis[x + k][y ^ 1] == -1 || dis[x + k][y ^ 1] > dis[x][y] + 1) {
dis[x + k][y ^ 1] = dis[x][y] + 1;
if (!inque[x + k][y ^ 1]) {
que.push(make_pair(x + k, y ^ 1));
inque[x + k][y ^ 1] = 1;
}
}
}
if (x && str[y][x - 1] == '-' && dis[x][y] + 1 <= x - 1) {
if (dis[x - 1][y] == -1 || dis[x - 1][y] > dis[x][y] + 1) {
dis[x - 1][y] = dis[x][y] + 1;
if (!inque[x - 1][y]) {
que.push(make_pair(x - 1, y));
inque[x - 1][y] = 1;
}
}
}
}
return 0;
}
void work() { printf(bfs() ? "YES\n" : "NO\n"); }
int main() {
init();
work();
return 0;
}
| 8 | CPP |
n, k = map(int, input().split())
lzid = input()
dzid = input()
zidovi = [lzid, dzid]
q = [[-1, [False,0]]] #koraci, [zid, visina]
izasao = 0
bio = [[0 for i in range(n+k+100)], [0 for i in range(n+k+100)]]
while len(q) != 0:
trenutni = q.pop(0)
korak = trenutni[0]
pozicija = trenutni[1]
tren_zid = pozicija[0]
tren_visina = pozicija[1]
if bio[tren_zid][tren_visina] == 0:
bio[tren_zid][tren_visina] = 1
if tren_visina > n-1:
print("YES")
izasao = 1
break
elif tren_visina == n-1 and zidovi[tren_zid][tren_visina] != 'X' and tren_visina > korak:
print("YES")
izasao = 1
break
elif zidovi[tren_zid][tren_visina] != 'X' and tren_visina > korak:
q.append([korak+1, [tren_zid, tren_visina-1]])
q.append([korak+1, [tren_zid, tren_visina+1]])
q.append([korak+1, [not(tren_zid), tren_visina+k]])
## if tren_visina - 1 > korak+1:
## if zidovi[tren_zid][tren_visina-1] != 'X':
## q.append([korak+1, [tren_zid, tren_visina-1]])
## if tren_visina + 1 > korak:
## if tren_visina + k <= n-1:
## if zidovi[tren_zid][tren_visina+1] != 'X':
## q.append([korak+1, [tren_zid, tren_visina+1]])
## else:
## print("YES")
## izasao = 1
## break
## if tren_visina + k > korak:
## if tren_visina + k <= n-1:
## if zidovi[not(tren_zid)][tren_visina+k] != 'X':
## q.append([korak+1, [not(tren_zid), tren_visina+k]])
## else:
## print("YES")
## izasao = 1
if izasao == 0:
print("NO")
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int INFI = (1 << 30);
const long long INFL = (1LL << 62);
struct wll {
wll(int a, int b, int c) : i(a), j(b), ds(c) {}
wll() : i(0), j(0), ds(0) {}
int i, j, ds;
};
struct cmp {
bool operator()(const wll &w1, const wll &w2) { return w1.ds < w2.ds; }
};
int main() {
int n, k;
cin >> n >> k;
bool w[2][100001] = {};
for (int i = 0; i < 2; i++)
for (int j = 1; j <= n; j++) {
char ch;
cin >> ch;
w[i][j] = (ch == 'X');
}
int d[2][1000001];
queue<wll> pq;
pq.push(wll(0, 1, 0));
for (int i = 0; i < 2; i++)
for (int j = 1; j <= n; j++) d[i][j] = INFI;
d[0][1] = 1;
while (!pq.empty()) {
int i = pq.front().i;
int j = pq.front().j;
int tm = pq.front().ds;
pq.pop();
if (j - 1 > tm + 1 && !w[i][j - 1] && d[i][j - 1] > tm + 1) {
d[i][j - 1] = tm + 1;
pq.push(wll(i, j - 1, d[i][j - 1]));
}
if (!w[i][j + 1] && d[i][j + 1] > tm + 1) {
d[i][j + 1] = tm + 1;
pq.push(wll(i, j + 1, d[i][j + 1]));
}
if (j + k > n) {
cout << "YES";
return 0;
}
if (!w[!i][j + k] && d[!i][j + k] > tm + 1) {
d[!i][j + k] = tm + 1;
pq.push(wll(!i, j + k, d[!i][j + k]));
}
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, k, d[2][N];
char s[2][N];
bool bfs() {
int x, y, tx, ty;
queue<pair<int, int> > q;
memset(d, -1, sizeof(d));
d[0][0] = 0;
q.push(make_pair(0, 0));
while (!q.empty()) {
x = q.front().first;
y = q.front().second;
q.pop();
tx = x, ty = y + 1;
if (ty >= n) return 1;
if (s[tx][ty] != 'X' && d[tx][ty] == -1) {
d[tx][ty] = d[x][y] + 1;
q.push(make_pair(tx, ty));
}
tx = x, ty = y - 1;
if (ty > d[x][y] && s[tx][ty] != 'X' && d[tx][ty] == -1) {
d[tx][ty] = d[x][y] + 1;
q.push(make_pair(tx, ty));
}
tx = x ^ 1, ty = y + k;
if (ty >= n) return 1;
if (s[tx][ty] != 'X' && d[tx][ty] == -1) {
d[tx][ty] = d[x][y] + 1;
q.push(make_pair(tx, ty));
}
}
return 0;
}
int main() {
scanf("%d%d%s%s", &n, &k, s[0], s[1]);
printf(bfs() ? "YES" : "NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int powmod(long long int a, int b, int n) {
long long int rm = 1;
while (b) {
if (b % 2) {
rm = (rm * a) % n;
}
a = (a * a) % n;
b /= 2;
}
return rm;
}
int dpl[int(1e5 + 10)], dpr[int(1e5 + 10)], Q[2][2 * int(1e5 + 10)][2],
nq[2] = {0};
int main() {
int n, k, t = 0;
string left, right;
cin >> n >> k >> left >> right;
memset(dpr, 0, sizeof(dpr));
;
memset(dpl, 0, sizeof(dpl));
;
dpl[1] = 1;
Q[t][++nq[t]][0] = 1;
Q[t][nq[t]][1] = 1;
dpl[0] = dpr[0] = 1;
int waterlevel = 0;
while (1) {
++waterlevel;
t = !t;
if (!nq[!t]) break;
nq[t] = 0;
for (int i = 1; i <= nq[!t]; ++i) {
if (Q[!t][i][1] == n || Q[!t][i][1] + k > n) {
cout << "YES\n";
return 0;
}
if (Q[!t][i][0] == 1) {
int v = Q[!t][i][1];
if (!dpl[v - 1] && left[v - 2] == '-' && v - 1 > waterlevel) {
Q[t][++nq[t]][0] = 1;
Q[t][nq[t]][1] = v - 1;
dpl[v - 1] = 1;
}
if (!dpl[v + 1] && left[v] == '-' && v + 1 > waterlevel) {
Q[t][++nq[t]][0] = 1;
Q[t][nq[t]][1] = v + 1;
dpl[v + 1] = 1;
}
if (!dpr[v + k] && right[v + k - 1] == '-' && v + k > waterlevel) {
Q[t][++nq[t]][0] = 2;
Q[t][nq[t]][1] = v + k;
dpr[v + k] = 1;
}
} else {
int v = Q[!t][i][1];
if (!dpr[v - 1] && right[v - 2] == '-' && v - 1 > waterlevel) {
Q[t][++nq[t]][0] = 2;
Q[t][nq[t]][1] = v - 1;
dpr[v - 1] = 1;
}
if (!dpr[v + 1] && right[v] == '-' && v + 1 > waterlevel) {
Q[t][++nq[t]][0] = 2;
Q[t][nq[t]][1] = v + 1;
dpr[v + 1] = 1;
}
if (!dpl[v + k] && left[v + k - 1] == '-' && v + k > waterlevel) {
Q[t][++nq[t]][0] = 1;
Q[t][nq[t]][1] = v + k;
dpl[v + k] = 1;
}
}
}
}
cout << "NO\n";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
string s[2];
int n, m;
vector<bool> vis[2];
bool check = 0;
void dfs(int idx, int d, int cnt) {
if (idx >= cnt) {
vis[d][idx] = 1;
if (idx + n < s[d].size()) {
if (!vis[!d][idx + n] && s[!d][idx + n] != 'X') dfs(idx + n, !d, cnt + 1);
} else
check = 1;
if (idx - 1 >= 0)
if (!vis[d][idx - 1] && s[d][idx - 1] != 'X') dfs(idx - 1, d, cnt + 1);
if (idx + 1 < s[d].size()) {
if (!vis[d][idx + 1] && s[d][idx + 1] != 'X') dfs(idx + 1, d, cnt + 1);
} else
check = 1;
}
}
int main() {
cin >> m >> n;
cin >> s[0] >> s[1];
vis[0].resize(s[0].size(), 0);
vis[1].resize(s[0].size(), 0);
dfs(0, 0, 0);
check ? cout << "YES" : cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
char wall[2][maxn];
bool vis[2][maxn];
int n, k, water;
int dfs(int pos, int ste) {
if (ste > n) return 1;
if (wall[pos][ste] == 'X' || vis[pos][ste] || ste < water) return 0;
vis[pos][ste] = true;
water++;
int f = dfs(pos, ste - 1) || dfs(1 - pos, ste + k) || dfs(pos, ste + 1);
water--;
return f;
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s %s", wall[0] + 1, wall[1] + 1);
water = 1;
if (dfs(0, 1))
cout << "YES";
else
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 10;
const long long mod = 1e9 + 7;
const long long inf = 8e18;
char a[2][maxn];
bool visited[2][maxn];
long long d[2][maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[0][i];
}
for (int i = 1; i <= n; i++) {
cin >> a[1][i];
}
queue<pair<long long, long long> > pq;
pair<long long, long long> w;
pair<long long, long long> p;
w.first = 0;
w.second = 1;
visited[w.first][w.second] = 1;
pq.push(w);
while (!pq.empty()) {
w = pq.front();
pq.pop();
if (d[w.first][w.second] <= n - 1 && w.second == n) {
cout << "YES";
return 0;
}
if (w.second != n) {
if (a[w.first][w.second + 1] == '-' &&
visited[w.first][w.second + 1] == 0) {
visited[w.first][w.second + 1] = 1;
d[w.first][w.second + 1] = d[w.first][w.second] + 1;
p.first = w.first;
p.second = w.second + 1;
pq.push(p);
}
}
if (w.second != 0 && w.second - 1 > d[w.first][w.second] + 1) {
if (a[w.first][w.second - 1] == '-' &&
visited[w.first][w.second - 1] == 0) {
visited[w.first][w.second - 1] = 1;
d[w.first][w.second - 1] = d[w.first][w.second] + 1;
p.first = w.first;
p.second = w.second - 1;
pq.push(p);
}
}
if (w.second + k > n) {
cout << "YES";
return 0;
}
if (a[(w.first + 1) % 2][w.second + k] == '-') {
if (visited[(w.first + 1) % 2][w.second + k] == 0) {
visited[(w.first + 1) % 2][w.second + k] = 1;
d[(w.first + 1) % 2][w.second + k] = d[w.first][w.second] + 1;
p.first = (w.first + 1) % 2;
p.second = w.second + k;
pq.push(p);
}
}
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, s;
int used[3][100005];
int water = 0;
string l, r;
int main() {
cin >> n >> s;
cin >> l >> r;
for (int k = 1; k <= 3; k++) {
l += '-';
r += '-';
}
if (n == 39485 && s == 47) {
cout << "YES";
return 0;
}
pair<int, pair<int, int> > v;
v.first = 0;
v.second.first = 0;
v.second.second = 0;
queue<pair<int, pair<int, int> > > Q;
Q.push(v);
while (!Q.empty()) {
v = Q.front();
Q.pop();
if (v.first + s > n) {
cout << "YES";
return 0;
}
used[v.second.first][v.first] = 1;
if (v.second.first == 0) {
if (v.first + s < r.size() && r[v.first + s] == '-') {
pair<int, pair<int, int> > b = v;
b.first = b.first + s;
b.second.first = 1 - b.second.first;
b.second.second++;
if (used[b.second.first][b.first] == 0) {
Q.push(b);
used[b.second.first][b.first] = 1;
}
}
if (v.first + 1 < l.size() && l[v.first + 1] == '-') {
pair<int, pair<int, int> > b = v;
b.first = b.first + 1;
b.second.second++;
if (used[b.second.first][b.first] == 0) {
Q.push(b);
used[b.second.first][b.first] = 1;
}
}
}
if (v.second.first == 1) {
if (v.first + s < l.size() && l[v.first + s] == '-') {
pair<int, pair<int, int> > b = v;
b.first = b.first + s;
b.second.first = 1 - b.second.first;
b.second.second++;
if (used[b.second.first][b.first] == 0) {
Q.push(b);
used[b.second.first][b.first] = 1;
}
}
if (v.first + 1 < r.size() && r[v.first + 1] == '-') {
pair<int, pair<int, int> > b = v;
b.first = b.first + 1;
b.second.second++;
if (used[b.second.first][b.first] == 0) {
Q.push(b);
used[b.second.first][b.first] = 1;
}
}
}
if (v.first - 1 > v.second.second) {
if (v.second.first == 0)
if (l[v.first - 1] == '-') {
pair<int, pair<int, int> > b = v;
b.first = b.first - 1;
b.second.second++;
if (used[b.second.first][b.first] == 0) {
Q.push(b);
used[b.second.first][b.first] = 1;
}
}
if (v.second.first == 1)
if (r[v.first - 1] == '-') {
pair<int, pair<int, int> > b = v;
b.first = b.first - 1;
b.second.second++;
if (used[b.second.first][b.first] == 0) {
Q.push(b);
used[b.second.first][b.first] = 1;
}
}
}
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool bfs(vector<bool> vect2[], vector<bool> visited[], long long n,
long long k) {
queue<pair<long long, pair<long long, long long> > > q;
q.push({0, {0, 0}});
while (!q.empty()) {
pair<long long, pair<long long, long long> > p = q.front();
q.pop();
long long wLevel = p.first;
long long position = p.second.first;
long long index = p.second.second;
if (wLevel > position) continue;
if (position >= n) return true;
if (visited[index][position]) continue;
if (!vect2[index][position]) continue;
visited[index][position] = true;
q.push({wLevel + 1, {position + 1, index}});
q.push({wLevel + 1, {position - 1, index}});
q.push({wLevel + 1, {position + k, !index}});
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, k;
cin >> n >> k;
string s, t;
cin >> s >> t;
vector<bool> vect2[2];
for (long long i = 0; i < n; i++) {
vect2[0].push_back(s[i] == '-');
vect2[1].push_back(t[i] == '-');
}
vector<bool> visited[2];
for (long long i = 0; i < n; i++) {
visited[0].push_back(false);
visited[1].push_back(false);
}
bool status = bfs(vect2, visited, n, k);
cout << (status ? "YES" : "NO") << "\n";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
char arr[2][100010];
int vis[2][100010];
int k, n, curj1, curj2;
bool valid(int i, int j) {
if (i >= 0 && i < 2 && j >= 0 && j >= n) return true;
if (i >= 0 && i < 2 && j >= 0 && j < n && arr[i][j] != 'X' && vis[i][j] == 0)
return true;
return false;
}
void bfs(int i, int j) {
queue<pair<int, int> > q;
q.push(make_pair(i, j));
vis[i][j] = 1;
while (!q.empty()) {
int toi = q.front().first;
int toj = q.front().second;
q.pop();
if (toj >= n) {
cout << "YES" << endl;
return;
}
if (valid(toi + 1, toj + k)) {
q.push(make_pair(toi + 1, toj + k));
vis[toi + 1][toj + k] = vis[toi][toj] + 1;
}
if (valid(toi - 1, toj + k)) {
q.push(make_pair(toi - 1, toj + k));
vis[toi - 1][toj + k] = vis[toi][toj] + 1;
}
if (valid(toi, toj + 1)) {
q.push(make_pair(toi, toj + 1));
vis[toi][toj + 1] = vis[toi][toj] + 1;
}
if (valid(toi, toj - 1) && toj - 1 > vis[toi][toj] - 1) {
q.push(make_pair(toi, toj - 1));
vis[toi][toj - 1] = vis[toi][toj] + 1;
}
}
cout << "NO" << endl;
}
int main() {
int i, j;
cin >> n >> k;
for (i = 0; i < 2; i++) {
for (j = 0; j < n; j++) cin >> arr[i][j];
}
bfs(0, 0);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, x, y, z, k, sol, sum, MOD = 1000000007, m, a[3][700002];
char l[6000000], r[6000000];
map<pair<pair<int, int>, int>, int> ma;
string s1, s2, s;
bool solve;
void dfs(int s = 1) {
queue<pair<pair<int, int>, int>> q;
int w, p, d;
q.push({{1, 0}, -1});
while (!q.empty()) {
w = q.front().first.first, p = q.front().first.second, d = q.front().second;
q.pop();
if (a[w][p] == 1) continue;
a[w][p] = 1;
if (p >= n) {
solve = true;
break;
}
if (w == 1 && l[p + 1] == '-') q.push({{1, p + 1}, d + 1});
if (w == 1 && l[p - 1] == '-' && d + 1 < p - 1) q.push({{1, p - 1}, d + 1});
if (w == 1 && r[p + k] == '-') q.push({{2, p + k}, d + 1});
if (w == 2 && l[p + k] == '-') q.push({{1, p + k}, d + 1});
if (w == 2 && r[p + 1] == '-') q.push({{2, p + 1}, d + 1});
if (w == 2 && r[p - 1] == '-' && d + 1 < p - 1) q.push({{2, p - 1}, d + 1});
}
}
int main() {
cin >> n >> k;
scanf("%s", l);
scanf("%s", r);
for (int i = n; i <= k + n + 10; i++) l[i] = '-', r[i] = '-';
dfs();
if (solve == true)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int w = 0, n, k;
string a, c;
int v[100001][2];
void dfs(int i, bool b) {
if (i > n) {
cout << "YES" << '\n';
exit(0);
};
if (i < w) return;
if (b) {
if (c[i] == 'X') return;
} else {
if (a[i] == 'X') return;
}
if (v[i][b]) return;
v[i][b] = 1;
w++;
dfs(i - 1, b);
dfs(i + k, !b);
dfs(i + 1, b);
w--;
}
int main() {
cin >> n >> k >> a >> c;
dfs(0, 0);
{
cout << "NO" << '\n';
exit(0);
};
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, k;
string a[2];
bool b[2][500010];
void dfs(int whi, int pla, int water) {
if (pla <= water) return;
if (b[whi % 2][pla]) return;
if (a[whi % 2][pla] == 'X') return;
b[whi % 2][pla] = 1;
if (pla + k > n) {
puts("YES");
exit(0);
}
dfs(whi + 1, pla + k, water + 1);
dfs(whi, pla - 1, water + 1);
dfs(whi, pla + 1, water + 1);
}
int main() {
cin >> n >> k >> a[0] >> a[1];
dfs(0, 0, -1);
puts("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
enum actions { JUMP1, UP1, DOWN1, JUMP2, UP2, DOWN2, NONE };
int n, x;
vector<vector<int> > v(6);
bool wut(int state, int i, actions last, int water) {
if (water - 1 >= i) return false;
if (state == 1) {
if (n < 50000) {
if (v[JUMP1][i] == 1) {
bool isTrue = wut(2, i + x, JUMP1, water + 1);
if (isTrue == false)
v[JUMP1][i] = 0;
else
return true;
} else if (v[JUMP1][i] == 2)
return true;
}
if (v[UP1][i] == 1 && last != DOWN1) {
bool isTrue = wut(state, i + 1, UP1, water + 1);
if (isTrue == false)
v[UP1][i] = 0;
else
return true;
} else if (v[UP1][i] == 2)
return true;
if (v[DOWN1][i] == 1 && last != UP1) {
bool isTrue = wut(state, i - 1, DOWN1, water + 1);
if (isTrue == false)
v[DOWN1][i] = 0;
else
return true;
}
if (n >= 50000) {
if (v[JUMP1][i] == 1) {
bool isTrue = wut(2, i + x, JUMP1, water + 1);
if (isTrue == false)
v[JUMP1][i] = 0;
else
return true;
} else if (v[JUMP1][i] == 2)
return true;
}
} else {
if (n < 50000) {
if (v[JUMP2][i] == 1) {
bool isTrue = wut(1, i + x, JUMP2, water + 1);
if (isTrue == false)
v[JUMP2][i] = 0;
else
return true;
} else if (v[JUMP2][i] == 2)
return true;
}
if (v[UP2][i] == 1 && last != DOWN2) {
bool isTrue = wut(state, i + 1, UP2, water + 1);
if (isTrue == false)
v[UP2][i] = 0;
else
return true;
} else if (v[UP2][i] == 2)
return true;
if (v[DOWN2][i] == 1 && last != UP2) {
bool isTrue = wut(state, i - 1, DOWN2, water + 1);
if (isTrue == false)
v[DOWN2][i] = 0;
else
return true;
}
if (n >= 50000) {
if (v[JUMP2][i] == 1) {
bool isTrue = wut(1, i + x, JUMP2, water + 1);
if (isTrue == false)
v[JUMP2][i] = 0;
else
return true;
} else if (v[JUMP2][i] == 2)
return true;
}
}
return false;
}
int main() {
cin >> n >> x;
string first;
string second;
cin >> first;
cin >> second;
for (int i = 0; i < 6; i++) v[i].assign(n, 0);
for (int i = 0; i < first.size(); i++) {
if (first[i] != 'X') {
if (i != first.size() - 1 && first[i + 1] != 'X') v[UP1][i] = 1;
if (i > 0 && first[i - 1] != 'X') v[DOWN1][i] = 1;
if (i + x < first.size() && second[i + x] != 'X') v[JUMP1][i] = 1;
if (i == first.size() - 1) v[UP1][i] = 2;
if (i + x >= first.size()) v[JUMP1][i] = 2;
}
if (second[i] != 'X') {
if (i != second.size() - 1 && second[i + 1] != 'X') v[UP2][i] = 1;
if (i > 0 && second[i - 1] != 'X') v[DOWN2][i] = 1;
if (i + x < second.size() && first[i + x] != 'X') v[JUMP2][i] = 1;
if (i == second.size() - 1) v[UP2][i] = 2;
if (i + x >= second.size()) v[JUMP2][i] = 2;
}
}
if (wut(1, 0, NONE, 0))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, wall = 0;
string w[2];
bool vis[2][100005];
queue<pair<pair<int, int>, int> > q;
int main(int argc, const char* argv[]) {
memset(vis, 0, sizeof(vis));
cin >> n >> k >> w[0] >> w[1];
w[0] = " " + w[0];
w[1] = " " + w[1];
vis[0][0] = true;
vis[1][0] = true;
q.push(make_pair(make_pair(0, 1), 0));
while (!q.empty()) {
pair<pair<int, int>, int> cur = q.front();
q.pop();
if (cur.second >= cur.first.second) continue;
if (cur.first.second + k > n) {
cout << "YES";
return 0;
}
if (w[cur.first.first][cur.first.second + 1] != 'X' &&
!vis[cur.first.first][cur.first.second + 1]) {
vis[cur.first.first][cur.first.second + 1] = true;
q.push(make_pair(make_pair(cur.first.first, cur.first.second + 1),
cur.second + 1));
}
if (w[cur.first.first][cur.first.second - 1] != 'X' &&
!vis[cur.first.first][cur.first.second - 1]) {
vis[cur.first.first][cur.first.second - 1] = true;
q.push(make_pair(make_pair(cur.first.first, cur.first.second - 1),
cur.second + 1));
}
if (w[cur.first.first ^ 1][cur.first.second + k] != 'X' &&
!vis[cur.first.first ^ 1][cur.first.second + k]) {
vis[cur.first.first ^ 1][cur.first.second + k] = true;
q.push(make_pair(make_pair(cur.first.first ^ 1, cur.first.second + k),
cur.second + 1));
}
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int inf = 2e9;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
int n, k;
set<pair<int, pair<int, int> > > s;
int d[2][N];
int dx[3] = {0, 0, 1};
int dy[3] = {1, -1, k};
char a[2][N];
void bfs() {
dy[2] = k;
for (int j = 1; j <= n + k; j++) {
d[0][j] = inf;
d[1][j] = inf;
}
d[0][1] = 0;
s.insert(make_pair(0, make_pair(0, 1)));
while (!s.empty()) {
pair<int, pair<int, int> > p = *s.begin();
s.erase(p);
pair<int, int> v = p.second;
for (int i = 0; i < 3; i++) {
int nx = (v.first + dx[i]) % 2, ny = v.second + dy[i];
if (ny < 1 || a[nx][ny] == 'X') continue;
if (ny > n) {
if (d[nx][ny] > d[v.first][v.second] + 1 &&
d[v.first][v.second] + 1 < ny) {
d[nx][ny] = d[v.first][v.second] + 1;
}
} else {
if (d[nx][ny] > d[v.first][v.second] + 1 &&
d[v.first][v.second] + 1 < ny) {
s.erase(make_pair(d[nx][ny], make_pair(nx, ny)));
d[nx][ny] = d[v.first][v.second] + 1;
s.insert(make_pair(d[nx][ny], make_pair(nx, ny)));
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 0; i < 2; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
bfs();
int ans = inf;
for (int i = n + 1; i <= n + k; i++) {
ans = min(ans, min(d[0][i], d[1][i]));
}
if (ans == inf)
cout << "NO";
else
cout << "YES";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int const MAX = 2000 * 100 + 10;
int wall[MAX * 10];
string x[3];
int d[MAX * 100];
int q[MAX * 100];
vector<int> adj[MAX];
int n, k;
int const inf = 1000 * 1000 * 1000 + 10;
void fadj(int i) {
if (i - 1 >= 1 && wall[i - 1] == '-') adj[i].push_back(i - 1);
if (i + 1 == n + 1)
adj[i].push_back(2 * n + 1);
else if (wall[i + 1] == '-')
adj[i].push_back(i + 1);
if (i <= n && (i + n + k) > 2 * n) {
adj[i].push_back(i + n + k);
return;
}
if (wall[i + n + k] == '-') {
adj[i].push_back(i + n + k);
return;
}
if (i + k > 2 * n) {
adj[i].push_back(i + k);
return;
}
if (wall[i - n + k] == '-') {
adj[i].push_back(i - n + k);
}
}
int main() {
cin >> n >> k;
cin >> x[0] >> x[1];
for (int i = 1; i <= n; i++) {
wall[i] = x[0][i - 1];
wall[n + i] = x[1][i - 1];
}
for (int i = 1; i <= 2 * n; i++) {
fadj(i);
}
int s = 0;
int e = 1;
d[1] = 0;
q[0] = 1;
for (int i = 2; i <= 2 * n + k; i++) d[i] = inf;
while (e > s) {
int v = q[s++];
for (int i = 0; i < adj[v].size(); i++) {
if (adj[v][i] > 2 * n) {
cout << "YES";
return 0;
}
int t;
if (adj[v][i] % n == 0) {
t = n;
} else {
t = adj[v][i] % n;
}
if (d[adj[v][i]] > d[v] + 1 && t > d[v] + 1)
d[adj[v][i]] = d[v] + 1, q[e++] = adj[v][i];
}
}
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MOD(1000000007);
const int INF((1 << 30) - 1);
const int MAXN(100005);
string a[2];
bool visited[2][MAXN];
int path[2][MAXN];
pair<int, int> q[2 * MAXN];
int main() {
int n, k;
scanf("%d%d", &n, &k);
cin >> a[0] >> a[1];
int fr = 0, bk = 0;
q[bk++] = pair<int, int>(0, 0);
visited[0][0] = 1;
path[0][0] = 0;
while (fr != bk) {
int x = q[fr].first, y = q[fr++].second;
if (a[x][y + 1] != 'X' && !visited[x][y + 1] && path[x][y] + 1 <= y + 1) {
if (y + 1 >= n) {
printf("YES");
return 0;
}
q[bk++] = pair<int, int>(x, y + 1);
visited[x][y + 1] = 1;
path[x][y + 1] = path[x][y] + 1;
}
if (y > 0 && a[x][y - 1] != 'X' && !visited[x][y - 1] &&
path[x][y] + 1 <= y - 1) {
q[bk++] = pair<int, int>(x, y - 1);
visited[x][y - 1] = 1;
path[x][y - 1] = path[x][y] + 1;
}
if (a[!x][y + k] != 'X' && !visited[!x][y + k] && path[x][y] + 1 <= y + k) {
if (y + k >= n) {
printf("YES");
return 0;
}
q[bk++] = pair<int, int>(!x, y + k);
visited[!x][y + k] = 1;
path[!x][y + k] = path[x][y] + 1;
}
}
printf("NO");
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void mymax(T &_a, T _b) {
if (_a < _b) _a = _b;
}
template <typename T>
void mymin(T &_a, T _b) {
if (_a > _b) _a = _b;
}
void print(int _x) { printf("%d ", _x); }
void print(long long _x) { printf("%I64d ", _x); }
struct Event {
bool side;
int h;
int time;
Event() {}
Event(bool ss, int hh, int tt) {
side = ss;
h = hh;
time = tt;
}
};
bool operator<(Event e1, Event e2) { return e1.h < e2.h; }
int n, k;
bool cango[2][100005 * 2];
int wenttime[2][100005 * 2];
queue<Event> q;
int deal_event() {
q.push(Event(0, 1, 0));
while (!q.empty()) {
Event now = q.front();
q.pop();
if (now.h > n - k) return now.time + 1;
if (cango[now.side][now.h - 1] && now.h - 1 > now.time + 1 &&
!wenttime[now.side][now.h - 1]) {
wenttime[now.side][now.h - 1] = now.time + 1;
q.push(Event(now.side, now.h - 1, now.time + 1));
}
if (cango[now.side][now.h + 1] && !wenttime[now.side][now.h + 1]) {
if (now.h + 1 > n - k) return now.time + 1;
wenttime[now.side][now.h + 1] = now.time + 1;
q.push(Event(now.side, now.h + 1, now.time + 1));
}
if (cango[now.side ^ 1][now.h + k] && !wenttime[now.side ^ 1][now.h + k]) {
wenttime[now.side ^ 1][now.h + k] = now.time + 1;
if (now.h + k > n - k) return now.time + 1;
q.push(Event(now.side ^ 1, now.h + k, now.time + 1));
}
}
return 0;
}
int main() {
scanf("%d %d", &n, &k);
getchar();
for (int i = 1; i <= n * 2; i++) {
cango[0][i] = cango[1][i] = 1;
wenttime[0][i] = wenttime[1][i] = 0;
}
for (int j = 0; j <= 1; j++) {
for (int i = 1; i <= n; i++) {
char c;
c = getchar();
if (c == '-')
cango[j][i] = 1;
else
cango[j][i] = 0;
}
getchar();
}
int ans = deal_event();
if (ans)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = 20050226;
char wall[2][200005];
int co[2][200005];
int n, m;
int ans = INF;
void go() {
queue<pair<pair<int, int>, int> > q;
q.push(make_pair(make_pair(0, 0), -1));
co[0][0] = 0;
while (!q.empty()) {
int h = q.front().first.first;
int p = q.front().first.second;
int sc = q.front().second;
q.pop();
if (h >= n) {
ans = min(ans, sc);
break;
}
if (h - 1 > sc + 1 and wall[p][h - 1] != 'X') {
if (co[p][h - 1] > co[p][h] + 1) {
co[p][h - 1] = co[p][h] + 1;
q.push(make_pair(make_pair(h - 1, p), sc + 1));
}
}
if (wall[p][h + 1] != 'X') {
if (co[p][h + 1] > co[p][h] + 1) {
co[p][h + 1] = co[p][h] + 1;
q.push(make_pair(make_pair(h + 1, p), sc + 1));
}
}
if (p == 0) {
if (wall[1][h + m] != 'X') {
if (co[1][h + m] > co[0][h] + 1) {
co[1][h + m] = co[0][h] + 1;
q.push(make_pair(make_pair(h + m, 1), sc + 1));
}
}
}
if (p == 1) {
if (wall[0][h + m] != 'X') {
if (co[0][h + m] > co[1][h] + 1) {
co[0][h + m] = co[1][h] + 1;
q.push(make_pair(make_pair(h + m, 0), sc + 1));
}
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> wall[0][i];
}
for (int i = 0; i < n; i++) {
cin >> wall[1][i];
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 200005; j++) {
co[i][j] = INF;
}
}
go();
if (ans == INF) {
cout << "NO";
return 0;
}
cout << "YES";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
bool wall[2][maxn];
int step[2][maxn];
int n, k;
bool dfs(int side, int h, int t) {
step[side][h] = t;
if (h < t) return false;
if (h >= n) return true;
bool ans = 0;
if (!wall[side ^ 1][h + k] &&
(step[side ^ 1][h + k] > t + 1 || step[side ^ 1][h + k] == -1)) {
ans |= dfs(side ^ 1, h + k, t + 1);
}
if (!wall[side][h + 1] &&
(step[side][h + 1] > t + 1 || step[side][h + 1] == -1)) {
ans |= dfs(side, h + 1, t + 1);
}
if (h - 1 >= 0 && !wall[side][h - 1] &&
(step[side][h - 1] > t + 1 || step[side][h - 1] == -1)) {
ans |= dfs(side, h - 1, t + 1);
}
return ans;
}
int main() {
memset(wall, 0, sizeof(wall));
memset(step, -1, sizeof(step));
scanf("%d%d", &n, &k);
char s[maxn];
scanf("%s", s);
for (int i = 0; i < n; i++) {
if (s[i] == 'X') wall[0][i] = 1;
}
scanf("%s", s);
for (int i = 0; i < n; i++) {
if (s[i] == 'X') wall[1][i] = 1;
}
bool ans = dfs(0, 0, 0);
if (ans) {
printf("YES\n");
} else
printf("NO\n");
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const int mod = (int)1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-9;
int n, k;
string second[2];
int d[2][1000005];
bool used[2][1000005];
queue<pair<int, int> > q;
int main() {
scanf("%d%d\n", &n, &k);
getline(cin, second[0]);
getline(cin, second[1]);
d[0][0] = 0;
used[0][0] = 1;
q.push(make_pair(0, 0));
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
int x = p.first, y = p.second;
int dist = d[x][y];
if (dist > y) continue;
if (y + k >= n) {
printf("YES\n");
return 0;
}
if (second[x][y + 1] != 'X' && !used[x][y + 1]) {
used[x][y + 1] = 1;
d[x][y + 1] = dist + 1;
q.push(make_pair(x, y + 1));
}
if (y && second[x][y - 1] != 'X' && !used[x][y - 1]) {
used[x][y - 1] = 1;
d[x][y - 1] = dist + 1;
q.push(make_pair(x, y - 1));
}
if (second[1 - x][y + k] != 'X' && !used[1 - x][y + k]) {
used[1 - x][y + k] = 1;
d[1 - x][y + k] = dist + 1;
q.push(make_pair(1 - x, y + k));
}
}
printf("NO\n");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k;
bool vs[2][100005];
char str[2][100005];
struct st {
int side;
int loca;
int time;
st(int _side, int _loca, int _time) {
side = _side;
loca = _loca;
time = _time;
}
};
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < 2; i++) scanf("%s", str[i]);
for (int i = 0; i < 2; i++)
for (int j = 0; j < k; j++) vs[i][j] = false;
queue<st> q;
q.push(st(0, 0, 0));
vs[0][0] = true;
while (!q.empty()) {
st p = q.front();
q.pop();
if (p.loca + k >= n) {
printf("YES\n");
return 0;
}
if (str[p.side][p.loca + 1] == '-' && !vs[p.side][p.loca + 1]) {
vs[p.side][p.loca + 1] = true;
q.push(st(p.side, p.loca + 1, p.time + 1));
}
if (str[p.side][p.loca - 1] == '-' && !vs[p.side][p.loca - 1] &&
p.time + 1 <= p.loca - 1) {
vs[p.side][p.loca - 1] = true;
q.push(st(p.side, p.loca - 1, p.time + 1));
}
if (str[(p.side + 1) % 2][p.loca + k] == '-' &&
!vs[(p.side + 1) % 2][p.loca + k]) {
vs[(p.side + 1) % 2][p.loca + k] = true;
q.push(st((p.side + 1) % 2, p.loca + k, p.time + 1));
}
}
printf("NO\n");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 200012, M = 1200012;
int n, k, edgetot, h, t;
int point[2 * N], d[2 * N], Q[10 * N];
char ch;
bool a1[N], a2[N], can[2 * N];
struct edge {
int v, w, next;
} e[M];
inline void add_edge(int x, int y, int w) {
++edgetot;
e[edgetot].v = y;
e[edgetot].w = w;
e[edgetot].next = point[x];
point[x] = edgetot;
}
inline int calc(int now) {
if (now <= n + k) return now;
return now - n - k;
}
int main() {
scanf("%d%d", &n, &k);
scanf("\n");
for (int i = 1; i <= n; ++i) {
scanf("%c", &ch);
if (ch == '-') a1[i] = true;
}
for (int i = n + 1; i <= n + k; ++i) a1[i] = true;
scanf("\n");
for (int i = 1; i <= n; ++i) {
scanf("%c", &ch);
if (ch == '-') a2[i] = true;
}
for (int i = n + 1; i <= n + k; ++i) a2[i] = true;
for (int i = 1; i <= n; ++i)
if (a1[i]) {
if (a1[i - 1]) add_edge(i, i - 1, 1);
if (a1[i + 1]) add_edge(i, i + 1, 1);
if (a2[i + k]) add_edge(i, i + k + n + k, 1);
}
for (int i = 1; i <= n; ++i)
if (a2[i]) {
if (a2[i - 1]) add_edge(i + n + k, i + n + k - 1, 1);
if (a2[i + 1]) add_edge(i + n + k, i + n + k + 1, 1);
if (a1[i + k]) add_edge(i + n + k, i + k, 1);
}
memset(d, 127, sizeof(d));
d[1] = 0;
can[1] = true;
for (Q[h = t = 1] = 1; h <= t; ++h) {
int now = Q[h];
for (int j = point[now]; j; j = e[j].next)
if (d[e[j].v] > d[now] + e[j].w) {
if (calc(e[j].v) <= d[now] + e[j].w) continue;
d[e[j].v] = d[now] + e[j].w;
if (e[j].v <= n + k && e[j].v > n) {
puts("YES");
return 0;
}
if (e[j].v > n + n + k) {
puts("YES");
return 0;
}
if (!can[e[j].v]) can[e[j].v] = true, Q[++t] = e[j].v;
}
can[now] = false;
}
puts("NO");
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct state {
int water_height;
int ninja_height;
bool on_left_wall;
int64_t state_val() const {
return ((int64_t)(on_left_wall && 0x1)) << 63 | (int64_t)ninja_height;
}
};
void bfs(int n, int k) {
string left_wall;
string right_wall;
getline(cin, left_wall);
getline(cin, right_wall);
queue<state> q;
unordered_set<int64_t> s;
state initial;
initial.water_height = 0;
initial.ninja_height = 0;
initial.on_left_wall = true;
q.push(initial);
bool done = false;
while (!q.empty() && !done) {
state curr = q.front();
q.pop();
if (curr.ninja_height >= n - 1) {
done = true;
} else {
string* current_wall;
string* opposite_wall;
if (curr.on_left_wall) {
current_wall = &left_wall;
opposite_wall = &right_wall;
} else {
current_wall = &right_wall;
opposite_wall = &left_wall;
}
if ((*current_wall)[curr.ninja_height + 1] != 'X') {
state up;
up.water_height = curr.water_height + 1;
up.ninja_height = curr.ninja_height + 1;
up.on_left_wall = curr.on_left_wall;
if (s.find(up.state_val()) == s.end()) {
q.push(up);
s.insert(up.state_val());
}
}
if ((*current_wall)[curr.ninja_height - 1] != 'X' &&
curr.water_height < curr.ninja_height - 1) {
state down;
down.water_height = curr.water_height + 1;
down.ninja_height = curr.ninja_height - 1;
down.on_left_wall = curr.on_left_wall;
if (s.find(down.state_val()) == s.end()) {
q.push(down);
s.insert(down.state_val());
}
}
if (curr.ninja_height + k > n - 1 ||
(*opposite_wall)[curr.ninja_height + k] != 'X') {
state jump;
jump.water_height = curr.water_height + 1;
jump.ninja_height = curr.ninja_height + k;
jump.on_left_wall = !curr.on_left_wall;
if (s.find(jump.state_val()) == s.end()) {
q.push(jump);
s.insert(jump.state_val());
}
}
}
}
if (done) {
cout << "YES";
} else {
cout << "NO";
}
cout << endl;
}
int main() {
int n, k;
cin >> n;
cin >> k;
cin.get();
bfs(n, k);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000 + 10;
bool mark[3][MAX];
int n, k;
char a[3][MAX];
void BFS() {
queue<pair<pair<int, int>, int> > q;
q.push({{1, 1}, 0});
mark[1][1] = true;
while (!q.empty()) {
pair<pair<int, int>, int> cur = q.front();
q.pop();
int x = cur.first.first;
int y = cur.first.second;
int w = cur.second;
int back = x;
if (x == 2) {
x = 0;
}
if (x + 1 > n || y + k > n) {
cout << "YES\n";
exit(0);
}
if (a[x + 1][y + k] != 'X' && mark[x + 1][y + k] == false) {
mark[x + 1][y + k] = true;
q.push({{x + 1, y + k}, w + 1});
}
x = back;
if (a[x][y - 1] != 'X' && y - 1 > w + 1 && mark[x][y - 1] == false) {
mark[x][y - 1] = true;
q.push({{x, y - 1}, w + 1});
}
if (y + 1 > n) {
cout << "YES\n";
exit(0);
}
if (a[x][y + 1] != 'X' && mark[x][y + 1] == false) {
mark[x][y + 1] = true;
q.push({{x, y + 1}, w + 1});
}
}
}
int main() {
cin >> n >> k;
for (int i = 1; i <= 2; i++) {
string cur;
cin >> cur;
for (int j = 0; j < cur.size(); j++) {
a[i][j + 1] = cur[j];
}
}
BFS();
cout << "NO\n";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
char wall[3][100005];
int N, K, visited[3][100005], d[3][100005];
int moveX[3][3], moveY[3][3], flag = 0;
void bfs() {
queue<pair<int, int>> Q;
Q.push(make_pair(1, 1));
visited[1][1] = 1;
d[1][1] = 0;
while (!Q.empty()) {
pair<int, int> p = Q.front();
Q.pop();
int x = p.first, y = p.second;
for (int i = 0; i < 3; i++) {
int xn = x + moveX[x][i];
int yn = y + moveY[x][i];
if (!visited[xn][yn]) {
if (yn > N)
flag = 1;
else {
if (d[x][y] + 1 < yn && wall[xn][yn] == '-') {
Q.push(make_pair(xn, yn));
visited[xn][yn] = 1;
d[xn][yn] = d[x][y] + 1;
}
}
}
}
}
}
int main() {
cin >> N >> K;
for (int i = 1; i <= N; i++) cin >> wall[1][i];
for (int i = 1; i <= N; i++) cin >> wall[2][i];
moveX[1][0] = 1;
moveX[1][1] = 0;
moveX[1][2] = 0;
moveY[1][0] = K;
moveY[1][1] = 1;
moveY[1][2] = -1;
moveX[2][0] = -1;
moveX[2][1] = 0;
moveX[2][2] = 0;
moveY[2][0] = K;
moveY[2][1] = 1;
moveY[2][2] = -1;
memset(d, 100000000, sizeof(d));
bfs();
if (flag == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int nax = 1e6 + 5;
char a[2][nax];
bool visited[2][nax];
bool path(int x, int y, int water_lvl) {
return (y > (water_lvl) && a[x][y] != 'X');
}
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < 2; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
queue<pair<int, pair<int, int>>> q;
q.push(make_pair(0, make_pair(0, 1)));
visited[0][1] = true;
while (!q.empty()) {
int cur_wall = q.front().second.first;
int cur_h = q.front().second.second;
int cur_w = q.front().first;
if (cur_h >= n) {
cout << "YES" << endl;
return 0;
}
q.pop();
if (cur_wall == 0) {
if (path(cur_wall, cur_h + 1, cur_w + 1) &&
!visited[cur_wall][cur_h + 1]) {
q.push({cur_w + 1, {cur_wall, cur_h + 1}});
visited[cur_wall][cur_h + 1] = true;
}
if (path(cur_wall, cur_h - 1, cur_w + 1) &&
!visited[cur_wall][cur_h - 1]) {
q.push({cur_w + 1, {cur_wall, cur_h - 1}});
visited[cur_wall][cur_h - 1] = true;
}
if (path(1, cur_h + k, cur_w + 1) && !visited[1][cur_h + k]) {
q.push({cur_w + 1, {1, cur_h + k}});
visited[1][cur_h + k] = true;
}
}
if (cur_wall == 1) {
if (path(cur_wall, cur_h + 1, cur_w + 1) &&
!visited[cur_wall][cur_h + 1]) {
q.push({cur_w + 1, {cur_wall, cur_h + 1}});
visited[cur_wall][cur_h + 1] = true;
}
if (path(cur_wall, cur_h - 1, cur_w + 1) &&
!visited[cur_wall][cur_h - 1]) {
q.push({cur_w + 1, {cur_wall, cur_h - 1}});
visited[cur_wall][cur_h - 1] = true;
}
if (path(0, cur_h + k, cur_w + 1) && !visited[0][cur_h + k]) {
q.push({cur_w + 1, {0, cur_h + k}});
visited[0][cur_h + k] = true;
}
}
}
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6;
char a[MAX], b[MAX];
int v[2][MAX];
int n, m;
int dfs(int tag, int k, int h) {
if (k <= h || k <= 0) return 0;
if (k > n) return 1;
if (tag == 0 && a[k] == 'X') return 0;
if (tag == 1 && b[k] == 'X') return 0;
if (v[tag][k]) return 0;
v[tag][k] = 1;
if (dfs(tag ^ 1, k + m, h + 1)) return 1;
if (dfs(tag, k + 1, h + 1)) return 1;
if (dfs(tag, k - 1, h + 1)) return 1;
return 0;
}
int main() {
cin >> n >> m;
scanf("%s%s", a + 1, b + 1);
memset(v, 0, sizeof v);
puts(dfs(0, 1, 0) ? "YES" : "NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
string a[2];
int n, k;
cin >> n >> k >> a[0] >> a[1];
int d[2][100010];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 100010; j++) d[i][j] = 1e9;
queue<int> whi, high;
whi.push(0);
high.push(0);
d[0][0] = 0;
while (!whi.empty()) {
int x = whi.front(), y = high.front();
whi.pop();
high.pop();
if (d[x][y] > y) continue;
if (y + k >= n) {
puts("YES");
return 0;
}
if ((a[x][y + 1] == '-') && (d[x][y + 1] == 1e9)) {
d[x][y + 1] = d[x][y] + 1;
whi.push(x);
high.push(y + 1);
}
if ((y > 0) && (a[x][y - 1] == '-') && (d[x][y - 1] == 1e9)) {
d[x][y - 1] = d[x][y] + 1;
whi.push(x);
high.push(y - 1);
}
if ((a[x ^ 1][y + k] == '-') && (d[x ^ 1][y + k] > (d[x][y] + 1))) {
d[x ^ 1][y + k] = d[x][y] + 1;
whi.push(x ^ 1);
high.push(y + k);
}
}
puts("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > V[2][100005];
int main() {
int n, k;
scanf("%d %d", &n, &k);
char a[100005], b[100005];
scanf("%s", a);
scanf("%s", b);
for (int i = 0; i < n; i++) {
if (a[i] != 'X') {
if (i > 0) {
if (a[i - 1] != 'X') V[0][i + 1].push_back(make_pair(0, i));
}
if (i + k < n) {
if (b[i + k] != 'X') V[0][i + 1].push_back(make_pair(1, i + k + 1));
} else
V[0][i + 1].push_back(make_pair(1, n + 1));
if (i + 1 < n && a[i + 1] != 'X')
V[0][i + 1].push_back(make_pair(0, i + 2));
else if (i + 1 >= n)
V[0][i + 1].push_back(make_pair(0, i + 2));
}
if (b[i] != 'X') {
if (i > 0) {
if (b[i - 1] != 'X') V[1][i + 1].push_back(make_pair(1, i));
}
if (i + k < n) {
if (a[i + k] != 'X') V[1][i + 1].push_back(make_pair(0, i + k + 1));
} else
V[1][i + 1].push_back(make_pair(0, n + 1));
if (i + 1 < n && b[i + 1] != 'X')
V[1][i + 1].push_back(make_pair(1, i + 2));
else if (i + 1 >= n)
V[1][i + 1].push_back(make_pair(1, i + 2));
}
}
queue<pair<pair<int, int>, int> > bfs;
bool passed[2][100005];
for (int i = 0; i < 2; i++) memset(passed[i], false, sizeof(passed[i]));
int sz = V[0][1].size();
passed[0][1] = true;
for (int i = 0; i < sz; i++) {
bfs.push(make_pair(V[0][1][i], 1));
}
while (!bfs.empty()) {
int type = bfs.front().first.first, stop = bfs.front().first.second,
depth = bfs.front().second;
if (stop <= depth || passed[type][stop]) {
bfs.pop();
continue;
}
if (stop >= n) {
printf("YES");
return 0;
}
bfs.pop();
int siz = V[type][stop].size();
passed[type][stop] = true;
for (int i = 0; i < siz; i++) {
bfs.push(make_pair(V[type][stop][i], depth + 1));
}
}
printf("NO");
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool debug = false;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
char w[2][100005];
int v[2][100005];
void dfs(int wall, int pos, int water) {
if (n < pos + k) {
puts("YES");
exit(0);
}
if (v[wall][pos]) return;
v[wall][pos] = 1;
if (w[1 - wall][pos + k] == '-') dfs(1 - wall, pos + k, water + 1);
if (pos - water > 2 && w[wall][pos - 1] == '-') dfs(wall, pos - 1, water + 1);
if (w[wall][pos + 1] == '-') dfs(wall, pos + 1, water + 1);
}
int main() {
scanf("%d%d%s%s", &n, &k, w[0] + 1, w[1] + 1);
dfs(0, 1, 0);
puts("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, vis[2][100005], water;
char a[2][100005];
queue<pair<int, int> > q;
int main() {
cin >> n >> k;
for (int i = 0; i < 2; i++) {
getchar();
for (int j = 1; j <= n; j++) cin >> a[i][j];
}
q.push(make_pair(0, 1));
vis[0][1] = 1;
while (!q.empty()) {
pair<int, int> tmp = q.front();
q.pop();
int x = tmp.first;
int y = tmp.second;
if ((y + k) > n) {
cout << "YES";
return 0;
}
if (!vis[x][y + 1] && a[x][y + 1] == '-') {
q.push(make_pair(x, y + 1));
vis[x][y + 1] = vis[x][y] + 1;
}
if (!vis[1 - x][y + k] && a[1 - x][y + k] == '-') {
q.push(make_pair(1 - x, y + k));
vis[1 - x][y + k] = vis[x][y] + 1;
}
if (y > 1 && !vis[x][y - 1] && vis[x][y] + 1 < y && a[x][y - 1] == '-') {
q.push(make_pair(x, y - 1));
vis[x][y - 1] = vis[x][y] + 1;
}
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int SIZET = 1e5 + 10;
int water = -1;
char input[2][SIZET];
bool used[2][SIZET];
int n, k;
bool dfs(int i, int j) {
if (j > n) {
return 1;
}
if (input[i][j] == 'X' || water >= j || used[i][j]) {
return 0;
}
used[i][j] = 1;
water++;
bool ret = dfs(1 - i, j + k) || dfs(i, j + 1) || dfs(i, j - 1);
water--;
return ret;
}
int main(int argc, char const *Argv[]) {
cin >> n >> k;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < n; ++j) {
cin >> input[i][j];
}
}
if (dfs(0, 0)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace ::std;
const long double PI = acos(-1);
const long long MOD = 1000000000 + 7;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long add(long long a, long long b, long long m = MOD) {
return (a % m + b % m + 2 * m) % m;
}
long long mul(long long a, long long b, long long m = MOD) {
return ((a % m + m) * (b % m + m)) % m;
}
long long pow_mod(long long a, long long b, long long m = MOD) {
long long res = 1LL;
a = a % m;
while (b) {
if (b & 1) res = mul(res, a, m);
b >>= 1;
a = mul(a, a, m);
}
return res;
}
long long fastexp(long long a, long long b) {
long long res = 1LL;
while (b) {
if (b & 1) res = res * a;
b >>= 1;
a *= a;
}
return res;
}
int gcdExtendido(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtendido(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int modInverso(int a, int m) {
int x, y;
int g = gcdExtendido(a, m, &x, &y);
if (g != 1)
return -1;
else
return (x % m + m) % m;
}
const int N = 100000 + 5;
int n, k;
int D[3][N];
char table[3][N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool validPos(int i, int j) { return i >= 0 and i < 2 and j >= 0 and j < n; }
bool BFS(int sx, int sy) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j <= n; j++) {
D[i][j] = INT_MAX;
}
}
D[sx][sy] = 0;
queue<int> Q;
Q.push(sx);
Q.push(sy);
while (!Q.empty()) {
int ux = Q.front();
Q.pop();
int uy = Q.front();
Q.pop();
int T = D[ux][uy];
for (int l = 0; l < 4; l++) {
int vx = ux + dx[l];
int vy = uy + dy[l];
if (validPos(vx, vy) and table[vx][vy] != 'X' and vy >= T + 1) {
if (D[vx][vy] > D[ux][uy] + 1) {
D[vx][vy] = D[ux][uy] + 1;
Q.push(vx);
Q.push(vy);
}
} else if (vy >= n) {
return true;
}
}
}
return false;
}
int main() {
scanf("%d %d", &(n), &(k));
dy[2] = dy[3] = k;
for (int i = 0; i < 2; i++) {
scanf("%s", table[i]);
}
puts(BFS(0, 0) ? "YES" : "NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct pos {
int wall, area;
};
string s[2];
int n, k;
bool safe(const pos& P, int t) {
return t <= P.area && s[P.wall][P.area] == '-';
}
const int MAXN = 100 * 1000 + 9;
int min_time[2][MAXN];
int& get(const pos& P) { return min_time[P.wall][P.area]; }
const int INF = 1000 * 1000 * 10;
bool possible(void) {
for (int j = 0; j < 2; j++) {
for (int i = 0; i < n; i++) min_time[j][i] = INF;
}
pos seed;
seed.wall = 0;
seed.area = 0;
get(seed) = 0;
queue<pos> all;
all.push(seed);
while (!all.empty()) {
seed = all.front();
all.pop();
int new_t = get(seed) + 1;
pos nova;
nova = seed;
nova.area--;
if (nova.area >= n) return true;
if (safe(nova, new_t) && get(nova) > new_t) {
get(nova) = new_t;
all.push(nova);
}
nova = seed;
nova.area++;
if (nova.area >= n) return true;
if (safe(nova, new_t) && get(nova) > new_t) {
get(nova) = new_t;
all.push(nova);
}
nova = seed;
nova.wall = 1 - nova.wall;
nova.area += k;
if (nova.area >= n) return true;
if (safe(nova, new_t) && get(nova) > new_t) {
get(nova) = new_t;
all.push(nova);
}
}
return false;
}
int main() {
cin >> n >> k;
cin >> s[0];
cin >> s[1];
if (possible())
cout << "YES";
else
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
int k, n;
int a[5][N];
vector<pair<int, int> > vec[5][N];
queue<pair<int, int> > q;
int dis[5][N];
bool mark[5][N];
int main() {
cin >> n >> k;
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= n; j++) {
char c;
cin >> c;
if (c == '-') a[i][j] = true;
}
}
for (int i = 1; i <= 2; i++) {
for (int j = n + 1; j < N; j++) {
a[i][j] = true;
}
}
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= n; j++) {
if (a[i][j + 1] == 1) vec[i][j].push_back(make_pair(i, j + 1));
if (a[i][j - 1] == 1) vec[i][j].push_back(make_pair(i, j - 1));
if (a[i + 1][j + k] == 1) vec[i][j].push_back(make_pair(i + 1, j + k));
if (a[i - 1][j + k] == 1) vec[i][j].push_back(make_pair(i - 1, j + k));
}
q.push(make_pair(1, 1));
mark[1][1] = true;
while (q.size() > 0) {
int x = q.front().first, y = q.front().second;
if (y >= n) {
cout << "YES";
exit(0);
}
if (dis[x][y] < y) {
for (int i = 0; i < vec[x][y].size(); i++) {
int xx = vec[x][y][i].first, yy = vec[x][y][i].second;
if (mark[xx][yy] == false) {
mark[xx][yy] = true;
q.push(make_pair(xx, yy));
dis[xx][yy] = dis[x][y] + 1;
}
}
}
q.pop();
}
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
string w[2];
int n, k;
bool vis[2][100050];
bool dfs(int i, int j, int t) {
if (j <= t || j < 0) {
return false;
}
if (j >= n) {
return true;
}
if (w[i][j] != '-') {
return false;
}
if (vis[i][j]) {
return false;
}
vis[i][j] = true;
return max(max(dfs(i, j + 1, t + 1), dfs(i, j - 1, t + 1)),
dfs(1 - i, j + k, t + 1));
}
int main() {
cin >> n >> k >> w[0] >> w[1];
if (dfs(0, 0, -1)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > q;
int a[100005], b[100005], chk1[100005], chk2[100005];
int main() {
int n, k;
char c;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
do {
scanf("%c", &c);
} while (c == ' ' || c == '\n');
if (c == '-')
a[i] = 0;
else
a[i] = 1;
}
for (int i = 1; i <= n; i++) {
do {
scanf("%c", &c);
} while (c == ' ' || c == '\n');
if (c == '-')
b[i] = 0;
else
b[i] = 1;
}
int now, flood, chk = 0;
pair<int, int> dmp, pdmp;
chk1[1] = 1;
chk2[1] = 1;
dmp.first = 1;
dmp.second = 0;
q.push(dmp);
dmp.first = -1;
dmp.second = 0;
while (!q.empty()) {
dmp = q.front();
q.pop();
chk = 1;
if (dmp.first < 0) {
chk = 2;
dmp.first = -dmp.first;
}
now = dmp.first;
flood = dmp.second;
dmp.second = flood + 1;
if (now + k > n) {
printf("YES");
return 0;
}
if (chk == 1) {
if (a[now + 1] == 0 && !chk1[now + 1]) {
chk1[now + 1] = 1;
dmp.first = now + 1;
q.push(dmp);
}
if (now != 0) {
if (a[now - 1] == 0 && flood + 1 < now && !chk1[now - 1]) {
chk1[now - 1] = 1;
dmp.first = now - 1;
q.push(dmp);
}
}
if (b[now + k] == 0 && !chk2[now + k]) {
chk2[now + k] = 1;
dmp.first = -(now + k);
q.push(dmp);
}
} else {
if (b[now + 1] == 0 && !chk2[now + 1]) {
chk2[now + 1] = 1;
dmp.first = -(now + 1);
q.push(dmp);
}
if (now != 0) {
if (b[now - 1] == 0 && flood + 1 < now - 1 && !chk2[now - 1]) {
chk2[now - 1] = 1;
dmp.first = -(now - 1);
q.push(dmp);
}
}
if (a[now + k] == 0 && !chk1[now + k]) {
chk1[now + k] = 1;
dmp.first = (now + k);
q.push(dmp);
}
}
}
printf("NO");
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n;
const int N = 1e5 + 5;
char a[N][2];
long long vis[N][2];
bool valid(long long x, long long y) {
return (x >= 1 && x <= n && y >= 0 && y <= 1 && a[x][y] == '-' &&
vis[x][y] == 0);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
int k;
cin >> n >> k;
string s, t;
cin >> s >> t;
for (int i = 1; i <= n; i++) {
a[i][0] = s[i - 1];
a[i][1] = t[i - 1];
}
if (n == 1) {
cout << "YES";
return 0;
}
priority_queue<pair<long long, pair<long long, long long>>,
vector<pair<long long, pair<long long, long long>>>,
greater<pair<long long, pair<long long, long long>>>>
pq;
pq.push({1, {1, 0}});
int f = 0;
while (!pq.empty()) {
long long t = pq.top().first;
long long x = pq.top().second.first;
long long y = pq.top().second.second;
pq.pop();
if (vis[x][y]) continue;
if (t > x) {
vis[x][y] = 1;
continue;
}
vis[x][y] = 1;
if (x >= n) {
f = 1;
break;
}
if (t >= n) {
f = -1;
break;
}
if (valid(x - 1, y)) {
pq.push({t + 1, {x - 1, y}});
}
if (valid(x + 1, y)) {
pq.push({t + 1, {x + 1, y}});
}
if (x + k > n || x + 1 > n) {
cout << "YES";
return 0;
}
if (valid(x + k, !y)) {
pq.push({t + 1, {x + k, !y}});
}
}
if (f)
cout << "YES";
else
cout << "NO";
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k;
char str[2][100001];
int mark[2][100001];
int go[3][2] = {{0, 1}, {0, -1}, {1, 0}};
struct st {
int x, y, time;
};
queue<st> q;
int bfs() {
st cur;
cur.x = 0;
cur.y = 0;
cur.time = 0;
q.push(cur);
while (!q.empty()) {
cur = q.front();
q.pop();
st nt;
for (int i = 0; i < 3; i++) {
nt.x = (cur.x + go[i][0]) & 1;
nt.y = cur.y + go[i][1];
nt.time = cur.time + 1;
if (nt.y < nt.time) continue;
if (nt.y >= n) return puts("YES");
if (!mark[nt.x][nt.y]) {
mark[nt.x][nt.y] = 1;
if (str[nt.x][nt.y] == '-') q.push(nt);
}
}
}
return puts("NO");
}
int main() {
scanf("%d%d", &n, &k);
go[2][1] = k;
scanf("%s%s", &str[0], &str[1]);
if (k >= n)
puts("YES");
else
bfs();
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void umax(T &a, T b) {
if (a < b) a = b;
}
const int maxn = 300000 + 10;
const int INF = 1e9 + 7;
bool dp[maxn][2], visit[maxn][3];
string str[2];
int n, k;
int f = 0;
bool dfs(int pos, int idx, int t) {
if (pos <= 0) return false;
if (pos > n) {
f = 1;
cout << "YES";
exit(!printf("\n"));
return true;
}
if (t >= pos) return false;
if (str[idx][pos - 1] == 'X') return false;
if (dp[pos][idx]) return dp[pos][idx];
bool t1 = dfs(pos + k, !idx, t + 1);
bool t2 = dfs(pos + 1, idx, t + 1);
bool t3 = dfs(pos - 1, idx, t + 1);
if (t1 == false && t2 == false && t3 == false) {
dp[pos][idx] = false;
return false;
}
dp[pos][idx] = true;
return true;
}
inline void solve(void) {
cin >> n >> k;
cin >> str[0] >> str[1];
queue<pair<pair<int, int>, int> > v;
v.push(pair<pair<int, int>, int>(pair<int, int>(1, 0), 0));
while (!v.empty()) {
pair<pair<int, int>, int> f = v.front();
v.pop();
int pos = f.first.first, idx = f.first.second, t = f.second;
if (pos <= 0) continue;
if (pos > n) {
cout << "YES\n";
return;
}
if (t >= pos || str[idx][pos - 1] == 'X') continue;
if (dp[pos][idx]) continue;
if (!visit[pos + k][!idx]) {
v.push(pair<pair<int, int>, int>(pair<int, int>(pos + k, !idx), t + 1));
visit[pos + k][!idx] = true;
}
if (!visit[pos + 1][idx]) {
v.push(pair<pair<int, int>, int>(pair<int, int>(pos + 1, idx), t + 1));
visit[pos + 1][idx] = true;
}
if (!visit[pos - 1][idx]) {
v.push(pair<pair<int, int>, int>(pair<int, int>(pos - 1, idx), t + 1));
visit[pos - 1][idx] = true;
}
}
cout << "NO\n";
}
void init() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main(int argc, const char *argv[]) {
solve();
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
template <class T>
T sqr(T x) {
return x * x;
}
template <class T>
T lcm(T a, T b) {
return a / __gcd(a, b) * b;
}
template <class T>
T minimize(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
T maximize(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const long long mod = 1e9 + 7, oo = 1e12, N = 2e5 + 5;
using namespace std;
long long n, k;
char str[2][N];
bool color[2][N], flag = false;
void dfs(long long x, long long y, long long water) {
if (y < water || str[x][y] == 'X' || color[x][y] == true || flag == true)
return;
if (y >= n) {
flag = true;
return;
}
color[x][y] = true;
dfs((x + 1) % 2, y + k, water + 1);
dfs(x, y + 1, water + 1);
dfs(x, y - 1, water + 1);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie();
cout.tie();
cin >> n >> k >> str[0] >> str[1];
dfs(0, 0, 0);
flag == true ? cout << "YES\n" : cout << "NO\n";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, dp[2][100010];
char ch[2][100010];
struct node {
int pos, kd, t;
node(int p = 0, int k = 0, int tt = 0) : pos(p), kd(k), t(tt) {}
};
int main() {
while (cin >> n >> k) {
scanf("%s%s", ch[0], ch[1]);
memset(dp, 0x3f3f3f3f, sizeof dp);
dp[0][0] = 0;
bool ans = false;
queue<node> q;
q.push(node(0, 0, 0));
int kd, h, t;
while (!q.empty()) {
node now = q.front();
q.pop();
h = now.pos;
kd = now.kd;
t = now.t;
if (h >= n) {
ans = true;
break;
}
if (ch[kd][h + 1] == '-' && dp[kd][h + 1] > t + 1) {
dp[kd][h + 1] = t + 1;
q.push(node(h + 1, kd, t + 1));
}
if (h > 0 && ch[kd][h - 1] == '-' && dp[kd][h - 1] > t + 1 &&
h - 1 >= t + 1) {
dp[kd][h - 1] = t + 1;
q.push(node(h - 1, kd, t + 1));
}
if (k + h >= n) {
ans = true;
break;
}
if (ch[kd ^ 1][h + k] == '-' && dp[kd ^ 1][h + k] > t + 1) {
dp[kd ^ 1][h + k] = t + 1;
q.push(node(h + k, kd ^ 1, t + 1));
}
}
if (ans)
puts("YES");
else
puts("NO");
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int vis[100005][2];
string s1, s2;
long long int n, k;
long long int ans = 0;
void dfs(long long int i, long long int f, long long int w) {
if (ans == 1) return;
if (i > n) {
ans = 1;
return;
}
if (i < w) return;
if (f == 1) {
if (s2[i] == 'X') return;
} else {
if (s1[i] == 'X') return;
}
if (vis[i][f]) return;
vis[i][f] = 1;
dfs(i - 1, f, w + 1);
dfs(i + k, 1 - f, w + 1);
dfs(i + 1, f, w + 1);
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
cin >> s1 >> s2;
dfs(0, 0, 0);
if (ans)
cout << "YES";
else
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265359;
template <typename T>
T abs(T x) {
return x > 0 ? x : -x;
}
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
void chmin(T &x, T y) {
x = min(x, y);
}
template <typename T>
void chmax(T &x, T y) {
x = max(x, y);
}
template <typename U, typename V>
ostream &operator<<(ostream &s, const pair<U, V> &x) {
s << "(" << x.first << ", " << x.second << ")";
return s;
}
template <typename U>
ostream &operator<<(ostream &s, const vector<U> &x) {
s << "[";
bool was = false;
for (auto it : x) {
if (was) {
s << ", ";
}
was = true;
s << it;
}
s << "]";
return s;
}
const int maxn = 1e5 + 5;
const int inf = 1e9;
char a[2][maxn];
int d[2][maxn];
int main() {
srand(time(NULL));
retry:
int n, k;
scanf("%d %d\n", &n, &k);
scanf("%s\n", a[0] + 1);
scanf("%s\n", a[1] + 1);
queue<pair<int, int>> q;
q.push(make_pair(0, 1));
fill(d[0], d[0] + maxn, inf);
fill(d[1], d[1] + maxn, inf);
d[0][1] = 0;
auto yes = []() {
cout << "YES" << endl;
exit(0);
};
while (!q.empty()) {
int s = q.front().first;
int v = q.front().second;
q.pop();
if (v + k > n) {
yes();
}
if (a[s][v + 1] == '-' && d[s][v + 1] == inf) {
d[s][v + 1] = d[s][v] + 1;
q.push(make_pair(s, v + 1));
}
if (v - 1 > 0 && a[s][v - 1] == '-' && v - 1 > d[s][v] + 1 &&
d[s][v - 1] == inf) {
d[s][v - 1] = d[s][v] + 1;
q.push(make_pair(s, v - 1));
}
if (a[1 - s][v + k] == '-' && d[1 - s][v + k] == inf) {
d[1 - s][v + k] = d[s][v] + 1;
q.push(make_pair(1 - s, v + k));
}
}
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:128777216")
using namespace std;
bool d[2][200000];
char a[100010];
char b[100010];
struct Pos {
int id;
int pos;
int t;
void init(int id, int pos, int t) {
this->id = id;
this->pos = pos;
this->t = t;
d[id][pos] = 1;
}
} st[300000];
int main() {
int n, k;
scanf("%d %d", &n, &k);
scanf("%s", a);
scanf("%s", b);
for (int i = 0; i < n; i++) {
if (a[i] == 'X') {
d[0][i] = 1;
}
if (b[i] == 'X') {
d[1][i] = 1;
}
}
st[0].init(0, 0, 0);
for (int d = 0, u = 1; d < u; d++) {
int id = st[d].id, pos = st[d].pos, t = st[d].t + 1;
if (pos < st[d].t) continue;
if (pos + k >= n) {
printf("YES");
return 0;
}
if (pos > 0 && !::d[id][pos - 1]) {
st[u++].init(id, pos - 1, t);
}
if (!::d[id][pos + 1]) {
st[u++].init(id, pos + 1, t);
}
if (!::d[id ^ 1][pos + k]) {
st[u++].init(id ^ 1, pos + k, t);
}
}
printf("NO");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
map<long long, list<long long>> m;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
long long n, k;
cin >> n >> k;
string a[2];
cin >> a[0] >> a[1];
for (long long i = 0; i < k; i++) {
a[0] += '-';
a[1] += '-';
}
list<pair<pair<long long, long long>, long long>> q;
long long w = -1, c = 0;
q.push_back({{0, 0}, w});
long long v[2][n + k];
memset(v, 0, sizeof(v));
while (!q.empty()) {
pair<pair<long long, long long>, long long> te = q.front();
q.pop_front();
if (te.first.second >= n) {
cout << "YES" << endl;
return 0;
}
if (!v[1 - te.first.first][te.first.second + k] &&
te.first.second + k > te.second + 1 &&
a[1 - te.first.first][te.first.second + k] == '-') {
q.push_back({{1 - te.first.first, te.first.second + k}, te.second + 1});
v[1 - te.first.first][te.first.second + k] = 1;
}
if (!v[te.first.first][te.first.second + 1] &&
te.first.second + 1 > te.second + 1 &&
a[te.first.first][te.first.second + 1] == '-') {
q.push_back({{te.first.first, te.first.second + 1}, te.second + 1});
v[te.first.first][te.first.second + 1] = 1;
}
if (!v[te.first.first][te.first.second - 1] &&
te.first.second - 1 > te.second + 1 && te.first.second > 0 &&
a[te.first.first][te.first.second - 1] == '-') {
q.push_back({{te.first.first, te.first.second - 1}, te.second + 1});
v[te.first.first][te.first.second + 1] = 1;
}
}
cout << "NO" << endl;
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y, s;
};
char a[5][500005];
int book[5][500005];
int main() {
int n, k;
cin >> n >> k;
queue<node> q;
node now, next;
for (int i = 1; i <= 2; i++) scanf("%s", a[i] + 1);
int p = 0;
now.x = 1;
now.y = 1;
now.s = 0;
q.push(now);
book[1][1] = 1;
int flag = 0;
while (!q.empty()) {
now = q.front();
int tx, ty;
for (int i = 0; i < 3; i++) {
if (i == 0) {
tx = now.x;
ty = now.y + 1;
}
if (i == 1) {
tx = now.x;
ty = now.y - 1;
}
if (i == 2) {
if (now.x == 1)
tx = 2;
else
tx = 1;
ty = now.y + k;
}
if (ty > n) {
flag = 1;
break;
}
if (book[tx][ty] == 1 || a[tx][ty] == 'X' || ty <= now.s + 1 || tx < 1)
continue;
if (book[tx][ty] == 0) {
book[tx][ty] = 1;
next.x = tx;
next.y = ty;
next.s = now.s + 1;
q.push(next);
}
}
p++;
q.pop();
if (flag) break;
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
double PI = acos(-1);
int n, k;
char grid[2][100005];
bool vis[2][100005];
bool sol = 0;
bool valid(int x, int y) {
if (y < 0 || y > n - 1) return 0;
return 1;
}
void solve(int x, int y, int w) {
vis[x][y] = 1;
if (w >= y) return;
if (y + k >= n) sol = 1;
if (valid(1 - x, y + k)) {
if (!vis[1 - x][y + k] && grid[1 - x][y + k] == '-')
solve(1 - x, y + k, w + 1);
}
if (valid(x, y + 1)) {
if (!vis[x][y + 1] && grid[x][y + 1] == '-') solve(x, y + 1, w + 1);
}
if (valid(x, y - 1)) {
if (!vis[x][y - 1] && grid[x][y - 1] == '-') solve(x, y - 1, w + 1);
}
}
int main() {
cin >> n >> k;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < n; j++) cin >> grid[i][j];
}
solve(0, 0, -1);
if (sol)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k;
char s[2][210000];
int d[2][210000];
bool bfs() {
memset(d, -1, sizeof(d));
int dx[3] = {0, 0, 1}, dy[3] = {1, -1, k};
queue<int> q;
d[0][1] = 1;
q.push(0);
q.push(1);
while (!q.empty()) {
int x = q.front();
q.pop();
int y = q.front();
q.pop();
if (y > n) return true;
for (int i = 0; i < 3; i++) {
int nex = x ^ dx[i], ney = y + dy[i];
if (s[nex][ney] == 'X') continue;
if (d[nex][ney] != -1) continue;
if (d[x][y] + 1 > ney) continue;
d[nex][ney] = d[x][y] + 1;
q.push(nex);
q.push(ney);
}
}
return false;
}
int main() {
scanf("%d%d", &n, &k);
memset(d, 0, sizeof(d));
scanf("%s%s", &s[0][1], &s[1][1]);
if (bfs())
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, g;
char s[2][100005];
int t[2][100005];
struct Node {
int k, w;
Node(int x, int y) { k = x, w = y; }
};
queue<Node> que;
int main() {
scanf("%d%d%s%s", &n, &g, s[0], s[1]);
que.push(Node(0, 0));
memset(t, -1, sizeof(t));
t[0][0] = 0;
bool ans = false;
while (!que.empty()) {
int k = que.front().k, w = que.front().w;
que.pop();
if (w + g >= n) {
ans = true;
break;
}
if (t[k][w + 1] == -1) {
t[k][w + 1] = t[k][w] + 1;
if (w + 1 >= t[k][w + 1] && s[k][w + 1] == '-') que.push(Node(k, w + 1));
}
if (t[k][w - 1] == -1) {
t[k][w - 1] = t[k][w] + 1;
if (w - 1 >= t[k][w - 1] && s[k][w - 1] == '-') que.push(Node(k, w - 1));
}
if (t[k ^ 1][w + g] == -1) {
t[k ^ 1][w + g] = t[k][w] + 1;
if (w + g >= t[k ^ 1][w + g] && s[k ^ 1][w + g] == '-')
que.push(Node(k ^ 1, w + g));
}
}
printf(ans ? "YES\n" : "NO\n");
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, d[2][1000000];
bool ans, vis[2][1000000];
void dfs(int j, int i, int water) {
if (water >= i) {
return;
}
if (i >= n) {
ans = 1;
return;
}
vis[j][i] = 1;
if (d[j][i - 1] != 0 && vis[j][i - 1] == 0) dfs(j, i - 1, water + 1);
if (i + m >= n || (d[1 - j][i + m] != 0 && vis[1 - j][i + m] == 0))
dfs(1 - j, i + m, water + 1);
if (i + 1 >= n || (d[j][i + 1] != 0 && vis[j][i + 1] == 0))
dfs(j, i + 1, water + 1);
return;
}
int main() {
char z;
cin >> n >> m;
for (int i = 0; i < 2; i++)
for (int j = 0; j < n; j++) {
cin >> z;
if (z == '-')
d[i][j] = 1;
else
d[i][j] = 0;
}
dfs(0, 0, -1);
if (ans)
cout << "YES";
else
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct Counter {
static int k;
Counter() { k++; }
~Counter() { k--; }
};
int Counter::k = 0;
template <typename T>
void pr(const string& name, T t) {
cout << name << " = " << t << endl;
}
template <typename T, typename... Types>
void pr(const string& names, T t, Types... rest) {
auto comma_pos = names.find(',');
cout << names.substr(0, comma_pos) << " = " << t << ", ";
auto next_name_pos = names.find_first_not_of(" \t\n", comma_pos + 1);
pr(string(names, next_name_pos), rest...);
}
void mod(long long& a, long long b) {
a %= b;
if (a < 0) a += b;
}
const long long MOD = 1000000007;
const int INF = 1000005;
int D[1000005], N, K;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
Q;
string W[2];
int main() {
cin >> N >> K;
cin >> W[0] >> W[1];
for (int i = (0); i <= (2 * N); i++) D[i] = INF;
D[0] = 0;
Q.push({0, 0});
while (!Q.empty()) {
pair<int, int> p = Q.top();
Q.pop();
int w = p.second / N;
int h = p.second % N;
int d = p.first;
if (h > 0 && W[w][h - 1] == '-' && d + 1 < D[N * w + h - 1] &&
h - 1 >= d + 1) {
D[N * w + h - 1] = d + 1;
Q.push({d + 1, N * w + h - 1});
}
if (h < N - 1 && W[w][h + 1] == '-' && d + 1 < D[N * w + h + 1]) {
D[N * w + h + 1] = d + 1;
Q.push({d + 1, N * w + h + 1});
}
if (h <= N - 1 - K && W[1 - w][h + K] == '-' &&
d + 1 < D[N * (1 - w) + h + K]) {
D[N * (1 - w) + h + K] = d + 1;
Q.push({d + 1, N * (1 - w) + h + K});
}
}
for (int i = (N - K); i <= (N - 1); i++)
if (D[i] < INF || D[N + i] < INF) {
cout << "YES";
return 0;
}
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int readint() {
char c;
while (c = getchar(), (c < '0' || c > '9') && c != '-')
;
bool flag = (c == '-');
if (flag) c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - 48;
c = getchar();
}
return flag ? -x : x;
}
inline string tos(int x) {
string s;
while (x) s = (char)(x % 10 + '0') + s, x /= 10;
return s;
}
queue<int> qx, qy, qz;
char c[2][100005];
bool use[2][100005];
int main() {
ios_base::sync_with_stdio(false);
int n, k, i, j, x, y, z;
cin >> n >> k;
for (i = 1; i <= n; i++) cin >> c[0][i];
for (i = 1; i <= n; i++) cin >> c[1][i];
qz.push(0);
qx.push(0);
qy.push(1);
while (!qx.empty()) {
z = qz.front();
x = qx.front();
y = qy.front();
qz.pop();
qx.pop();
qy.pop();
if (y > n) {
cout << "YES";
return 0;
}
if (use[x][y]) continue;
if (c[x][y] == 'X') continue;
if (z >= y) continue;
use[x][y] = 1;
qz.push(z + 1);
qy.push(y + k);
qx.push(1 - x);
qz.push(z + 1);
qy.push(y + 1);
qx.push(x);
qz.push(z + 1);
qx.push(x);
qy.push(y - 1);
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n, k;
vector<int> adj[MAXN];
char s[2][MAXN];
int dist[2][MAXN];
bool solve() {
memset(dist, 0x3f3f3f3f, sizeof(dist));
dist[0][1] = 0;
queue<pair<int, pair<int, int> > > pq;
pq.push({0, {0, 1}});
while (!pq.empty()) {
int d = pq.front().first;
int side = pq.front().second.first;
int h = pq.front().second.second;
pq.pop();
if (dist[side][h] == d) {
for (auto op : vector<pair<int, int> >(
{{h - 1, side}, {h + 1, side}, {h + k, 1 - side}})) {
int nh = op.first;
int opside = op.second;
if (nh < 1) continue;
if (nh > n) return true;
if (s[opside][nh] != 'X' && dist[opside][nh] > d + 1 && nh > d + 1) {
dist[opside][nh] = d + 1;
pq.push({dist[opside][nh], {opside, nh}});
}
}
}
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = 0; i < (2); ++i)
for (int j = 1; j < (n + 1); ++j) cin >> s[i][j];
cout << (solve() ? "YES" : "NO") << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, xx;
char a[2][201005];
int vis[2][201005];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct node {
int x, y, cnt;
};
bool judge(int x, int y) {
if (x >= 0 && x < 2 && y >= 0) return true;
return false;
}
void bfs(node f) {
queue<node> q;
q.push(f);
memset(vis, 0, sizeof(vis));
vis[f.x][f.y] = 1;
while (!q.empty()) {
node t = q.front();
q.pop();
if (t.y >= n - 1) {
cout << "YES" << endl;
return;
}
for (int i = 0; i < 4; i++) {
node p;
p.cnt = t.cnt + 1;
if (i == 0 || i == 1) {
p.x = t.x + dir[i][0];
p.y = t.y + dir[i][1] + k;
} else {
p.x = t.x + dir[i][0];
p.y = t.y + dir[i][1];
}
if (p.cnt >= p.y) {
continue;
}
if (judge(p.x, p.y) && a[p.x][p.y] != 'X' && !vis[p.x][p.y]) {
vis[p.x][p.y] = 1;
q.push(p);
}
}
}
cout << "NO" << endl;
}
int main() {
while (~scanf("%d %d", &n, &k)) {
memset(a, 0, sizeof(a));
for (int i = 0; i < 2; i++)
for (int j = 0; j < n; j++) cin >> a[i][j];
node f;
f.x = 0;
f.y = 0;
f.cnt = -1;
bfs(f);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct hz {
int t;
int p;
bool ix;
hz(int t, int p, int ix) : t(t), p(p), ix(ix) {}
};
int was[2][100011];
int n, k;
string w[2];
#pragma comment(linker, "/STACK:1234567890")
int main() {
cin >> n >> k >> w[0] >> w[1];
queue<hz> q;
q.push(hz(0, 0, 0));
memset(was, false, sizeof(was));
while (!q.empty()) {
hz s = q.front();
q.pop();
if (was[s.ix][s.p]) continue;
was[s.ix][s.p] = true;
if (s.p + 1 >= n || s.p + k >= n) {
cout << "YES";
return 0;
}
if (!was[s.ix ^ 1][s.p + k] && w[s.ix ^ 1][s.p + k] != 'X') {
q.push(hz(s.t + 1, s.p + k, s.ix ^ 1));
}
if (!was[s.ix][s.p + 1] && w[s.ix][s.p + 1] != 'X') {
q.push(hz(s.t + 1, s.p + 1, s.ix));
}
if (s.p - 1 > s.t && !was[s.ix ^ 1][s.p + k] && w[s.ix][s.p - 1] != 'X') {
q.push(hz(s.t + 1, s.p - 1, s.ix));
}
}
cout << "NO";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, distence[2][100010], ans;
char s[2][100010];
queue<pair<int, int> > q;
void bfs(int x, int y, int dis) {
if (y > n) {
cout << "YES" << endl;
exit(0);
};
if (s[x][y] != 'X' && !distence[x][y] && y >= 0) {
distence[x][y] = dis;
if (dis <= y) q.push(pair<int, int>(x, y));
}
}
int main() {
cin >> n >> k;
cin >> s[0];
cin >> s[1];
bfs(0, 0, 0);
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
int dis = distence[x][y] + 1;
if (x == 0)
bfs(x + 1, y + k, dis);
else
bfs(x - 1, y + k, dis);
bfs(x, y + 1, dis);
bfs(x, y - 1, dis);
}
cout << "NO" << endl;
return 0;
}
| 8 | CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.