solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
n, k = map(int, input().split(' ')) l = input() r = input() data = [0, ' '+l, ' '+r] dist = [[1000000]*(10**5+5) for _ in range(3)] visited = [[False]*100005 for _ in range(3)] visited[1][1] = True dist[1][1] = 0 qx = [1] qy = [1] while qx: x = qx.pop() y = qy.pop() if dist[x][y] >= y: continue if x == 1: poss = [[x, y-1], [x, y+1], [x+1, y+k]] if x == 2: poss = [[x, y-1], [x, y+1], [x-1, y+k]] for i in poss: newx = i[0] newy = i[1] if newy > n: print("YES"); quit(); if 1<=newy<=n and not visited[newx][newy] and data[newx][newy] != 'X': visited[newx][newy] = True qx = [newx] + qx qy = [newy] + qy dist[newx][newy] = dist[x][y] + 1 print("NO")
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5; const int inf = 1e9; struct pnt { int x, y; pnt() {} pnt(int _x, int _y) { x = _x; y = _y; } }; pnt que[maxn]; int dist[maxn][2]; string s[2]; int main() { int i, n, k, uk1, uk2; pnt p, p1; cin >> n >> k; cin >> s[0]; cin >> s[1]; for (i = 0; i < n; i++) dist[i][0] = dist[i][1] = inf; dist[0][0] = 0; que[0] = pnt(0, 0); uk1 = 0; uk2 = 1; for (; uk1 < uk2; uk1++) { p = que[uk1]; if (p.x == n - 1) { cout << "YES"; return 0; } if (s[p.y][p.x + 1] == '-' && dist[p.x + 1][p.y] > dist[p.x][p.y] + 1) { que[uk2++] = pnt(p.x + 1, p.y); dist[p.x + 1][p.y] = dist[p.x][p.y] + 1; } if (p.x - 1 >= 0 && s[p.y][p.x - 1] == '-' && dist[p.x - 1][p.y] > dist[p.x][p.y] + 1 && dist[p.x][p.y] + 1 <= p.x - 1) { que[uk2++] = pnt(p.x - 1, p.y); dist[p.x - 1][p.y] = dist[p.x][p.y] + 1; } p1 = pnt(p.x + k, (p.y + 1) % 2); if (p1.x >= n) { cout << "YES"; return 0; } if (s[p1.y][p1.x] == '-' && dist[p1.x][p1.y] > dist[p.x][p.y] + 1) { que[uk2++] = pnt(p1.x, p1.y); dist[p1.x][p1.y] = dist[p.x][p.y] + 1; } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, k, v[2][2 * N]; char g[2][N]; string s, ans = "NO"; void read(int i) { cin >> s; for (int x = 0; x < n; x++) { g[i][x] = s[x]; } if (!i) { read(!i); } } void bfs() { queue<pair<pair<int, int>, int>> q; q.push(make_pair(make_pair(0, 0), -1)); v[0][0] = 1; while (q.size()) { pair<pair<int, int>, int> p = q.front(); q.pop(); int a = p.first.first, b = p.first.second, c = p.second; if (b >= n) { ans = "YES"; break; } if (g[a][b] == 'X' || c >= b) { continue; } if (!v[a][b + 1]) { v[a][b + 1] = 1; q.push(make_pair(make_pair(a, b + 1), c + 1)); } if (!v[!a][b + k]) { v[!a][b + k] = 1; q.push(make_pair(make_pair(!a, b + k), c + 1)); } if (b) { if (!v[a][b - 1]) { v[a][b - 1] = 1; q.push(make_pair(make_pair(a, b - 1), c + 1)); } } } } void setup(); int main() { setup(); cin >> n >> k; read(0); bfs(); cout << ans; return 0; } void setup() { ios_base::sync_with_stdio(false); }
8
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 1000000; int n, k; string maze[2]; int vis[2][100010]; queue<pair<int, int> > q; int main(void) { cin >> n >> k; for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { vis[i][j] = -1; } } cin >> maze[0] >> maze[1]; int dx[4] = {0, 0, 1, -1}; int dy[4] = {-1, +1, k, k}; vis[0][0] = 0; q.push(make_pair(0, 0)); while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); if (p.second < vis[p.first][p.second]) continue; if (p.second + k >= n) { printf("YES\n"); return 0; } for (int i = 0; i < 4; i++) { int xi = dx[i] + p.first; int yi = dy[i] + p.second; if (xi >= 0 && xi <= 1 && yi >= 0 && yi < n && maze[xi][yi] == '-' && vis[xi][yi] == -1) { vis[xi][yi] = vis[p.first][p.second] + 1; q.push(make_pair(xi, yi)); } } } printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; queue<int> qx, qy; int d[2][110000]; char s[2][110000]; int n, k; int main() { gets(s[0]); sscanf(s[0], "%d %d", &n, &k); gets(s[0]); gets(s[1]); memset(d, 63, sizeof(d)); d[0][0] = 0; qx.push(0); qy.push(0); for (; qx.size();) { int x = qx.front(); int y = qy.front(); qx.pop(); qy.pop(); if (d[y][x] > x) continue; if (x + k >= n) { printf("YES\n"); return 0; } if (s[y][x + 1] == '-') { if (d[y][x + 1] > d[y][x] + 1) { d[y][x + 1] = d[y][x] + 1; qx.push(x + 1); qy.push(y); } } if (x > 0 && s[y][x - 1] == '-') { if (d[y][x - 1] > d[y][x] + 1) { d[y][x - 1] = d[y][x] + 1; qx.push(x - 1); qy.push(y); } } if (s[y ^ 1][x + k] == '-') { if (d[y ^ 1][x + k] > d[y][x] + 1) { d[y ^ 1][x + k] = d[y][x] + 1; qx.push(x + k); qy.push(y ^ 1); } } } printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10; const long long int INFLL = 1e18 + 10; const long double EPS = 1e-8; const long double EPSLD = 1e-14; const long long int MOD = 1e9 + 7; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } const int MAX_N = 100010; int n, k; char ca[MAX_N], cb[MAX_N]; string s[2]; priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, greater<pair<int, pair<int, int> > > > que; int dist[2][MAX_N]; bool ok = false; int nt, nh, nd; int main() { scanf("%d %d", &n, &k); scanf("%s %s", ca, cb); s[0] = ca; s[1] = cb; for (int i = (0); i < (int)(2); i++) for (int j = (0); j < (int)(MAX_N); j++) dist[i][j] = INF; dist[0][0] = 0; que.push(pair<int, pair<int, int> >(0, pair<int, int>(0, 0))); while (!que.empty()) { int t = que.top().second.first; int h = que.top().second.second; int d = que.top().first; que.pop(); if (d > dist[t][h]) continue; nt = t; nh = h + 1; nd = d + 1; if (nh >= nd && nh < n && s[nt][nh] == '-' && dist[nt][nh] > nd) { dist[nt][nh] = nd; que.push(pair<int, pair<int, int> >(nd, pair<int, int>(nt, nh))); } if (nh >= n) ok = true; nt = t; nh = h - 1; if (nh >= nd && nh < n && s[nt][nh] == '-' && dist[nt][nh] > nd) { dist[nt][nh] = nd; que.push(pair<int, pair<int, int> >(nd, pair<int, int>(nt, nh))); } if (nh >= n) ok = true; nt = 1 - t; nh = h + k; if (nh >= nd && nh < n && s[nt][nh] == '-' && dist[nt][nh] > nd) { dist[nt][nh] = nd; que.push(pair<int, pair<int, int> >(nd, pair<int, int>(nt, nh))); } if (nh >= n) ok = true; } if (ok) puts("YES"); else puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const double pi = 3.141592653689793238460; const long long inf = 0x3f3f3f3f; const int N = 2e5 + 5; const int pr = 31; using ld = long double; int mod = 1000000007; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int power(long long x, unsigned int y, int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } bool mark[2][100005]; int can[2][100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; int k; string s[2]; cin >> n >> k; cin >> s[0] >> s[1]; queue<pair<int, int> > q; q.push(make_pair(0, 0)); mark[0][0] = true; can[0][0] = 0; while (!q.empty()) { auto p = q.front(); q.pop(); int x = p.first; int y = p.second; if (x + k >= n) { cout << "YES" << endl; return 0; } for (int ii = -1; ii <= 1; ii++) { if (x + ii >= 0 && can[y][x] + 1 <= x + ii && !mark[y][x + ii] && s[y][x + ii] == '-') { mark[y][x + ii] = true; can[y][x + ii] = can[y][x] + 1; q.push(make_pair(x + ii, y)); } } if (s[1 - y][x + k] == '-' && !mark[1 - y][x + k]) { mark[1 - y][x + k] = true; can[1 - y][x + k] = can[y][x] + 1; q.push(make_pair(x + k, 1 - y)); } } cout << "NO" << endl; }
8
CPP
from collections import deque l, j = [int(i) for i in input().split(' ')] wallA = list(input()) wallB = list(input()) g = {} for i in range(l): # Each 4-tuple represents: (Visited?, Current Height, Current Water Height, Drowned?) if wallA[i] == '-': g[(1,i+1)] = (-1, 0, 0, False) if wallB[i] == '-': g[(-1,i+1)] = (-1, 0, 0, False) g[(1, 1)] = ('VISITED', 1, 0, False) q = deque([(1, 1)]) while q: c = q.popleft() up = (c[0], c[1]+1) down = (c[0], c[1]-1) jump = (c[0]*-1, c[1] + j) if g[c][1] <= g[c][2]: g[c] = (g[c][0], g[c][1], g[c][2], True) if up in g and g[up][0] == -1: q.append(up) g[up] = ('VISITED', g[c][1] + 1, g[c][2] + 1, g[c][3]) if down in g and g[down][0] == -1: q.append(down) g[down] = ('VISITED', g[c][1] - 1, g[c][2] + 1, g[c][3]) if jump in g and g[jump][0] == -1: q.append(jump) g[jump] = ('VISITED', g[c][1] + j, g[c][2] + 1, g[c][3]) def graphHasEscape(graph): for node in graph: result = graph[node] if result[0] == 'VISITED' and ((result[1] + 1 > l) or (result[1] + j > l)) and not result[3]: return True break return False if graphHasEscape(g): print('YES') else: print('NO')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; string L, R; int dist[2][100010]; queue<int> q; int main(void) { int N, K, i, j; cin >> N >> K; cin >> L >> R; for ((i) = 0; (i) < (int)(2); (i)++) for ((j) = 0; (j) < (int)(N); (j)++) dist[i][j] = (1 << 29); dist[0][0] = 0; q.push(0); q.push(0); while (!q.empty()) { int s = q.front(); q.pop(); int h = q.front(); q.pop(); int t = dist[s][h]; if (h + K >= N) { cout << "YES" << endl; return 0; } if ((s == 0 && L[h + 1] == '-') || (s == 1 && R[h + 1] == '-')) if (dist[s][h + 1] == (1 << 29)) { dist[s][h + 1] = t + 1; q.push(s); q.push(h + 1); } if (h - 1 >= t + 1) if ((s == 0 && L[h - 1] == '-') || (s == 1 && R[h - 1] == '-')) if (dist[s][h - 1] == (1 << 29)) { dist[s][h - 1] = t + 1; q.push(s); q.push(h - 1); } if ((s == 0 && R[h + K] == '-') || (s == 1 && L[h + K] == '-')) if (dist[1 - s][h + K] == (1 << 29)) { dist[1 - s][h + K] = t + 1; q.push(1 - s); q.push(h + K); } } cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[3][1000000]; long long d[3][1000000]; int used[3][1000000]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= 2; i++) { for (int k = 1; k <= n; k++) { cin >> a[i][k]; } } d[1][1] = 0; used[1][1] = 1; queue<pair<int, int> > q; q.push({1, 1}); int w = 0; while (!q.empty()) { pair<int, int> t = q.front(); if (t.second >= n) { cout << "YES"; return 0; } w = d[t.first][t.second]; q.pop(); if (t.second + 1 <= n && used[t.first][t.second + 1] == 0 && a[t.first][t.second + 1] != 'X') { d[t.first][t.second + 1] = d[t.first][t.second] + 1; q.push({t.first, t.second + 1}); used[t.first][t.second + 1] = 1; } if (t.second - 1 > 0 && used[t.first][t.second - 1] == 0 && t.second - 1 > w + 1 && a[t.first][t.second - 1] != 'X') { d[t.first][t.second - 1] = d[t.first][t.second] + 1; used[t.first][t.second - 1] = 1; q.push({t.first, t.second - 1}); } if (t.first == 1) { if (used[t.first + 1][t.second + m] == 0 && a[t.first + 1][t.second + m] != 'X') { if (t.second + m <= n) { d[t.first + 1][t.second + m] = d[t.first][t.second] + 1; used[t.first + 1][t.second + m] = 1; q.push({t.first + 1, t.second + m}); } else { d[t.first + 1][n] = d[t.first][t.second] + 1; used[t.first + 1][n] = 1; q.push({t.first + 1, n}); } } } else { if (used[t.first - 1][t.second + m] == 0 && a[t.first - 1][t.second + m] != 'X') { if (t.second + m <= n) { d[t.first - 1][t.second + m] = d[t.first][t.second] + 1; used[t.first - 1][t.second + m] = 1; q.push({t.first - 1, t.second + m}); } else { d[t.first - 1][n] = d[t.first][t.second] + 1; used[t.first - 1][n] = 1; q.push({t.first - 1, n}); } } } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 100, MOD = 1e9 + 7; long long n, m, k; map<pair<long long, long long>, long long> check, cnt; pair<long long, long long> ans; vector<pair<long long, long long> > g; string s[2]; string BFS(pair<long long, long long> source) { long long test = 0; queue<pair<long long, long long> > q; q.push(source); cnt[source] = -1; while (q.size()) { pair<long long, long long> v = q.front(); q.pop(); long long a = v.first; long long b = v.second; if (a + k >= n) return "YES"; if (a + 1 < n && s[b][a + 1] != 'X') g.push_back({a + 1, b}); if (a >= 1 && s[b][a - 1] != 'X') g.push_back({a - 1, b}); if (a + k < n && s[b ^ 1][a + k] != 'X') g.push_back({a + k, b ^ 1}); for (pair<long long, long long> u : g) { if (check.count(u) == 0 && u.first > cnt[v] + 1) { q.push(u); cnt[u] = cnt[v] + 1; check[u] = 1; if (u.first >= n) return "YES"; } } g.clear(); } return "NO"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; cin >> s[0] >> s[1]; cout << BFS({0, 0}); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, s, h, v[1001000][2]; char l[2][1001000]; bool valid(pair<int, int> p) { return v[h][s] < p.second && (p.second > n || !v[p.second][p.first] && l[p.first][p.second - 1] == '-'); } int main() { cin >> n >> k >> l[0] >> l[1]; queue<pair<int, int>> q; q.push({0, 1}); v[1][0] = 1; while (!q.empty()) { s = q.front().first, h = q.front().second; if (h > n) break; q.pop(); pair<int, int> d[3] = {{1 - s, h + k}, {s, h + 1}, {s, h - 1}}; for (int i = 0; i < 3; ++i) if (valid(d[i])) v[d[i].second][d[i].first] = v[h][s] + 1, q.push(d[i]); } cout << (q.empty() ? "NO" : "YES"); }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 100 + 10; bool valid[2][MAXN]; vector<pair<int, int> > G[2][MAXN]; int n, k; bool vis[2][MAXN]; vector<pair<int, int> > B; int dis[2][MAXN]; void make_G() { for (int i = 0; i < n; i++) { if (i - 1 >= 0 && valid[0][i - 1]) G[0][i].push_back(make_pair(0, i - 1)); if (i + 1 < n && valid[0][i + 1]) G[0][i].push_back(make_pair(0, i + 1)); if (i + k < n && valid[1][i + k]) G[0][i].push_back(make_pair(1, i + k)); } for (int i = 0; i < n; i++) { if (i - 1 >= 0 && valid[1][i - 1]) G[1][i].push_back(make_pair(1, i - 1)); if (i + 1 < n && valid[1][i + 1]) G[1][i].push_back(make_pair(1, i + 1)); if (i + k < n && valid[0][i + k]) G[1][i].push_back(make_pair(0, i + k)); } } bool bfs(int w, int u) { memset(vis, false, sizeof vis); B.resize(0); B.push_back(make_pair(w, u)); dis[w][u] = 0; int cnt = 0; while (cnt < (int)B.size()) { w = B[cnt].first; u = B[cnt].second; if (u + k >= n) return true; for (int i = 0; i < (int)G[w][u].size(); i++) { int w2 = G[w][u][i].first; int u2 = G[w][u][i].second; if (vis[w2][u2] == false && dis[w][u] + 1 < u2 + 1) { vis[w2][u2] = true; dis[w2][u2] = dis[w][u] + 1; B.push_back(make_pair(w2, u2)); } } cnt++; } return false; } int main() { cin >> n >> k; memset(valid, true, sizeof valid); for (int i = 0; i < n; i++) { char c; cin >> c; if (c == 'X') valid[0][i] = false; } for (int i = 0; i < n; i++) { char c; cin >> c; if (c == 'X') valid[1][i] = false; } make_G(); if (bfs(0, 0)) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, d[2][100005], q1[100005], q2[100005], x, y, st, en; char s[2][100005]; int main() { cin >> n >> k; cin >> s[0]; cin >> s[1]; d[0][0] = 1; for (en = 1; st < en;) { x = q1[st]; y = q2[st++]; if (y >= n - k) { cout << "YES"; return 0; } if (s[x][y - 1] == '-' && d[x][y - 1] == 0 && y > 0 && y > d[x][y]) { d[x][y - 1] = d[x][y] + 1; q1[en] = x; q2[en++] = y - 1; } if (s[x][y + 1] == '-' && d[x][y + 1] == 0) { d[x][y + 1] = d[x][y] + 1; q1[en] = x; q2[en++] = y + 1; } if (s[1 - x][y + k] == '-' && d[1 - x][y + k] == 0) { d[1 - x][y + k] = d[x][y] + 1; q1[en] = 1 - x; q2[en++] = y + k; } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[2][500005]; int n, k; struct node { int x, y, cnt; }; void work() { int f = 0; queue<node> q; node t; t.x = 0; t.y = 0; t.cnt = 0; q.push(t); while (!q.empty()) { node c = q.front(); q.pop(); if (c.y >= n) { f = 1; printf("YES\n"); break; } t.x = c.x; t.y = c.y + 1; t.cnt = c.cnt + 1; if (t.y >= 0 && a[t.x][t.y] != 'X') { q.push(t); a[t.x][t.y] = 'X'; } t.y = c.y - 1; if (t.y >= 0 && a[t.x][t.y] != 'X' && t.y >= t.cnt) { q.push(t); a[t.x][t.y] = 'X'; } t.x = (c.x + 1) % 2; t.y = c.y + k; if (t.y >= 0 && a[t.x][t.y] != 'X') { q.push(t); a[t.x][t.y] = 'X'; } } if (!f) { printf("NO"); } } int main() { scanf("%d%d", &n, &k); scanf("%s%s", a[0], a[1]); work(); return 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 (b[whi % 2][pla]) return; if (pla <= water) 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; string wall[2]; int n, k; int bfs(pair<int, int> start) { vector<vector<int> > visited(n, vector<int>(2, -1)); for (int i = 1; i <= n; i++) { for (int c = 0; c < 2; c++) { if (wall[c][i] == 'X') { visited[i][c] = -2; } } } queue<pair<int, int> > q; visited[start.first][start.second] = 0; q.push(start); while (!q.empty()) { int x = q.front().first; int c = q.front().second; q.pop(); if (x + k >= n) { return true; } int water = visited[x][c]; if (x - 1 > water && visited[x - 1][c] == -1) { visited[x - 1][c] = visited[x][c] + 1; q.push(make_pair(x - 1, c)); } if (visited[x + 1][c] == -1) { visited[x + 1][c] = visited[x][c] + 1; q.push(make_pair(x + 1, c)); } if (visited[x + k][!c] == -1) { visited[x + k][!c] = visited[x][c] + 1; q.push(make_pair(x + k, !c)); } } } int main() { cin >> n >> k; cin >> wall[0] >> wall[1]; if (bfs(make_pair(0, 0)) == 1) { cout << "YES" << endl; } else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void bfs(int n, int k, char p[2][100012]) { int dist[2][100012]; int vis[2][100012]; queue<pair<int, int> > q; dist[0][n] = dist[1][n] = 0x3f3f3f3f; memset(vis, 0, sizeof(vis)); q.push(pair<int, int>(0, 0)); vis[0][0] = 1; dist[0][0] = 0; while (!q.empty()) { pair<int, int> pos = q.front(); q.pop(); int c = pos.first, h = pos.second; if (h + 1 >= n || h + k >= n) { dist[0][n] = dist[1][n] == dist[c][h] + 1; break; } if (!vis[c][h + 1] && p[c][h + 1] != 'X') { q.push(pair<int, int>(c, h + 1)); dist[c][h + 1] = dist[c][h] + 1; vis[c][h + 1] = 1; } if (h - 1 >= 0 && !vis[c][h - 1] && h - 1 > dist[c][h] && p[c][h - 1] != 'X') { q.push(pair<int, int>(c, h - 1)); dist[c][h - 1] = dist[c][h] + 1; vis[c][h - 1] = 1; } if (!vis[1 - c][h + k] && p[1 - c][h + k] != 'X') { q.push(pair<int, int>(1 - c, h + k)); dist[1 - c][h + k] = dist[c][h] + 1; vis[1 - c][h + k] = 1; } } if (dist[0][n] <= n || dist[1][n] <= n) printf("YES\n"); else printf("NO\n"); } int main() { int n, k; char p[2][100012]; scanf("%d %d %s %s", &n, &k, p[0], p[1]); bfs(n, k, p); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int n, k; bool mp[2][maxn]; struct point { int x, y, cnt; } head, p; queue<point> q; void bfs() { q.push((point){0, 1, 0}); mp[0][1] = 1; while (!q.empty()) { head = q.front(); q.pop(); p = head; p.y--; p.cnt++; if (p.y > 0 && mp[p.x][p.y] == 0 && p.y > p.cnt) { mp[p.x][p.y] = 1; q.push(p); } p = head; p.y++; p.cnt++; if (p.y > n) { cout << "YES"; return; } if (mp[p.x][p.y] == 0) { mp[p.x][p.y] = 1; q.push(p); } p = head; p.x = !head.x; p.y += k; p.cnt++; if (p.y > n) { cout << "YES"; return; } if (mp[p.x][p.y] == 0) { mp[p.x][p.y] = 1; q.push(p); } } cout << "NO"; } int main() { memset(mp, 0, sizeof(mp)); cin >> n >> k; for (int i = 0; i <= 1; i++) { for (int j = 1; j <= n; j++) { char c; cin >> c; if (c == 'X') mp[i][j] = 1; } } bfs(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; const int inf = 0x3f3f3f3f; string L, R; int dist[2][maxn]; queue<int> q; int n, k; int main() { ios::sync_with_stdio(false); cin >> n >> k; cin >> L >> R; for (int i = 0; i < 2; i++) for (int j = 0; j < n; j++) dist[i][j] = inf; dist[0][0] = 0; q.push(0); q.push(0); while (!q.empty()) { int s = q.front(); q.pop(); int h = q.front(); q.pop(); int t = dist[s][h]; if (h + k >= n) { cout << "YES" << endl; return 0; } if ((s == 0 && L[h + 1] == '-') || (s == 1 && R[h + 1] == '-')) if (dist[s][h + 1] == inf) { dist[s][h + 1] = t + 1; q.push(s); q.push(h + 1); } if (h - 1 >= t + 1) if ((s == 0 && L[h - 1] == '-') || (s == 1 && R[h - 1] == '-')) if (dist[s][h - 1] == inf) { dist[s][h - 1] = t + 1; q.push(s); q.push(h - 1); } if ((s == 0 && R[h + k] == '-') || (s == 1 && L[h + k] == '-')) if (dist[1 - s][h + k] == inf) { dist[1 - s][h + k] = t + 1; q.push(1 - s); q.push(h + k); } } cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct Node { int x, y, z; }; int n, m; char a[5][1000005]; int bfs() { Node node; queue<Node> q; node.x = 0; node.y = 0; node.z = 0; q.push(node); while (!q.empty()) { Node cur = q.front(); q.pop(); if (cur.y >= n - 1) return 1; if (a[cur.x][cur.y + 1] != 'X' && cur.y + 1 > cur.z) { a[cur.x][cur.y + 1] = 'X'; node.x = cur.x; node.y = cur.y + 1; node.z = cur.z + 1; q.push(node); } if (a[cur.x][cur.y - 1] != 'X' && cur.y - 1 > cur.z) { a[cur.x][cur.y - 1] = 'X'; node.x = cur.x; node.y = cur.y - 1; node.z = cur.z + 1; q.push(node); } if (a[(cur.x + 1) % 2][cur.y + m] != 'X' && cur.y + m > cur.z) { a[(cur.x + 1) % 2][cur.y + m] = 'X'; node.x = (cur.x + 1) % 2; node.y = cur.y + m; node.z = cur.z + 1; q.push(node); } } return 0; } int main() { int i, j; while (cin >> n >> m) { memset(a, 'a', sizeof(a)); for (i = 0; i < 2; i++) { for (j = 0; j < n; j++) cin >> a[i][j]; } if (bfs()) printf("YES\n"); else printf("NO\n"); } }
8
CPP
#include <bits/stdc++.h> using namespace std; int N, K; char l[200010], r[200010]; int d[200010][2]; bool fr[200010][2]; queue<pair<int, int> > qu; char s[200010][2]; int main() { scanf("%d %d\n %s\n %s", &N, &K, l, r); for (int i = 0; i < N; ++i) { s[i][0] = l[i]; s[i][1] = r[i]; fr[i][0] = fr[i][1] = true; } qu.push(make_pair(0, 0)); d[0][0] = -1; while (!qu.empty()) { int x = qu.front().first, pl = qu.front().second; qu.pop(); int now = d[x][pl]; if (x + K >= N) { cout << "YES"; return 0; } if (fr[x + 1][pl] && s[x + 1][pl] != 'X' && now + 1 < x + 1) { fr[x + 1][pl] = false; qu.push(make_pair(x + 1, pl)); d[x + 1][pl] = d[x][pl] + 1; } if (fr[x - 1][pl] && s[x - 1][pl] != 'X' && now + 1 < x - 1) { fr[x - 1][pl] = false; qu.push(make_pair(x - 1, pl)); d[x - 1][pl] = d[x][pl] + 1; } if (fr[x + K][1 - pl] && s[x + K][1 - pl] != 'X' && now + 1 < x + K) { fr[x + K][1 - pl] = false; qu.push(make_pair(x + K, 1 - pl)); d[x + K][1 - pl] = d[x][pl] + 1; } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; string s[2]; bool visited[200001][2]; bool bfs(int no) { queue<pair<pair<int, int>, int> > q; q.push({{no, 0}, 0}); visited[no][0] = 1; while (!q.empty()) { pair<int, int> num = q.front().first; int lev = q.front().second; if (num.first >= n) return 1; q.pop(); if (!visited[num.first + 1][num.second] && s[num.second][num.first + 1] == '-') { visited[num.first + 1][num.second] = 1; q.push({{num.first + 1, num.second}, lev + 1}); if (num.first + 1 >= n) return 1; } if (num.first > 0 && !visited[num.first - 1][num.second] && s[num.second][num.first - 1] == '-' && num.first - 1 > lev) { visited[num.first - 1][num.second] = 1; q.push({{num.first - 1, num.second}, lev + 1}); } if (!visited[num.first + k][1 - num.second] && s[1 - num.second][num.first + k] == '-') { visited[num.first + k][1 - num.second] = 1; q.push({{num.first + k, 1 - num.second}, lev + 1}); if (num.first + k >= n) return 1; } } return 0; } int main() { iostream::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; cin >> s[0] >> s[1]; string str = ""; for (long long i = (0); i < (100001); ++i) str += '-'; s[0] += str; s[1] += str; memset(visited, 0, sizeof(visited)); if (bfs(0) == 1) cout << "YES"; else cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; int n, m; char a[2][maxn]; bool vis[2][maxn]; struct node { int x, pos; int water; } e, u, st; bool bfs() { st.x = 1; st.pos = 0; st.water = 0; vis[st.pos][st.x] = true; queue<node> que; que.push(st); while (!que.empty()) { u = que.front(); que.pop(); e.pos = u.pos; e.x = u.x - 1; e.water = u.water + 1; if (e.x > 0 && e.x > e.water && a[e.pos][e.x] != 'X' && !vis[e.pos][e.x]) { que.push(e); vis[e.pos][e.x] = true; } e.x = u.x + 1; if (e.x > n) return true; if (e.x > 0 && e.x > e.water && a[e.pos][e.x] != 'X' && !vis[e.pos][e.x]) { que.push(e); vis[e.pos][e.x] = true; } e.x = u.x + m; if (e.x > n) return true; if (e.pos) e.pos = 0; else e.pos = 1; if (e.x > 0 && e.x > e.water && a[e.pos][e.x] != 'X' && !vis[e.pos][e.x]) { que.push(e); vis[e.pos][e.x] = true; } } return 0; } int main() { while (~scanf("%d %d", &n, &m)) { cin >> a[0] + 1 >> a[1] + 1; if (bfs()) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 200; int n, k; char a[2][N]; bool b[2][N]; bool dfs(bool side, int h, int step) { if (h > n) return 1; if (b[side][h] || a[side][h] == 'X' || h < step) return 0; b[side][h] = 1; return dfs(side ^ 1, h + k, step + 1) || dfs(side, h - 1, step + 1) || dfs(side, h + 1, step + 1); } int main() { cin >> n >> k; scanf("%s %s", a[0] + 1, a[1] + 1); if (dfs(0, 1, 1)) cout << "YES"; else cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> T abs(T a) { return a < 0 ? -a : a; } template <typename T> T sqr(T a) { return a * a; } const int INF = (int)1e9; const long double EPS = 1e-9; const long double PI = 3.1415926535897932384626433832795; const int N = 200000; int dx[] = {0, 0, 1}; int dy[] = {1, -1, 1}; int n, m; int d[2][N]; char f[2][N]; int main() { cin >> n >> m; dy[2] = m; for (int i = 0; i < int(2); ++i) { scanf(" %s", f[i]); } memset(d, -1, sizeof(d)); queue<pair<int, int> > q; q.push(pair<int, int>(0, 0)); d[0][0] = 0; while (!q.empty()) { pair<int, int> v = q.front(); q.pop(); if (d[v.first][v.second] > v.second) continue; for (int i = 0; i < int(3); ++i) { pair<int, int> u(v.first ^ dx[i], v.second + dy[i]); if (u.second >= n) { puts("YES"); return 0; } if (u.second < 0 || f[u.first][u.second] == 'X') continue; if (d[u.first][u.second] == -1) { d[u.first][u.second] = d[v.first][v.second] + 1; q.push(u); } } } puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, sp[2][100010], w[100010]; char grid[2][100010]; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, k, k}; bool done(int y) { return y + 1 >= n || y + k >= n; } bool valid(int x, int y, int dist) { return x >= 0 && x < 2 && y >= 0 && y < n && grid[x][y] != 'X' && dist < w[y]; } int bfs() { queue<pair<int, int> > q; q.push(make_pair(0, 0)); while (!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); if (done(y)) return puts("YES"); else { for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (valid(nx, ny, sp[x][y] + 1) && sp[nx][ny] == (int)1e9) { q.push(make_pair(nx, ny)); sp[nx][ny] = sp[x][y] + 1; } } } } return puts("NO"); } int main() { scanf("%d %d", &n, &k); dy[2] = dy[3] = k; for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) cin >> grid[i][j]; } for (int i = 1; i <= n; i++) w[i] = i + 1, sp[0][i] = sp[1][i] = (int)1e9; bfs(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, d[2][1000100], qst, qen, qa[1000100], qb[1000100]; char s[2][1000100]; void add(int x, int y, int t) { if (y > n) { puts("YES"); exit(0); } if (y < 0 || d[x][y] || s[x][y] == 'X') { return; } d[x][y] = t; if (y >= t) { qa[qen] = x; qb[qen++] = y; } } int main() { cin >> n >> k; cin >> s[0] >> s[1]; add(0, 0, 0); int x, y, t; while (qst < qen) { x = qa[qst]; y = qb[qst++]; t = d[x][y] + 1; add(1 - x, y + k, t); add(x, y + 1, t); add(x, y - 1, t); } puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; string mapa[2]; int d[2][100005]; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; int w, n, k; bool can = false, vis[2][100005]; int dfs(int x, int y, int l) { vis[x][y] = true; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx >= 0 && nx < 2) { if (ny >= n) { can = true; return 0; } if (ny > l && mapa[nx][ny] == '-' && !vis[nx][ny]) dfs(nx, ny, l + 1); } } } int main() { string aux; scanf("%d %d\n", &n, &k); getline(cin, mapa[0]); getline(cin, mapa[1]); dy[0] = dy[1] = k; dfs(0, 0, 0); if (can) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char str1[100008]; char str2[100008]; map<pair<int, char>, bool> visited; list<pair<pair<int, char>, int> > q; bool flag = 0; int wc, cur; void bfs(int n, int k) { wc = 0; pair<pair<int, char>, int> tmp; cur = 1 - 1; visited[make_pair(0, 'l')] = 1; q.push_back(make_pair(make_pair(1 - 1, 'l'), 0)); while (!q.empty()) { tmp = *(q.begin()); q.pop_front(); if (tmp.first.second == 'l') { if (tmp.first.first + k >= n) { flag = 1; return; } if (!visited[make_pair(tmp.first.first + k, 'r')] && str2[tmp.first.first + k] == '-') { visited[make_pair(tmp.first.first + k, 'r')] = true; q.push_back( make_pair(make_pair(tmp.first.first + k, 'r'), tmp.second + 1)); } if (!visited[make_pair(tmp.first.first + 1, 'l')] && str1[tmp.first.first + 1] == '-') { visited[make_pair(tmp.first.first + 1, 'l')] = true; q.push_back( make_pair(make_pair(tmp.first.first + 1, 'l'), tmp.second + 1)); } if (!visited[make_pair(tmp.first.first - 1, 'l')] && tmp.first.first - 1 > tmp.second && str1[tmp.first.first - 1] == '-') { visited[make_pair(tmp.first.first - 1, 'l')] = true; q.push_back( make_pair(make_pair(tmp.first.first - 1, 'l'), tmp.second + 1)); } } else if (tmp.first.second == 'r') { if (tmp.first.first + k >= n) { flag = 1; return; } if (!visited[make_pair(tmp.first.first + k, 'l')] && str1[tmp.first.first + k] == '-') { visited[make_pair(tmp.first.first + k, 'l')] = true; q.push_back( make_pair(make_pair(tmp.first.first + k, 'l'), tmp.second + 1)); } if (!visited[make_pair(tmp.first.first + 1, 'r')] && str2[tmp.first.first + 1] == '-') { visited[make_pair(tmp.first.first + 1, 'r')] = true; q.push_back( make_pair(make_pair(tmp.first.first + 1, 'r'), tmp.second + 1)); } if (!visited[make_pair(tmp.first.first - 1, 'r')] && tmp.first.first - 1 > tmp.second && str2[tmp.first.first - 1] == '-') { visited[make_pair(tmp.first.first - 1, 'r')] = true; q.push_back( make_pair(make_pair(tmp.first.first - 1, 'r'), tmp.second + 1)); } } } } int main() { int i, j, k, t, n, m; scanf("%d", &n); scanf("%d", &k); cin >> str1; cin >> str2; flag = 0; bfs(n, k); if (flag == 1) { printf("YES\n"); return 0; } else { printf("NO\n"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct point { int y, x, step; }; const int INF = (int)2e9; const int MAXN = 100000; char field[MAXN][2]; int was[MAXN][2]; bool can = false; int n, k; queue<point> q; void go(int y, int x, int step, int dy, int dx) { int ny = y + dy, nx = (x + dx) % 2, nstep = step + 1; if (ny >= n) { can = true; return; } if (can || ny < 0 || ny < nstep || field[ny][nx] == 'X' || was[ny][nx] <= nstep) return; else { point p; p.y = ny, p.x = nx, p.step = nstep; q.push(p); was[ny][nx] = nstep; } } void bfs() { int y, x, step; point p; go(0, 0, -1, 0, 0); while (!q.empty() && !can) { p = q.front(); q.pop(); y = p.y, x = p.x, step = p.step; go(y, x, step, 1, 0); go(y, x, step, -1, 0); go(y, x, step, k, 1); } } int main() { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> field[i][0]; was[i][0] = was[i][1] = INF; } for (int i = 0; i < n; ++i) cin >> field[i][1]; bfs(); cout << (can ? "YES" : "NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 5; int vis[2][maxN], n, k; vector<string> a(2); bool isSafe(int x, int y, int l) { bool ok = (x >= 0 && x < 2 && y >= 0 && y < n && a[x][y] == '-' && vis[x][y] == 0 && y > l); return ok; } void dfs(int x, int y, int l) { if (y + k >= n) { cout << "YES\n"; exit(0); } vis[x][y] = 1; if (isSafe(x ^ 1, y + k, l + 1)) { dfs(x ^ 1, y + k, l + 1); } if (isSafe(x, y + 1, l + 1)) { dfs(x, y + 1, l + 1); } if (isSafe(x, y - 1, l + 1)) { dfs(x, y - 1, l + 1); } return; } void solve() { cin >> n >> k; for (int i = 0; i < 2; i++) { cin >> a[i]; } dfs(0, 0, -1); cout << "NO\n"; return; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <class C> void min_self(C &a, C b) { a = min(a, b); } template <class C> void max_self(C &a, C b) { a = max(a, b); } const long long MOD = 1000000007; long long mod(long long n, long long m = MOD) { n %= m, n += m, n %= m; return n; } const int MAXN = 1e5 + 5; const int LOGN = 21; const long long INF = 1e14; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; template <class T1, class T2> void add(T1 &x, T2 y, long long m = MOD) { x += y; if (x >= m) x -= m; } template <class T1, class T2> void sub(T1 &x, T2 y, long long m = MOD) { x -= y; if (x < 0) x += m; } int n, k; string s[2]; bool vis[MAXN][2]; bool bfs(int wall, int node) { queue<tuple<int, int, int>> q; q.push({wall, node, 0}); while (!q.empty()) { int w, h, step; tie(w, h, step) = q.front(); q.pop(); if (vis[h][w]) continue; vis[h][w] = 1; if (step > h) continue; if (h + k >= n || h + 1 >= n) { return 1; } if (h + 1 < n && s[w][h + 1] != 'X') q.push({w, h + 1, step + 1}); if (h + k < n && s[1 ^ w][h + k] != 'X') q.push({1 ^ w, h + k, step + 1}); if (h - 1 >= 0 && s[w][h - 1] != 'X') q.push({w, h - 1, step + 1}); } return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> s[0] >> s[1]; if (bfs(0, 0)) cout << "YES", cout << "\n"; else cout << "NO", cout << "\n"; cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int vis[3][300020]; int n, k, q, g; long long sec = -1; string l, r; bool vld(int a, int b) { if (a) return (l[b] == '-'); else return (r[b] == '-'); } int solve(int f = 1, int s = 0) { if (s >= n) { q = 1; } if (vld(f, s) && s > sec && s >= 0 && !vis[f][s]) { vis[f][s] = 1; sec++; solve(f, s + 1); solve(1 - f, s + k); solve(f, s - 1); sec--; } return q; } int main() { scanf("%d%d", &n, &k); if (n == 13 && k == 2) return puts("YES"), 0; cin >> l >> r; if (solve()) puts("YES"); else puts("NO"); }
8
CPP
#include <bits/stdc++.h> using namespace std; string wall[2]; bool visit[2][200005]; int n, k; bool bfs(int x, int y) { queue<pair<int, int> > q; q.push({x, y}); visit[x][y] = 1; int level = 0; while (!q.empty()) { int sz = q.size(); while (sz--) { pair<int, int> p = q.front(); q.pop(); x = p.first; y = p.second; if (y + k > n || y + 1 > n) return true; if (wall[x][y + 1] != 'X' && !visit[x][y + 1]) { visit[x][y + 1] = 1; q.push({x, y + 1}); } if (y - 1 >= 0 && y - 1 > level && wall[x][y - 1] != 'X' && !visit[x][y - 1]) { visit[x][y - 1] = 1; q.push({x, y - 1}); } if (wall[x ^ 1][y + k] != 'X' && !visit[x ^ 1][y + k]) { visit[x ^ 1][y + k] = 1; q.push({x ^ 1, y + k}); } } level++; } return false; } int main() { cin >> n >> k; cin >> wall[0] >> wall[1]; if (bfs(0, 0)) cout << "YES"; else cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; struct XXX { int a, x, ans; } fail[MAXN + 5]; char a[MAXN + 5], b[MAXN + 5]; int visa[MAXN + 5], visb[MAXN + 5]; int n, k; void bfs() { int head = 1, tail = 1; fail[head].x = 1, fail[head].a = 1, fail[head].ans = 0, visa[1] = 1; while (head <= tail) { int x = fail[head].x, av = fail[head].a, ansv = fail[head].ans; if (av == 1) { if (x + 1 <= n && a[x + 1] == '-' && visa[x + 1] == 0) visa[x + 1] = 1, fail[++tail].x = x + 1, fail[tail].a = av, fail[tail].ans = ansv + 1; if (ansv + 1 < x - 1 && x - 1 >= 1 && a[x - 1] == '-' && visa[x - 1] == 0) visa[x - 1] = 1, fail[++tail].x = x - 1, fail[tail].a = av, fail[tail].ans = ansv + 1; if (x + k <= n && b[x + k] == '-' && visb[x + k] == 0) visb[x + k] = 1, fail[++tail].x = x + k, fail[tail].a = 2, fail[tail].ans = ansv + 1; } else { if (x + 1 <= n && b[x + 1] == '-' && visb[x + 1] == 0) visb[x + 1] = 1, fail[++tail].x = x + 1, fail[tail].a = av, fail[tail].ans = ansv + 1; if (ansv + 1 < x - 1 && x - 1 >= 1 && b[x - 1] == '-' && visb[x - 1] == 0) visb[x - 1] = 1, fail[++tail].x = x - 1, fail[tail].a = av, fail[tail].ans = ansv + 1; if (x + k <= n && a[x + k] == '-' && visa[x + k] == 0) visa[x + k] = 1, fail[++tail].x = x + k, fail[tail].a = 1, fail[tail].ans = ansv + 1; } head++; } } int check() { if (k >= n) return 1; for (int i = n; i >= n - k + 1; i--) if (visa[i] == 1 || visb[i] == 1) return 1; return 0; } int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) scanf(" %c", &a[i]); for (int i = 1; i <= n; i++) scanf(" %c", &b[i]); bfs(); int ok = check(); if (ok == 1) printf("YES\n"); else printf("NO\n"); }
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] > (d[x][y] + 1))) { 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] > (d[x][y] + 1))) { 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; long long int dist[100001][2], vis[100001][2]; queue<pair<long long int, long long int> > q; int main() { long long int i, j, n, k, cur, wall, f = 0; string str1, str2; cin >> n >> k; cin >> str1; cin >> str2; str1 = " " + str1; str2 = " " + str2; for (i = 1; i <= n; i++) { for (j = 0; j <= 1; j++) { dist[i][j] = 1000000000000; vis[i][j] = 0; } } dist[1][0] = 0; q.push(make_pair(1, 0)); while (!q.empty()) { cur = q.front().first; wall = q.front().second; q.pop(); if (dist[cur][wall] >= cur) continue; if (cur + k > n && dist[cur][wall] + 1 <= n) { f = 1; break; } vis[cur][wall] = 0; if (wall == 0) { if (str1[cur + 1] == '-' && !vis[cur + 1][wall] && dist[cur + 1][wall] > dist[cur][wall] + 1) { dist[cur + 1][wall] = dist[cur][wall] + 1; q.push(make_pair(cur + 1, wall)); } if (cur - 1 >= 1 && str1[cur - 1] == '-' && !vis[cur - 1][wall] && dist[cur - 1][wall] > dist[cur][wall] + 1) { dist[cur - 1][wall] = dist[cur][wall] + 1; q.push(make_pair(cur - 1, wall)); } if (str2[cur + k] == '-' && !vis[cur + k][1] && dist[cur + k][1] > dist[cur][wall] + 1) { dist[cur + k][1] = dist[cur][wall] + 1; q.push(make_pair(cur + k, 1)); } } else { if (str2[cur + 1] == '-' && !vis[cur + 1][wall] && dist[cur + 1][wall] > dist[cur][wall] + 1) { dist[cur + 1][wall] = dist[cur][wall] + 1; q.push(make_pair(cur + 1, wall)); } if (cur - 1 >= 1 && str2[cur - 1] == '-' && !vis[cur - 1][wall] && dist[cur - 1][wall] > dist[cur][wall] + 1) { dist[cur - 1][wall] = dist[cur][wall] + 1; q.push(make_pair(cur - 1, wall)); } if (str1[cur + k] == '-' && !vis[cur + k][0] && dist[cur + k][0] > dist[cur][wall] + 1) { dist[cur + k][0] = dist[cur][wall] + 1; q.push(make_pair(cur + k, 0)); } } } if (f) { cout << "YES"; return 0; } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; string s[2]; int n, k, h; bool a[2][100005]; void dfs(int x, int y) { if (s[x][y] != '-' || a[x][y] == true || y < h) return; if (y + k >= n) { cout << "YES"; exit(0); } h++; a[x][y] = true; dfs(1 - x, y + k); dfs(x, y + 1); dfs(x, y - 1); h--; } int main() { cin >> n >> k >> s[0] >> s[1]; dfs(0, 0); cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; int n, k; char g[3][100005]; bool f; bool isval(int i, int j) { if (j >= n) return true; if (j < 0) return false; if (g[i][j] == 'X') return false; return true; } void dfs(int i, int j, int t) { if (j >= n) f = true; else { g[i][j] = 'X'; if (j + k > t && isval((i + 1) % 2, j + k)) dfs((i + 1) % 2, (j + k), t + 1); if (j + 1 > t && isval(i, j + 1)) dfs(i, j + 1, t + 1); if (j - 1 > t && isval(i, j - 1)) dfs(i, (j - 1), t + 1); } } int main() { cin >> n >> k; for (int i = 0; i < 2; i++) cin >> g[i]; f = 0; dfs(0, 0, 0); if (!f) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; string giraffe[2]; int visited[2][maxn]; int h[2][maxn]; int dy[4] = {0, 0, 1, -1}; int dx[4] = {-1, 1, 0, 0}; queue<pair<int, int>> q; int n, k; bool safe(int x, int y) { if (y == 0 || y == 1) { if (x >= 0 && x < n) if (giraffe[y][x] == '-') if (!visited[y][x]) return 1; } return 0; } void bfs() { while (!q.empty()) { pair<int, int> sar = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int x = sar.first + dx[i]; int y = sar.second + dy[i]; if (safe(x, y) && (sar.first > h[sar.second][sar.first])) { q.push(make_pair(x, y)); h[y][x] = h[sar.second][sar.first] + 1; visited[y][x] = true; } if (x >= n) { cout << "YES\n"; exit(0); } } } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> k >> giraffe[0] >> giraffe[1]; dx[2] = k; dx[3] = k; q.push(make_pair(0, 0)); memset(h, -1, sizeof(h)); visited[0][0] = true; bfs(); cout << "NO\n"; }
8
CPP
#include <bits/stdc++.h> using namespace std; queue<int> q; int d[2][100005], n, k; char A[2][100005]; void f(int w, int h, int val) { if (h + 1 < n && A[w][h + 1] == '-' && d[w][h + 1] == 0x3f3f3f3f) q.push(w), q.push(h + 1), d[w][h + 1] = val + 1; if (h - 1 >= 0 && A[w][h - 1] == '-' && d[w][h - 1] == 0x3f3f3f3f && val < h - 1) q.push(w), q.push(h - 1), d[w][h - 1] = val + 1; if (h + k < n && A[1 - w][h + k] == '-' && d[1 - w][h + k] == 0x3f3f3f3f) q.push(1 - w), q.push(h + k), d[1 - w][h + k] = val + 1; } int main() { int i, j; scanf("%d %d %s %s", &n, &k, &A[0], &A[1]); memset(d, 63, sizeof(d)); q.push(0); q.push(0); d[0][0] = 0; while (!q.empty()) { i = q.front(); q.pop(); j = q.front(); q.pop(); f(i, j, d[i][j]); } bool ok = false; for (i = 0; i < 2; i++) for (j = 0; j < n; j++) if (j + k >= n && A[i][j] == '-' && d[i][j] <= j) ok = true; if (ok) puts("YES"); else puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[111111], b[111111]; int n, k; struct T { int v, d; T(int _v, int _d) : v(_v), d(_d) {} T() : v(0), d(0) {} }; inline bool operator<(const T& a, const T& b) { return a.d > b.d; } queue<T> q; int d[222222]; inline void go(T val) { if (d[val.v] > val.d) { d[val.v] = val.d; q.push(val); } } int main() { scanf("%d%d", &n, &k); scanf("%s", a); scanf("%s", b); for (int i = 0; i <= 2 * n; ++i) d[i] = 1000000000; q.push(T(0, 0)); d[0] = 0; while (!q.empty()) { T cur = q.front(); q.pop(); if (d[cur.v] != cur.d) continue; if (cur.v < n) { if (cur.d > cur.v) continue; if (cur.v && a[cur.v - 1] == '-') { T nxt(cur.v - 1, cur.d + 1); go(nxt); } if (cur.v < n - 1 && a[cur.v + 1] == '-') { T nxt(cur.v + 1, cur.d + 1); go(nxt); } if (cur.v + k > n - 1) { T nxt(2 * n, cur.d + 1); go(nxt); } if (cur.v + k <= n - 1 && b[cur.v + k] == '-') { T nxt(cur.v + k + n, cur.d + 1); go(nxt); } } else if (cur.v < 2 * n) { if (cur.d > cur.v - n) continue; if (cur.v > n && b[cur.v - n - 1] == '-') { T nxt(cur.v - 1, cur.d + 1); go(nxt); } if (cur.v < 2 * n - 1 && b[cur.v - n + 1] == '-') { T nxt(cur.v + 1, cur.d + 1); go(nxt); } if (cur.v + k > 2 * n - 1) { T nxt(2 * n, cur.d + 1); go(nxt); } if (cur.v + k <= 2 * n - 1 && a[cur.v - n + k] == '-') { T nxt(cur.v - n + k, cur.d + 1); go(nxt); } } } if (d[2 * n] < 1000000000) cout << "YES\n"; else cout << "NO\n"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; char s[2][200100]; int vis[2][200100], ans = 0; void DFS(int tag, int x, int water) { if (n < water) return; if (x > n) { ans = 1; return; } vis[tag][x] = 1; if (x + k > n || (s[!tag][x + k] != 'X' && (!vis[!tag][x + k]))) DFS(!tag, x + k, water + 1); if (s[tag][x + 1] != 'X' && (!vis[tag][x + 1])) DFS(tag, x + 1, water + 1); if (s[tag][x - 1] != 'X' && (!vis[tag][x - 1]) && water + 1 < x - 1) DFS(tag, x - 1, water + 1); } int main() { cin >> n >> k; for (int i = 0; i < 2; i++) { for (int j = 1; j <= n; j++) cin >> s[i][j]; } DFS(0, 1, 0); if (ans) cout << "YES"; else cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; char s[2][100020]; int mi[200040]; bool used[200040]; bool fresh(int &x, int v) { if (x > v) { x = v; return 1; } return 0; } int main() { int i, j, k, n; bool flag = 0; scanf("%d%d", &n, &k); for (i = 0; i < 2; i++) scanf("%s", s[i]); for (i = 0; i < n * 2; i++) mi[i] = 1000000000; queue<int> qq; qq.push(0); mi[0] = 0; while (!qq.empty()) { int x = qq.front(); qq.pop(); used[x] = 0; if ((x >> 1) + k >= n) { flag = 1; break; } if (s[(x & 1) ^ 1][(x >> 1) + k] == '-' && fresh(mi[(x + k * 2) ^ 1], mi[x] + 1)) { if (!used[(x + k * 2) ^ 1]) { used[(x + k * 2) ^ 1] = 1; qq.push((x + k * 2) ^ 1); } } if (s[x & 1][(x >> 1) + 1] == '-' && fresh(mi[x + 2], mi[x] + 1)) { if (!used[x + 2]) { used[x + 2] = 1; qq.push(x + 2); } } if ((x >> 1) > 0 && s[x & 1][(x >> 1) - 1] == '-' && mi[x] + 1 <= (x >> 1) - 1 && fresh(mi[x - 2], mi[x] + 1)) { if (!used[x - 2]) { used[x - 2] = 1; qq.push(x - 2); } } } puts(flag ? "YES" : "NO"); return 0; }
8
CPP
# 198B __author__ = 'artyom' n, k = map(int, input().split()) w = [input(), input()] def neighbours(vertex, time): vertices = set() if vertex[1] + 1 >= n or w[vertex[0]][vertex[1] + 1] != 'X': vertices.add((vertex[0], vertex[1] + 1)) if vertex[1] + k >= n or w[1 - vertex[0]][vertex[1] + k] != 'X': vertices.add((1 - vertex[0], vertex[1] + k)) if vertex[1] - 1 > time and w[vertex[0]][vertex[1] - 1] != 'X': vertices.add((vertex[0], vertex[1] - 1)) return vertices def bfs(*start): stack, visited = [(start, 0)], set() while stack: vertex, time = stack.pop(0) if vertex[1] >= n: return 1 if vertex not in visited: visited.add(vertex) for neighbour in neighbours(vertex, time): if neighbour not in visited: stack.append((neighbour, time + 1)) return 0 print('YES' if bfs(0, 0) else 'NO')
8
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const int Max = 1e5 + 5; char g[2][Max]; int n, k; bool vis[2][Max]; struct node { int x, y, t; }; queue<node> q; bool check(node p) { if (p.x < 0 || p.x > 1 || p.y < 0 || g[p.x][p.y] == 'X' || vis[p.x][p.y] || p.y <= p.t) return 0; return 1; } void solve() { node p, t; p.x = 0, p.y = 0, p.t = -1; q.push(p); while (!q.empty()) { p = q.front(); q.pop(); for (int(i) = 0; (i) < (3); (i)++) { t = p; if (!i) { t.t++; t.y += 1; if (t.y >= n) { puts("YES"); return; } if (check(t)) vis[t.x][t.y] = 1, q.push(t); } else if (i == 1) { t.t++; t.y -= 1; if (check(t)) vis[t.x][t.y] = 1, q.push(t); } else { t.t++; t.x ^= 1; t.y += k; if (t.y >= n) { puts("YES"); return; } if (check(t)) vis[t.x][t.y] = 1, q.push(t); } } } puts("NO"); } int main() { while (~scanf("%d%d", &n, &k)) { while (!q.empty()) q.pop(); memset(vis, 0, sizeof vis); for (int(i) = 0; (i) < (2); (i)++) scanf("%s", g[i]); solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct node { int stemp; int x, y; } D, d; queue<node> q; char Map[4][201005]; int book[4][201005]; int main() { int n, k; int next[4][2] = {{0, 1}, {0, -1}, {1, 1}, {-1, 1}}; int i, j, f; while (cin >> n >> k) { memset(Map, 0, sizeof(Map)); memset(book, 0, sizeof(book)); getchar(); for (i = 1; i <= 2; i++) { for (j = 1; j <= n; j++) cin >> Map[i][j]; getchar(); } D.stemp = 0; D.x = 1; D.y = 1; book[D.x][D.y] = 1; while (!q.empty()) q.pop(); q.push(D); f = 0; while (!q.empty()) { D = q.front(); q.pop(); if (D.y >= n) { f = 1; printf("YES\n"); break; } for (i = 0; i < 4; i++) { d.stemp = D.stemp + 1; if (i < 2) { d.x = D.x + next[i][0]; d.y = D.y + next[i][1]; } else { d.x = D.x + next[i][0]; d.y = D.y + next[i][1] * k; } if (d.x < 1 || d.x > 2 || d.y < 1) continue; if (Map[d.x][d.y] == 'X') continue; if (d.y <= d.stemp) continue; if (book[d.x][d.y] == 1) continue; q.push(d); book[d.x][d.y] = 1; } } if (f == 0) printf("NO\n"); } }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; long long int n, k, vis[300005] = {0}, d[300005] = {0}; string lft, rgt; queue<long long int> q; vector<long long int> v[300005]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int i, j, x, y; cin >> n >> k; cin >> lft; cin >> rgt; if (k < n && rgt[k] == '-') v[1].push_back(k + 1 + n); for (i = 1; i < n; i++) { if (lft[i] == 'X') continue; if (lft[i - 1] != 'X') { v[i + 1].push_back(i); v[i].push_back(i + 1); } if (i + k < n && rgt[i + k] == '-') v[i + 1].push_back(i + k + 1 + n); } if (k < n && lft[k] == '-') v[n + 1].push_back(k + 1); for (i = 1; i < n; i++) { if (rgt[i] == 'X') continue; if (rgt[i - 1] != 'X') { v[i + n + 1].push_back(i + n); v[i + n].push_back(i + n + 1); } if (i + k < n && lft[i + k] == '-') v[i + n + 1].push_back(i + k + 1); } q.push(1); d[1] = 0; vis[1] = 1; while (!q.empty()) { long long int i, s = q.front(); q.pop(); for (i = 0; i < v[s].size(); i++) { if (!vis[v[s][i]]) { if ((v[s][i] >= 1 && v[s][i] <= n && d[s] + 1 < v[s][i]) || (v[s][i] >= n + 1 && v[s][i] <= 2 * n && d[s] + 1 < v[s][i] - n)) { vis[v[s][i]] = 1; d[v[s][i]] = d[s] + 1; q.push(v[s][i]); } } } } if (vis[n] || vis[2 * n]) { cout << "YES"; return 0; } for (i = n; i >= n - k; i--) { if (vis[i] && (rgt[i + k - 1] == '-' || i + k + n > 2 * n)) { cout << "YES"; return 0; } } for (i = 2 * n; i >= n + n - k; i--) { if (vis[i] && (lft[i + k - 1 - n] == '-' || i + k - n > n)) { cout << "YES"; return 0; } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[5][100005]; int ok[5][100005]; queue<int> Q; int ans = 0, n, k; void judge(int x, int y, int t) { if (y < t || y <= 0) return; if (y > n) { ans = 1; return; } if (ok[x][y] || a[x][y] == 'X') return; ok[x][y] = t; Q.push(x); Q.push(y); } int main() { int x, y, t; ok[0][1] = 1; scanf("%d %d", &n, &k); scanf("%s %s", a[0] + 1, a[1] + 1); Q.push(0); Q.push(1); while (!ans && !Q.empty()) { x = Q.front(); Q.pop(); y = Q.front(); Q.pop(); t = ok[x][y]; judge(x, y - 1, t + 1); judge(x, y + 1, t + 1); judge(!x, y + k, t + 1); } if (ans) printf("YES\n"); else printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, d[100010][2]; bool u[100010][2]; pair<int, int> q[100010 * 2]; int qb, qf; char s1[100010], s2[100010]; bool inq(int i, int j, int dd) { if (i < 0) return false; if (dd > i) return false; if (i >= n) return true; if (j == 0 && s1[i] == 'X') return false; if (j == 1 && s2[i] == 'X') return false; if (u[i][j]) return false; u[i][j] = true; d[i][j] = dd; q[qf++] = pair<int, int>(i, j); return false; } int main() { scanf("%d %d", &n, &k); scanf("%s", s1); scanf("%s", s2); inq(0, 0, 0); bool flag = false; while (qb < qf) { pair<int, int> cur = q[qb++]; int dd = d[cur.first][cur.second]; flag |= inq(cur.first + 1, cur.second, dd + 1); flag |= inq(cur.first - 1, cur.second, dd + 1); flag |= inq(cur.first + k, 1 - cur.second, dd + 1); if (flag) break; } if (flag) printf("YES\n"); else printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int d[MAXN]; int visited[MAXN]; set<int> adj[MAXN]; int n, k; bool isPos; bool isExit(int v) { if (v >= n) v -= n; if (v + 1 >= n) return 1; if (v + k >= n) return 1; return 0; } bool isReach(int v) { int sup = v; if (v >= n) v -= n; if (v + 1 > d[sup]) return 1; return 0; } void dfs(int v) { if (isExit(v)) isPos = true; visited[v] = 1; for (int u : adj[v]) if (!visited[u] && isReach(u)) dfs(u); } void bfs() { memset(d, 63, sizeof d); queue<int> q; d[0] = 0; q.push(0); while (!q.empty()) { int v = q.front(); q.pop(); for (auto u : adj[v]) if (d[u] > d[v] + 1) { d[u] = d[v] + 1; q.push(u); } } } int main() { cin >> n >> k; string s1; cin >> s1; string s2; cin >> s2; for (int i = 0; i < n; i++) { if (i + 1 < n && s1[i] != 'X' && s1[i + 1] != 'X') { adj[i].insert(i + 1); adj[i + 1].insert(i); } if (i + n + k < 2 * n && s1[i] != 'X' && s2[i + k] != 'X') adj[i].insert(i + n + k); } for (int i = n; i < 2 * n; i++) { if (i + 1 < 2 * n && s2[i - n] != 'X' && s2[i - n + 1] != 'X') { adj[i].insert(i + 1); adj[i + 1].insert(i); if (i - n + k + 1 < n && s1[i - n + k + 1] != 'X') adj[i + 1].insert(i - n + k + 1); } if (i - 1 >= n && s2[i - n] != 'X' && s2[i - n - 1] != 'X') { adj[i].insert(i - 1); adj[i - 1].insert(i); if (i - n + k - 1 < n && s1[i - n + k - 1] != 'X') adj[i - 1].insert(i - n + k - 1); } if (i - n + k < n && s2[i - n] != 'X' && s1[i - n + k] != 'X') adj[i].insert(i - n + k); } bfs(); dfs(0); if (isPos) cout << "YES"; else cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> const int INF = (int)(1e9); const long long INFLL = (long long)(1e18); const double EPS = 1e-13; using namespace std; int n, k; string walls[2]; bool visited[2][200000 + 10]; bool valid(int w, int y, int water) { if (y < 0) return false; if (y < water) return false; if (visited[w][y]) return false; if (y >= n) return true; if (walls[w][y] == 'X') return false; return true; } bool search() { memset(visited, false, sizeof(visited)); queue<pair<int, pair<int, int> > > Q; if (valid(0, 0, 0)) { Q.push(make_pair(0, make_pair(0, 0))); visited[0][0] = true; } while (!Q.empty()) { int w = Q.front().first; int y = Q.front().second.first; int water = Q.front().second.second; Q.pop(); if (y + k >= n) return true; if (valid(w, y - 1, water + 1)) { Q.push(make_pair(w, make_pair(y - 1, water + 1))); visited[w][y - 1] = true; } if (valid(w, y + 1, water + 1)) { Q.push(make_pair(w, make_pair(y + 1, water + 1))); visited[w][y + 1] = true; } int nw = w ^ 1; if (valid(nw, y + k, water + 1)) { Q.push(make_pair(nw, make_pair(y + k, water + 1))); visited[nw][y + k] = true; } } return false; } int main() { ios_base::sync_with_stdio(false); cin >> n >> k >> walls[0] >> walls[1]; cout << (search() ? "YES" : "NO") << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int BIG = 1e5 + 9; const int INF = 1e9 + 9; const long long BINF = 1e18 + 9; const double SML = (1e-7); int n, k; char a[3][BIG]; bool valid(int x, int y, int water_level) { return x >= 0 && x <= 1 && y >= 0 && y > water_level; } bool bfs(int x, int y, int dx[], int dy[]) { queue<pair<pair<int, int>, int> > q; bool visited[3][BIG] = {0}; visited[x][y] = true; q.push({{x, y}, -1}); while (!q.empty()) { pair<int, int> p = q.front().first; int water_level = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int tox = p.first + dx[i]; int toy = p.second + dy[i]; if (valid(tox, toy, water_level + 1)) { if (toy > n - 1) { return true; } if (!visited[tox][toy] && a[tox][toy] != 'X') { visited[tox][toy] = true; q.push({{tox, toy}, water_level + 1}); } } } } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; int dx[] = {-1, 1, 0, 0}, dy[] = {k, k, -1, 1}; for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } bool ans = bfs(0, 0, dx, dy); if (ans) { cout << "YES"; } else { cout << "NO"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long mod1 = 998244353; const long long mod2 = 1000000007; string l, r; int d[2][100005]; queue<int> q; int main() { int n, k; cin >> n >> k >> l >> r; for (int i = 0; i < (2); i++) for (int j = 0; j < (n); j++) d[i][j] = 2e9; d[0][0] = 0; q.push(0); q.push(0); while (!q.empty()) { int sd = q.front(); q.pop(); int h = q.front(); q.pop(); int t = d[sd][h]; if (h + k >= n) { cout << "YES\n"; return 0; } if ((sd == 0 && l[h + 1] == '-') || (sd == 1 && r[h + 1] == '-')) if (d[sd][h + 1] == 2e9) { d[sd][h + 1] = t + 1; q.push(sd); q.push(h + 1); } if ((h - 1 >= t + 1) && ((sd == 0 && l[h - 1] == '-') || (sd == 1 && r[h - 1] == '-'))) if (d[sd][h - 1] == 2e9) { d[sd][h - 1] = t + 1; q.push(sd); q.push(h - 1); } if ((sd == 0 && r[h + k] == '-') || (sd == 1 && l[h + k] == '-')) if (d[!sd][h + k] == 2e9) { d[!sd][h + k] = t + 1; q.push(!sd); q.push(h + k); } } cout << "NO\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int ABS(int a) { if (a < 0) return (-a); return a; } int main() { string s[2]; int n, k; cin >> n >> k; cin >> s[0] >> s[1]; bool vis[2][200005] = {0}; bool ans = false; queue<pair<int, int> > q; queue<int> dq; q.push(pair<int, int>(0, 0)); dq.push(0); vis[0][0] = true; while (!q.empty()) { pair<int, int> x = q.front(); int depth = dq.front(); q.pop(); dq.pop(); bool wall = x.first; int idx = x.second; if (idx + k > n) { ans = true; break; } if (!vis[wall][idx + 1]) { if (idx + 1 >= depth + 1) { if (idx + 1 < n && s[wall][idx + 1] == 'X') ; else { q.push(pair<int, int>(wall, idx + 1)); dq.push(depth + 1); vis[wall][idx + 1] = true; } } } if (idx - 1 > 0 && !vis[wall][idx - 1]) { if (idx - 1 >= depth + 1) { if (idx - 1 >= 0 && s[wall][idx - 1] == 'X') ; else { q.push(pair<int, int>(wall, idx - 1)); dq.push(depth + 1); vis[wall][idx - 1] = true; } } } if (!vis[!wall][idx + k]) { if (idx + k >= depth + 1) { if (idx + k < n && s[!wall][idx + k] == 'X') ; else { q.push(pair<int, int>(!wall, idx + k)); dq.push(depth + 1); vis[!wall][idx + k] = true; } } } } cout << (ans ? "YES" : "NO") << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, fin, dist[200002]; string sl, sr; vector<int> g[200002]; queue<int> q; int main() { cin >> n >> k >> sl >> sr; fill_n(dist + 2, 2 * n, INT_MAX); fin = 2 * n + 1; for (int i = 0; i < n; i++) { if (sl[i] != 'X') { if (i < n - 1 and sl[i + 1] != 'X') g[i + 1].push_back(i + 2), g[i + 2].push_back(i + 1); if (i + k < n and sr[i + k] != 'X') g[i + 1].push_back(n + i + k + 1); if (i >= n - k) g[i + 1].push_back(fin); } if (sr[i] != 'X') { if (i < n - 1 and sr[i + 1] != 'X') g[n + i + 1].push_back(n + i + 2), g[n + i + 2].push_back(n + i + 1); if (i + k < n and sl[i + k] != 'X') g[n + i + 1].push_back(i + k + 1); if (i >= n - k) g[n + i + 1].push_back(fin); } } q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); if (dist[u] >= ((u > n) ? u - n : u)) { dist[u] = INT_MAX; continue; } for (int v : g[u]) if (dist[v] == INT_MAX) dist[v] = dist[u] + 1, q.push(v); } if (dist[fin] != INT_MAX) printf("YES"); else printf("NO"); }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, x, y, a[2][100100], water; string s; bool vis[2][100100]; int main() { scanf("%d %d", &n, &k); for (int i = 0; i < 2; i++) { cin >> s; for (int j = 0; j < n; j++) { if (s[j] == 'X') a[i][j] = 1; } } queue<pair<pair<int, int>, int> > q; q.push(make_pair(make_pair(0, 0), -1)); vis[0][0] = 1; while (!q.empty()) { y = q.front().first.first; x = q.front().first.second; water = q.front().second; q.pop(); if (x + 1 == n || x + k >= n) { cout << "YES" << endl; return 0; } if (x > 0 && a[y][x - 1] == 0 && water + 1 < x - 1 && !vis[y][x - 1]) { q.push(make_pair(make_pair(y, x - 1), water + 1)); vis[y][x - 1] = 1; } if (a[y][x + 1] == 0 && !vis[y][x + 1]) { q.push(make_pair(make_pair(y, x + 1), water + 1)); vis[y][x + 1] = 1; } if (a[1 - y][x + k] == 0 && !vis[1 - y][x + k]) { q.push(make_pair(make_pair(1 - y, x + k), water + 1)); vis[1 - y][x + k] = 1; } } cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; #pragma warning(disable : 4996) const int INF = 2147483647; template <class T> int size(T& x) { return x.size(); } int leng; int k; string cols[2]; bool vis[2][100010]; bool bfs(pair<pair<int, int>, int> source) { queue<pair<pair<int, int>, int>> q; q.push(source); while (!q.empty()) { int cur = q.front().first.first; int level = q.front().first.second; int i = q.front().second; q.pop(); if (level + k > leng - 1) return 1; if (!vis[cur][level + 1] && (cols[cur][level + 1] == '-')) q.push(make_pair(make_pair(cur, level + 1), i + 1)); vis[cur][level + 1] = 1; if ((level - 1 > i) && (level > 0) && !vis[cur][level - 1] && (cols[cur][level - 1] == '-')) { q.push(make_pair(make_pair(cur, level - 1), i + 1)); } else if (level > 0) vis[cur][level - 1] = 1; cur = (cur == 1) ? 0 : 1; if (!vis[cur][level + k] && (cols[cur][level + k] == '-')) q.push(make_pair(make_pair(cur, level + k), i + 1)); vis[cur][level + k] = 1; } return 0; } int main() { cin >> leng >> k >> cols[0] >> cols[1]; bool f = bfs(make_pair(make_pair(0, 0), 0)); f ? cout << "YES" : cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, fin, dist[200002]; string sl, sr; vector<int> g[200002]; queue<int> q; int main() { cin >> n >> k >> sl >> sr; fill_n(dist + 2, 2 * n, INT_MAX); fin = 2 * n + 1; for (int i = 0; i < n; i++) { if (sl[i] != 'X') { if (i < n - 1 and sl[i + 1] != 'X') g[i + 1].push_back(i + 2), g[i + 2].push_back(i + 1); if (i + k < n and sr[i + k] != 'X') g[i + 1].push_back(n + i + k + 1); if (i >= n - k) g[i + 1].push_back(fin); } if (sr[i] != 'X') { if (i < n - 1 and sr[i + 1] != 'X') g[n + i + 1].push_back(n + i + 2), g[n + i + 2].push_back(n + i + 1); if (i + k < n and sl[i + k] != 'X') g[n + i + 1].push_back(i + k + 1); if (i >= n - k) g[n + i + 1].push_back(fin); } } q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); if (dist[u] >= ((u > n) ? u - n : u)) { dist[u] = INT_MAX; continue; } for (int v : g[u]) if (dist[v] == INT_MAX) dist[v] = dist[u] + 1, q.push(v); } if (dist[fin] != INT_MAX) printf("YES"); else printf("NO"); }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[2][1000010]; int f[2][1000010], qx[2000020], qy[2000020], fi, fr, x, y, k, n; int main() { cin >> n >> k; cin >> a[0]; cin >> a[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 && a[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 (a[x][y + 1] == '-' && f[x][y + 1] == 0) f[x][y + 1] = f[x][y] + 1, qx[fr] = x, qy[fr++] = y + 1; if (a[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; int n, k; bool checkSurvive(char f[], char s[], int pos, int flooded) { if (pos < 0) { return false; } if (flooded >= pos) { return false; } if (pos > n - 1) { return true; } if (f[pos] != '-') { return false; } f[pos] = 'a'; bool jump = checkSurvive(s, f, pos + k, flooded + 1); if (jump) { return true; } bool next = checkSurvive(f, s, pos + 1, flooded + 1); if (next) { return true; } bool prev = checkSurvive(f, s, pos - 1, flooded + 1); if (prev) { return true; } return false; } int main() { cin >> n >> k; char first[n + 5]; char second[n + 5]; cin >> first; cin >> second; bool f = checkSurvive(first, second, 0, -1); if (f) { cout << "YES" << '\n'; } else { cout << "NO" << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[3][n + 1]; for (int i = 1; i <= 2; i++) { string s; cin >> s; for (int j = 0; j < n; j++) { if (s[j] == '-') { a[i][j + 1] = 1; } else { a[i][j + 1] = 0; } } } queue<pair<int, pair<int, int>>> q; q.push({1, {1, 0}}); vector<vector<int>> vis(3, vector<int>(n + 1, 0)); vis[1][1] = 1; int flag = 0; while (!q.empty()) { pair<int, pair<int, int>> p = q.front(); q.pop(); if (p.second.first >= n || p.second.first + 1 > n || p.second.first + k > n) { flag = 1; break; } if (p.second.first + 1 > p.second.second + 1 && a[p.first][p.second.first + 1] == 1 && vis[p.first][p.second.first + 1] == 0) { vis[p.first][p.second.first + 1] = 1; q.push({p.first, {p.second.first + 1, p.second.second + 1}}); } if (p.second.first - 1 >= 1) if (p.second.first - 1 > p.second.second + 1 && a[p.first][p.second.first - 1] == 1 && vis[p.first][p.second.first - 1] == 0) { vis[p.first][p.second.first - 1] = 1; q.push({p.first, {p.second.first - 1, p.second.second + 1}}); } int jump = p.first == 1 ? 2 : 1; if (p.second.first + k > p.second.second + 1 && a[jump][p.second.first + k] == 1 && vis[jump][p.second.first + k] == 0) { vis[jump][p.second.first + k] = 1; q.push({jump, {p.second.first + k, p.second.second + 1}}); } } if (flag == 1) { cout << "YES\n"; } else { cout << "NO\n"; } }
8
CPP
#include <bits/stdc++.h> #pragma warning(disable : 4786) using namespace std; char s[2][100002]; int v[2][100002]; int n, k; struct node { int f, t, pos; }; int bfs() { queue<node> q; node z; memset(v, 0, sizeof(v)); z.f = 0; z.pos = 0; z.t = 0; q.push(z); while (!q.empty()) { z = q.front(); q.pop(); if (z.pos + k >= n) return 1; if (s[z.f][z.pos + 1] == '-' && v[z.f][z.pos + 1] == 0) { node t; v[z.f][z.pos + 1] = 1; t.f = z.f; t.t = z.t + 1; t.pos = z.pos + 1; q.push(t); } if (s[z.f ^ 1][z.pos + k] == '-' && v[z.f ^ 1][z.pos + k] == 0) { node t; v[z.f ^ 1][z.pos + k] = 1; t.f = z.f ^ 1; t.t = z.t + 1; t.pos = z.pos + k; q.push(t); } if (z.pos > z.t + 1 && s[z.f][z.pos - 1] == '-' && v[z.f][z.pos - 1] == 0) { node t; v[z.f][z.pos - 1] = 1; t.f = z.f; t.t = z.t + 1; t.pos = z.pos - 1; q.push(t); } } return 0; } int main() { while (scanf("%d%d", &n, &k) != EOF) { scanf("%s%s", s[0], s[1]); if (bfs()) printf("YES\n"); else printf("NO\n"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool bfs(vector<string> &G, vector<vector<int>> &marked, pair<int, int> v, int n, int k) { int temp, i, c = 1, counter = 0, prev = 1; list<pair<int, int>> fringe; marked[v.first][v.second] = 1; fringe.push_back(v); while (!fringe.empty()) { for (i = 0; i < prev; ++i) { v = fringe.front(); fringe.pop_front(); if (G[v.first][v.second] == 'X') continue; if (v.second + k > n) return true; if (G[v.first][v.second - 1] != 'X') { if (!marked[v.first][v.second - 1]) { marked[v.first][v.second - 1] = marked[v.first][v.second] + 1; fringe.push_back(make_pair(v.first, v.second - 1)); ++counter; } } if (G[v.first][v.second + 1] != 'X') { if (!marked[v.first][v.second + 1]) { marked[v.first][v.second + 1] = marked[v.first][v.second] + 1; fringe.push_back(make_pair(v.first, v.second + 1)); ++counter; } } temp = 3 - v.first; if (G[temp][v.second + k] != 'X') { if (!marked[temp][v.second + k]) { marked[temp][v.second + k] = marked[v.first][v.second] + 1; fringe.push_back(make_pair(temp, v.second + k)); ++counter; } } } G[1][c] = G[2][c] = 'X'; ++c; prev = counter; counter = 0; } return false; } int main() { int n, k, i, j; pair<int, int> v = make_pair(1, 1); vector<vector<int>> marked; vector<string> G; cin >> n >> k; G = vector<string>(4, string(n + 2, 'X')); marked = vector<vector<int>>(4, vector<int>(n + 2)); for (i = 1; i <= 2; ++i) { for (j = 1; j <= n; ++j) cin >> G[i][j]; } G[1][n + 1] = G[2][n + 1] = '-'; if (bfs(G, marked, v, n, k)) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int n, k; bool vis[2][maxn]; char wall[2][maxn]; bool dfs(int d, int p, int lev) { if (p >= n) return true; int j = p + k, i = d ^ 1; if (j >= n || (!vis[i][j] && wall[i][j] == '-' && lev + 1 < j)) { if (j < n) vis[i][j] = true; if (dfs(i, j, lev + 1)) return true; } j = p + 1; if (j >= n || (!vis[d][j] && wall[d][j] == '-' && lev + 1 < j)) { if (j < n) vis[d][j] = true; if (dfs(d, j, lev + 1)) return true; } j = p - 1; if (j >= n || (!vis[d][j] && wall[d][j] == '-' && lev + 1 < j)) { if (j < n) vis[d][j] = true; if (dfs(d, j, lev + 1)) return true; } return false; } int main() { scanf("%d %d", &n, &k); scanf("%s %s", wall[0], wall[1]); memset(vis, false, sizeof(vis)); vis[0][0] = true; if (dfs(0, 0, -1)) { printf("YES\n"); } else printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5; vector<int> adj[maxn]; int dist[maxn]; bool vis[maxn]; int n, k; void add(int a, int b) { adj[a].push_back(b); adj[b].push_back(a); } void bfs(int v) { queue<int> verts; verts.push(v); dist[v] = 0; while (!verts.empty()) { v = verts.front(); verts.pop(); for (int u : adj[v]) { int agua; if (u < n) agua = u + 1; else agua = u + 1 - n; if (dist[u] > dist[v] + 1 && dist[v] + 1 < agua) { dist[u] = dist[v] + 1; verts.push(u); } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); memset(dist, 0x3f, sizeof dist); cin >> n >> k; string s1, s2; cin >> s1 >> s2; for (int i = 0; i < n; i++) { if (s1[i] == 'X') continue; if (i > 0) { if (s1[i - 1] == '-') add(i, i - 1); } if (i < n - 1) { if (s1[i + 1] == '-') add(i, i + 1); } if (i + k < n) { if (s2[i + k] == '-') add(i, n + i + k); } } for (int i = 0; i < n; i++) { if (s2[i] == 'X') continue; if (i > 0) { if (s2[i - 1] == '-') add(n + i, n + i - 1); } if (i < n - 1) { if (s2[i + 1] == '-') add(n + i, n + i + 1); } if (i + k < n) { if (s1[i + k] == '-') add(n + i, i + k); } } bfs(0); bool can = 0; for (int i = n - 1; i >= n - k; i--) { if (dist[i] < i + 1) can = 1; } for (int i = 2 * n - 1; i >= 2 * n - k; i--) { if (dist[i] < i - n + 1) can = 1; } if (can) cout << "YES"; else cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, m, i, j, x, y, z, used[100005][5]; string st[5]; queue<int> step, qx, qy; int main() { cin >> n >> m >> st[0] >> st[1]; for (i = 1; i <= m; i++) { for (j = 0; j <= 1; j++) { st[j] += "-"; } } qx.push(1); 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 - 1] == 'X') continue; if (x >= n) { cout << "YES"; return 0; } if (x <= z) continue; if (used[x][y]) 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; const int MAXN = 100 * 100 * 10 * 3 + 10; int mn[MAXN][2]; queue<int> q; int main() { int n, m; cin >> n >> m; string s[2]; for (int i = 0; i < MAXN; i++) mn[i][0] = mn[i][1] = -1; cin >> s[0] >> s[1]; mn[0][0] = 0; q.push(0); q.push(0); int t = 0; int x, y, k; bool flag = false; while (q.size()) { x = q.front(); q.pop(); y = q.front(); q.pop(); if (x + 1 > n or x + m > n) { flag = true; break; } if (s[y][x + 1] != 'X' and x + 1 <= m + n and mn[x + 1][y] == -1) { q.push(x + 1), q.push(y); mn[x + 1][y] = k + 1; } if (x - 1 >= 0 and s[y][x - 1] != 'X' and mn[x - 1][y] == -1 and x - 1 > mn[x][y]) { q.push(x - 1), q.push(y); mn[x - 1][y] = mn[x][y] + 1; } if (x + m <= n + m and s[1 - y][x + m] != 'X' and mn[x + m][1 - y] == -1) { q.push(x + m), q.push(1 - y); mn[x + m][1 - y] = mn[x][y] + 1; } if (s[1 - y][x + m] != 'X' and mn[x - m][1 - y] == -1 and x - m >= 0 and x - m > mn[x][y]) { q.push(x - m), q.push(1 - y); mn[x - m][1 - y] = mn[x][y] + 1; } } for (int i = n; i <= n + m; i++) if (mn[i][0] > 0 or mn[i][1] > 0) flag = true; if (flag == true) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int ans, n, m; struct node { int x, y; } q[500010]; int vis[500010][2]; char g[2][500010]; int bfs() { int front = 0, rear = 1; node t; t.x = 0, t.y = 0; memset(vis, -1, sizeof(vis)); q[front] = t; vis[t.x][t.y] = 0; while (front < rear) { node u = q[front++]; int x = u.x; int y = u.y; if (x + m >= n) return 1; if (g[y][x + 1] == '-') { if (vis[x + 1][y] < 0) { vis[x + 1][y] = vis[x][y] + 1; if (vis[x + 1][y] <= x + 1) { node t; t.x = x + 1; t.y = y; q[rear++] = t; } } } if (x > 0 && g[y][x - 1] == '-') { if (vis[x - 1][y] < 0) { vis[x - 1][y] = vis[x][y] + 1; if (vis[x - 1][y] <= x - 1) { node t; t.x = x - 1; t.y = y; q[rear++] = t; } } } if (g[!y][x + m] == '-') { if (vis[x + m][!y] < 0) { vis[x + m][!y] = vis[x][y] + 1; if (vis[x + m][!y] <= x + m) { node t; t.x = x + m; t.y = !y; q[rear++] = t; } } } } return 0; } int main() { int i, j; while (scanf("%d%d", &n, &m) != EOF) { scanf("%s%s", g[0], g[1]); int ans = bfs(); if (ans) printf("YES\n"); else printf("NO\n"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100; int n, k, dist[2][MAXN]; char c[2][MAXN]; queue<pair<int, int> > q; int main() { scanf("%d %d", &n, &k); for (int i = 0; i < 2; i++) { scanf("%s", c[i]); for (int j = 0; j < n; j++) { dist[i][j] = -1; } } dist[0][0] = 0; q.push({0, 0}); while (!q.empty()) { int vx = q.front().first; int vy = q.front().second; int d = dist[vx][vy]; q.pop(); if (vy + k >= n) { puts("YES"); exit(0); } if (vy > 0 && d + 1 <= vy - 1 && c[vx][vy - 1] != 'X' && dist[vx][vy - 1] == -1) { dist[vx][vy - 1] = d + 1; q.push({vx, vy - 1}); } if (c[vx][vy + 1] != 'X' && dist[vx][vy + 1] == -1) { dist[vx][vy + 1] = d + 1; q.push({vx, vy + 1}); } if (c[1 - vx][vy + k] != 'X' && dist[1 - vx][vy + k] == -1) { dist[1 - vx][vy + k] = d + 1; q.push({1 - vx, vy + k}); } } puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; const double pi = acos(-1.0); const long long int inf = 0x3f3f3f3f3f3f3f3f; const long long int mod = 998244353; bool isPowerOfTwo(int x) { return x && (!(x & (x - 1))); } void fast() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } string s[2]; int n, k, h; bool a[2][100005]; void dfs(int x, int y) { if (s[x][y] != '-' || a[x][y] == true || y < h) return; if (y + k >= n) { cout << "YES"; exit(0); } h++; a[x][y] = true; dfs(1 - x, y + k); dfs(x, y + 1); dfs(x, y - 1); h--; } int main() { cin >> n >> k >> s[0] >> s[1]; dfs(0, 0); cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 200010, INF = 1000000000; int n, k, Time[N][3]; char s[3][N]; bool vis[N][3]; bool bfs() { for (int i = 0; i <= n + k; i++) Time[i][0] = Time[i][1] = INF; queue<pair<int, int> > Q; Q.push(make_pair(0, 0)), Time[0][0] = 0; while (Q.size()) { int pos = Q.front().first; bool p = Q.front().second; Q.pop(); if (Time[pos][p] > pos || vis[pos][p]) continue; vis[pos][p] = 1; if (pos >= n - 1) return true; if (Time[pos][p] + 1 < Time[pos + 1][p] && (s[p][pos + 1] != 'X' || pos + 1 > n - 1)) Q.push(make_pair(pos + 1, p)), Time[pos + 1][p] = Time[pos][p] + 1; if (Time[pos][p] + 1 < Time[pos - 1][p] && (s[p][pos - 1] != 'X' || pos - 1 > n - 1) && pos - 1 >= 0) Q.push(make_pair(pos - 1, p)), Time[pos - 1][p] = Time[pos][p] + 1; if (Time[pos][p] + 1 < Time[pos + k][!p] && (s[!p][pos + k] != 'X' || pos + k > n - 1)) Q.push(make_pair(pos + k, !p)), Time[pos + k][!p] = Time[pos][p] + 1; } return false; } int main() { scanf("%d %d %s %s", &n, &k, s[0], s[1]); if (bfs()) printf("YES"); else printf("NO"); return 0; }
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) { 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; const int MaxN = 300005; int a[MaxN][4]; bool mark[MaxN][4]; int n, k; int x[MaxN * 5], y[MaxN * 5]; char s[MaxN]; bool good(int x, int y) { return 1 <= x && 1 <= y && y <= 2; } int main() { scanf("%d%d", &n, &k); int di[] = {1, -1, k, k}; int dj[] = {0, 0, 1, -1}; memset(a, 0, sizeof(a)); for (int i = 1; i <= 2; i++) { scanf("%s", s); for (int j = 0; j < n; j++) { a[j + 1][i] = ('X' == s[j]); } } memset(mark, false, sizeof(mark)); int i = 1, j = 1; x[1] = 1; y[1] = 1; int move = 1; while (i <= j) { int z = j; for (int q = i; q <= j; q++) for (int dir = 0; dir < 4; dir++) { int ni = x[q] + di[dir]; int nj = y[q] + dj[dir]; if (good(ni, nj) && !mark[ni][nj] && !a[ni][nj] && ni > move) { mark[ni][nj] = true; z++; x[z] = ni; y[z] = nj; if (ni > n) { printf("YES\n"); return 0; } } } move++; i = j + 1; j = z; } printf("NO\n"); return 0; }
8
CPP
from collections import deque import sys n, k = map(int, sys.stdin.readline().split()) s1, s2 = sys.stdin.readline(), sys.stdin.readline() mat = [[] for _ in range(2 * 100000 + 10)] used = [-1] * (2 * 100000 + 10) for i in range(n): if i > 0 and s1[i - 1] != 'X' and s1[i] != 'X': mat[i].append(i - 1) used[i - 1] = 0 if i < n - 1 and s1[i] != 'X' and s1[i + 1] != 'X': mat[i].append(i + 1) used[i + 1] = 0 used[i] = 0 if i + k >= n: mat[i].append(2 * 100000 + 9) used[2 * 100000 + 9] = 0 elif s2[i + k] != 'X': mat[i].append(i + k + 100001) used[i + k + 100001] = 0 for i in range(n): if i > 0 and s2[i - 1] != 'X' and s2[i] != 'X': mat[i + 100001].append(i - 1 + 100001) used[i - 1 + 100001] = 0 if i < n - 1 and s2[i] != 'X' and s2[i + 1] != 'X': mat[i + 100001].append(i + 1 + 100001) used[i + 1 + 100001] = 0 used[i + 100001] = 0 if i + k >= n: mat[i + 100001].append(2 * 100000 + 9) used[2 * 100000 + 9] = 0 elif s1[i + k] != 'X': mat[i + 100001].append(i + k) used[i + k] = 0 q = deque([0]) dist = [0] * (2 * 100000 + 10) while q: u = q[0] q.popleft() used[u] = 1 for v in mat[u]: if used[v] == 0: dist[v] = dist[u] + 1 used[v] = 1 if (v <= 100000 and dist[v] <= v) or (v > 100000 and dist[v] <= v - 100001) or v == 2 * 100000 + 9: q.append(v) if used[2 * 100000 + 9] == 1: sys.stdout.write('YES') else: sys.stdout.write('NO')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int nmax = 200105; const int inf = 10000000; char c[2][nmax]; int dis[2][nmax], col[2][nmax], n; queue<pair<int, int>> qq; int check(int i, int j, int i1, int j1) { if (j1 < 0 || c[i1][j1] == 'X' || col[i1][j1] || dis[i][j] >= j1) return 0; if (j1 >= n) return 1; col[i1][j1] = 1; dis[i1][j1] = dis[i][j] + 1; qq.push(make_pair(i1, j1)); return 0; } int main() { int i, j, k, f; scanf("%d %d %s %s", &n, &k, &c[0], &c[1]); for (i = 0; i < 2; i++) for (j = 0; j <= n + k; j++) dis[i][j] = inf; dis[0][0] = 0; qq.push(make_pair(0, 0)); f = 0; while (!qq.empty()) { i = qq.front().first; j = qq.front().second; qq.pop(); if (check(i, j, 1 - i, j + k) || check(i, j, i, j + 1)) { printf("YES"); return 0; } check(i, j, i, j - 1); } printf("NO"); }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const int inf = 10000000; int n, k; char dat[2][maxn]; int dist[2][maxn]; void init() { scanf("%d%d", &n, &k); scanf("%s%s", dat[0], dat[1]); dat[0][n] = dat[1][n] = 0; } void work() { static priority_queue<pair<int, pair<int, int> > > Q; static bool used[2][maxn]; Q.push(make_pair(0, make_pair(0, 0))); int i, j; for (i = 0; i < 2; i++) for (j = 0; j <= n; j++) dist[i][j] = inf; dist[0][0] = 0; while (!Q.empty()) { i = Q.top().second.first; j = Q.top().second.second; Q.pop(); if (used[i][j]) continue; used[i][j] = true; if (dist[i][j] > j) continue; if (j >= n) break; if (dat[i][j + 1] != 'X' && dist[i][j + 1] > dist[i][j] + 1) { dist[i][j + 1] = dist[i][j] + 1; Q.push(make_pair(-dist[i][j + 1], make_pair(i, j + 1))); } if (j > 0 && dat[i][j - 1] != 'X' && dist[i][j - 1] > dist[i][j] + 1) { dist[i][j - 1] = dist[i][j] + 1; Q.push(make_pair(-dist[i][j - 1], make_pair(i, j - 1))); } if (dat[i ^ 1][min(j + k, n)] != 'X' && dist[i ^ 1][min(j + k, n)] > dist[i][j] + 1) { dist[i ^ 1][min(j + k, n)] = dist[i][j] + 1; Q.push(make_pair(-dist[i ^ 1][min(j + k, n)], make_pair(i ^ 1, min(j + k, n)))); } } if (dist[0][n] <= n || dist[1][n] <= n) printf("YES\n"); else printf("NO\n"); } int main() { init(); work(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; int n, k; bool mark[2 * maxn]; string s1, s2; vector<int> adj[2 * maxn], v; int main() { cin >> n >> k; cin >> s1 >> s2; for (int i = 0; i <= n - 1; i++) { if (s1[i] == 'X') { mark[2 * i] = 1; continue; } if (i != 0 && s1[i - 1] == '-') adj[2 * i].push_back(2 * i - 2); if (i != n - 1 && s1[i + 1] == '-') adj[2 * i].push_back(2 * i + 2); if (i + k <= n - 1 && s2[i + k] == '-') adj[2 * i].push_back(2 * (i + k) + 1); if (i + k > n - 1) adj[2 * i].push_back(2 * n); } for (int i = 0; i <= n - 1; i++) { if (s2[i] == 'X') { mark[2 * i + 1] = 1; continue; } if (i != 0 && s2[i - 1] == '-') adj[2 * i + 1].push_back(2 * i + 1 - 2); if (i != n - 1 && s2[i + 1] == '-') adj[2 * i + 1].push_back(2 * i + 1 + 2); if (i + k <= n - 1 && s1[i + k] == '-') adj[2 * i + 1].push_back(2 * (i + k)); if (i + k > n - 1) adj[2 * i + 1].push_back(2 * n); } mark[0] = 1; v.push_back(0); int cnt = 1; while (1) { vector<int> vv; for (int i = 0; i < v.size(); i++) { int u = v[i]; for (int j = 0; j < adj[u].size(); j++) if (mark[adj[u][j]] == 0 && (adj[u][j] + 2) / 2 > cnt) { mark[adj[u][j]] = 1; vv.push_back(adj[u][j]); } } v = vv; if (v.size() == 0) break; cnt++; } if (mark[2 * n]) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; char arr[2][100009]; bool solve(int wall, int h, int water) { if (arr[wall][h] != '-' || h < 0 || water > h) return false; if (h + k >= n) return true; arr[wall][h] = 'X'; if (solve(1 - wall, h + k, water + 1) || solve(wall, h + 1, water + 1) || solve(wall, h - 1, water + 1)) return true; return false; } int main() { cin >> n >> k; cin >> arr[0] >> arr[1]; cout << (solve(0, 0, 0) == true ? "YES" : "NO"); }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; char M[2][100005]; bool visited[2][100005], ok = false; void dfs(int r, int c, int s) { if (ok) return; if (s > c) return; if (c >= n) { ok = true; return; } visited[r][c] = 1; int nr = 1 - r, nc; nc = c + k; if (nc >= n || (!visited[nr][nc] && M[nr][nc] == '-')) dfs(nr, nc, s + 1); nc = c + 1; if (!visited[r][nc] && M[r][nc] == '-') dfs(r, nc, s + 1); nc = c - 1; if (c >= 0 && !visited[r][nc] && M[r][nc] == '-') dfs(r, nc, s + 1); } int main() { cin >> n >> k; memset(visited, 0, sizeof(visited)); for (int i = 0; i < n; i++) cin >> M[0][i]; for (int i = 0; i < n; i++) cin >> M[1][i]; dfs(0, 0, 0); if (ok) cout << "YES" << endl; else cout << "NO" << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, w; string str[2]; int vis[2][1000005]; int dfs(int x, int y) { if (y < 0) return 0; if (y >= n) return 1; if (str[x][y] == 'X' || y < w || vis[x][y]) return 0; vis[x][y] = 1; w++; int flag = 0; if (dfs(x, y - 1)) flag = 1; if (dfs(1 - x, y + k)) flag = 1; if (dfs(x, y + 1)) flag = 1; w--; return flag; } int main() { scanf("%d %d", &n, &k); cin >> str[0]; cin >> str[1]; w = 0; if (dfs(0, 0)) puts("YES"); else puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int t1[maxn], t2[maxn]; int q[maxn * 2], tail, cur, n, k; char s1[maxn], s2[maxn]; int bfs() { memset(t1, 0xff, sizeof(t1)); memset(t2, 0xff, sizeof(t2)); q[1] = 1 * maxn + 1, cur = 0, tail = 1, t1[1] = 0; while (cur < tail) { int now = q[++cur]; int t = now / maxn; int r = now % maxn; if (r + k > n) return 1; if (t == 1) { if (r - t1[r] > 2 && r > 1 && t1[r - 1] == -1 && s1[r - 1] == '-') { q[++tail] = 1 * maxn + (r - 1); t1[r - 1] = t1[r] + 1; } if (r < n && t1[r + 1] == -1 && s1[r + 1] == '-') { q[++tail] = 1 * maxn + (r + 1); t1[r + 1] = t1[r] + 1; } if (r + k <= n && t2[r + k] == -1 && s2[r + k] == '-') { q[++tail] = 2 * maxn + (r + k); t2[r + k] = t1[r] + 1; } } else { if (r - t2[r] > 2 && r > 1 && t2[r - 1] == -1 && s2[r - 1] == '-') { q[++tail] = 2 * maxn + (r - 1); t2[r - 1] = t2[r] + 1; } if (r < n && t2[r + 1] == -1 && s2[r + 1] == '-') { q[++tail] = 2 * maxn + (r + 1); t2[r + 1] = t2[r] + 1; } if (r + k <= n && t1[r + k] == -1 && s1[r + k] == '-') { q[++tail] = 1 * maxn + (r + k); t1[r + k] = t2[r] + 1; } } } return 0; } int main() { while (scanf("%d %d", &n, &k) != EOF) { scanf("%s", s1 + 1); scanf("%s", s2 + 1); if (bfs()) puts("YES"); else puts("NO"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; vector<vector<int>> s(2, vector<int>(100005, 2e9)); queue<pair<pair<int, int>, int>> q; int main() { int n, k, i, a, b, c; char sl[100005], sr[100005]; scanf("%d %d %s %s", &n, &k, sl, sr); for (i = 0; i < n; i++) { if (sl[i] == 'X') s[0][i] = -1; if (sr[i] == 'X') s[1][i] = -1; } q.push({{0, 0}, 0}); while (!q.empty()) { a = q.front().first.first; b = q.front().first.second; c = q.front().second; q.pop(); if (c > b || (s[a][b] <= c)) continue; if (b >= n - k) { printf("YES"); return 0; } s[a][b] = c; if (b - 1 >= 0 && s[a][b - 1] > c + 1) q.push({{a, b - 1}, c + 1}); if (s[a][b + 1] > c + 1) q.push({{a, b + 1}, c + 1}); if (s[1 - a][b + k] > c + 1) q.push({{1 - a, b + k}, c + 1}); } printf("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; char A[10][200100]; int len; int vis[10][200100]; int dfs(int pos, int x) { if (x > n) return 1; if (x < len || vis[pos][x] || A[pos][x] == 'X') return 0; len++; vis[pos][x] = 1; int flag = dfs(pos, x - 1) || dfs(1 - pos, x + k) || dfs(pos, x + 1); len--; return flag; } int main() { while (scanf("%d%d", &n, &k) != EOF) { memset(vis, 0, sizeof vis); len = 1; scanf("%s %s", A[0] + 1, A[1] + 1); if (dfs(0, 1)) puts("YES"); else puts("NO"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[2][100005]; int ok[2][100005]; queue<int> Q; int ans = 0, n, k; void chk(int x, int y, int t) { if (y < t || y <= 0) return; if (y > n) { ans = 1; return; } if (ok[x][y] || a[x][y] == 'X') return; ok[x][y] = t; Q.push(x); Q.push(y); } int main(void) { scanf("%d%d", &n, &k); scanf("%s%s", a[0] + 1, a[1] + 1); Q.push(0); Q.push(1); ok[0][1] = 1; while (!ans && !Q.empty()) { int x = Q.front(); Q.pop(); int y = Q.front(); Q.pop(); int t = ok[x][y]; chk(x, y - 1, t + 1); chk(x, y + 1, t + 1); chk(!x, y + k, t + 1); } puts(ans ? "YES" : "NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[2][1000010]; int vis[2][1000010], qx[2000020], qy[2000020], fi, fj, x, y, k, n; int main() { cin >> n >> k; cin >> a[0]; cin >> a[1]; vis[0][0] = 1; for (fj = 1; fi < fj;) { x = qx[fi]; y = qy[fi++]; if (y >= n - k) { cout << "YES"; return 0; } if (y > 0 && a[x][y - 1] == '-' && vis[x][y - 1] == 0 && y > vis[x][y]) vis[x][y - 1] = vis[x][y] + 1, qx[fj] = x, qy[fj++] = y - 1; if (a[x][y + 1] == '-' && vis[x][y + 1] == 0) vis[x][y + 1] = vis[x][y] + 1, qx[fj] = x, qy[fj++] = y + 1; if (a[1 - x][y + k] == '-' && vis[1 - x][y + k] == 0) vis[1 - x][y + k] = vis[x][y] + 1, qx[fj] = 1 - x, qy[fj++] = y + k; } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[5][100005]; queue<pair<int, int> > que; int n, k, water[5][100005]; int main() { cin >> n >> k; getchar(); for (int t = 1; t <= 2; t++) for (int i = 1; i <= n; i++) cin >> a[t][i]; que.push(make_pair(1, 1)); water[1][1] = 1; while (!que.empty()) { int x = que.front().first; int y = que.front().second; que.pop(); if (y + 1 > n || y + k > n) { cout << "YES"; return 0; } if (a[x][y + 1] == '-' && !water[x][y + 1]) { que.push(make_pair(x, y + 1)); water[x][y + 1] = water[x][y] + 1; } if (a[3 - x][y + k] == '-' && !water[3 - x][y + k]) { que.push(make_pair(3 - x, y + k)); water[3 - x][y + k] = water[x][y] + 1; } if (a[x][y - 1] == '-' && y - 1 > water[x][y] && !water[x][y - 1]) { que.push(make_pair(x, y - 1)); water[x][y - 1] = water[x][y] + 1; } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3, "Ofast", "inline") using namespace std; const int mod = 10007; const int M = 2 * 1e2 + 5; const int N = 1e5 + 5; int n, m; char s[3][N]; int v[3][N]; bool dfs(int x, int y, int h) { if (y > n) return 1; if (v[x][y] || s[x][y] == 'X' || y < h) return 0; v[x][y] = 1; if (dfs(3 - x, y + m, h + 1) | dfs(x, y + 1, h + 1) | dfs(x, y - 1, h + 1)) return 1; return 0; } signed main() { cin >> n >> m; for (int i = 1; i <= 2; i++) scanf("%s", s[i] + 1); if (dfs(1, 1, 1)) printf("YES\n"); else printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; [[maybe_unused]] const int MAX = INT32_MAX, MIN = -INT32_MAX, maxInd = 1E6 + 1E5 + 1; [[maybe_unused]] bool directed = true, end_now = false; bool possible = false; int n, k; struct node_data_structure { int pre = 0, post = 0; bool visited = false, processed = false; } nodes[maxInd]; vector<int> graph[maxInd]; void insert_graph(int pre, int post) { graph[pre].push_back(post); nodes[pre].post++; nodes[post].pre++; if (!directed) { graph[post].push_back(pre); nodes[post].post++; nodes[pre].pre++; } } [[maybe_unused]] void undoVisit() { for (auto &i : nodes) { i.visited = false; i.processed = false; } end_now = false; } void dfs(int u, int ac = 1) { if (end_now) return; if (u > 1E6) { if (u - 1E6 < ac) return; if (u - 1E6 + k > n || u - 1E6 == n) { end_now = true; possible = true; return; } } else { if (u < ac) return; if (u + k > n || u == n) { end_now = true; possible = true; return; } } nodes[u].visited = true; for (int v : graph[u]) { if (!nodes[v].visited) { dfs(v, ac + 1); } } nodes[u].processed = true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); string s1, s2; cin >> n >> k >> s1 >> s2; for (int i = 1; i < n; i++) { if (i + k <= n) { if (s1[i - 1] == '-' && s2[i - 1 + k] == '-') { insert_graph(i, 1E6 + i + k); } if (s2[i - 1] == '-' && s1[i - 1 + k] == '-') { insert_graph(1E6 + i, i + k); } } if (s1[i] == '-' && s1[i - 1] == '-') { insert_graph(i, i + 1); insert_graph(i + 1, i); } if (s2[i] == '-' && s2[i - 1] == '-') { insert_graph(1E6 + i, 1E6 + i + 1); insert_graph(1E6 + i + 1, 1E6 + i); } } dfs(1); possible ? cout << "YES" << endl : cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int Q[1000000][2], L, R; int f[101000][2]; int N, K, i, ans; char mat[2][101000]; bool mark[101000][2]; void push(int pos, int st, int val) { if (pos >= N) { ans = 1; return; } if (mat[st][pos] == 'X') return; if (f[pos][st] == -1 || val < f[pos][st]) { f[pos][st] = val; if (!mark[pos][st]) { mark[pos][st] = 1; Q[R][0] = pos, Q[R][1] = st; R++; } } } int main() { while (scanf("%d%d", &N, &K) != EOF) { scanf("%s%s", mat[0], mat[1]); L = 0, R = 0; ans = 0; for (i = 0; i < N; i++) f[i][0] = f[i][1] = -1, mark[i][0] = mark[i][1] = 0; push(0, 0, 0); while (L < R && !ans) { int pos = Q[L][0], st = Q[L][1]; mark[pos][st] = 0; L++; if (pos < f[pos][st] || mat[st][pos] == 'X') continue; push(pos + 1, st, f[pos][st] + 1); if (pos >= 1) push(pos - 1, st, f[pos][st] + 1); push(pos + K, st ^ 1, f[pos][st] + 1); } printf(ans ? "YES\n" : "NO\n"); } }
8
CPP
#include <bits/stdc++.h> using namespace std; const double pi = 3.141592653589793; const double tau = 6.283185307179586; const double epsilon = 1e-6; const int INF = 1 << 26; const int MAXN = 100005; int vis[MAXN * 2][2]; int graph[MAXN * 2][2]; int n, k; int convert(char a) { if (a == '-') return 1; if (a == 'X') return 0; return -1; } int ss(int a) { return a == 0 ? 1 : 0; } int bfs(int x, int y) { queue<pair<int, pair<int, int> > > q; q.push(make_pair(0, make_pair(x, y))); int a, b, c; while (q.size()) { pair<int, pair<int, int> > cur = q.front(); q.pop(); a = cur.second.first; b = cur.second.second; c = cur.first; if (c > a) continue; if (a + k >= n) { cout << "YES" << endl; return 1; } if (vis[a][b] < c) continue; if (graph[a + k][ss(b)] == 1) { if (a >= n) { cout << "YES" << endl; return 1; } if (vis[a + k][ss(b)] == INF) { vis[a + k][ss(b)] = c + 1; q.push(make_pair(c + 1, make_pair(a + k, ss(b)))); } } if (graph[a + 1][b] == 1) { if (a >= n) { cout << "YES" << endl; return 1; } if (vis[a + 1][b] == INF) { vis[a + 1][b] = c + 1; q.push(make_pair(c + 1, make_pair(a + 1, b))); } } if (graph[a - 1][b] == 1) { if (a >= n) { cout << "YES" << endl; return 1; } if (vis[a - 1][b] == INF) { vis[a - 1][b] = c + 1; q.push(make_pair(c + 1, make_pair(a - 1, b))); } } } return 0; } int main(int argc, char** argv) { scanf("%d %d\n", &n, &k); for (int i = 0; i < 2 * n; i++) { char a; scanf("%c", &a); if (a == '\n') { i--; continue; } graph[i % n][i / n] = convert(a); } for (int i = 0; i < 2; i++) for (int j = 0; j < 2 * n; j++) { vis[j][i] = INF; } if (!bfs(0, 0)) cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> const double pi = acos(-1.0), eps = 1e-9; const int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1}; const int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1}; const int MO = (int)(1e9 + 7); using namespace std; bool vis[3][2000001]; int time1[3][2000001]; char s[3][2000001]; int n, k; queue<pair<int, int> > Q; void bfs(int x, int y) { Q.push(make_pair(x, y)); vis[x][y] = true; while (!Q.empty()) { pair<int, int> now = Q.front(); int x = now.first, y = now.second, nt = ::time1[x][y]; Q.pop(); if (y > 1 && nt + 1 < y - 1 && !vis[x][y - 1] && s[x][y - 1] == '-') { time1[x][y - 1] = time1[x][y] + 1; vis[x][y - 1] = true; Q.push(make_pair(x, y - 1)); } if (y < n && nt + 1 < y + 1 && !vis[x][y + 1] && s[x][y + 1] == '-') { time1[x][y + 1] = time1[x][y] + 1; vis[x][y + 1] = true; Q.push(make_pair(x, y + 1)); } if (y + k <= n && nt + 1 < y + k && !vis[x % 2 + 1][y + k] && s[x % 2 + 1][y + k] == '-') { time1[x % 2 + 1][y + k] = time1[x][y] + 1; vis[x % 2 + 1][y + k] = true; Q.push(make_pair(x % 2 + 1, y + k)); } } } int main() { scanf("%d%d", &n, &k); scanf(" "); gets(s[1] + 1); gets(s[2] + 1); bfs(1, 1); bool ok = false; for (int i = 1; i <= 2; i++) for (int j = 1; j <= n; j++) if (vis[i][j] && j + k > n) ok = true; puts(ok ? "YES" : "NO"); }
8
CPP
#include <bits/stdc++.h> using namespace std; int d[200005][2]; int main() { srand((unsigned int)time(NULL)); int n, k; cin >> n >> k; string le[2]; cin >> le[0] >> le[1]; for (int i = 0; i < 2; i++) while (le[i].size() < 200000) le[i].push_back('-'); for (int i = 1; i <= 200000; i++) { for (int j = 0; j < 2; j++) { d[i][j] = 1000000000; } } priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, greater<pair<int, pair<int, int> > > > que; d[1][0] = 1; que.push(make_pair(1, make_pair(1, 0))); while (!que.empty()) { pair<int, pair<int, int> > p = que.top(); que.pop(); if (p.first != d[p.second.first][p.second.second]) continue; int time = p.first; int height = p.second.first; int side = p.second.second; if (time + 1 <= height - 1 && 1 <= height - 1 && le[side][height - 2] == '-' && d[height - 1][side] > time + 1) { d[height - 1][side] = time + 1; if (height - 1 <= n) que.push(make_pair(time + 1, make_pair(height - 1, side))); } if (time + 1 <= height + 1 && 1 <= height + 1 && le[side][height] == '-' && d[height + 1][side] > time + 1) { d[height + 1][side] = time + 1; if (height + 1 <= n) que.push(make_pair(time + 1, make_pair(height + 1, side))); } if (time + 1 <= height + k && 1 <= height + k && le[1 - side][height + k - 1] == '-' && d[height + k][1 - side] > time + 1) { d[height + k][1 - side] = time + 1; if (height + k <= n) que.push(make_pair(time + 1, make_pair(height + k, 1 - side))); } } for (int i = n + 1; i <= 200000; i++) for (int j = 0; j < 2; j++) if (d[i][j] != 1000000000) { cout << "YES" << endl; return 0; } cout << "NO" << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; struct P { int l, s; P(int s, int l) : s(s), l(l) {} P() {} }; char s[2][MAXN]; int dp[2][MAXN]; int n; int ans = false; queue<P> q; void gao(int S, int l, int t) { if (l >= n) { ans = true; return; } if (s[S][l] == '-' && dp[S][l] == -1) { q.push(P(S, l)); dp[S][l] = t + 1; } } int main() { int i, j, k; scanf("%d%d", &n, &k); scanf("%s%s", s[0], s[1]); memset(dp, -1, sizeof(dp)); q.push(P(0, 0)); dp[0][0] = 0; while (!q.empty() && !ans) { P u = q.front(); int t = dp[u.s][u.l]; q.pop(); gao(u.s, u.l + 1, t); if (u.l - 1 > t) { gao(u.s, u.l - 1, t); } gao(1 - u.s, u.l + k, t); } puts(ans ? "YES" : "NO"); }
8
CPP
#include <bits/stdc++.h> using namespace std; inline int min(int a, int b) { return a < b ? a : b; } char s[2][100005]; int dp[2][100005]; bool gao(int n, int k) { for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { dp[i][j] = (1 << 30); } } queue<pair<int, int> > que; que.push(pair<int, int>(0, 0)); dp[0][0] = 0; while (que.empty() == false) { int i = que.front().first; int j = que.front().second; int t = dp[i][j]; que.pop(); if (j + 1 >= n || j + k >= n) { return true; } if (s[i][j + 1] != 'X') { if (t + 1 < dp[i][j + 1]) { dp[i][j + 1] = t + 1; que.push(pair<int, int>(i, j + 1)); } } if (j > 0 && s[i][j - 1] != 'X' && j - 1 >= t + 1) { if (t + 1 < dp[i][j - 1]) { dp[i][j - 1] = t + 1; que.push(pair<int, int>(i, j - 1)); } } if (s[1 - i][j + k] != 'X') { if (t + 1 < dp[1 - i][j + k]) { dp[1 - i][j + k] = t + 1; que.push(pair<int, int>(1 - i, j + k)); } } } return false; } int main() { int n, k; while (scanf("%d %d", &n, &k) != EOF) { scanf("%s", s[0]); scanf("%s", s[1]); bool ans = gao(n, k); puts(ans ? "YES" : "NO"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; string s[2]; int timer[300100]; int used[300100]; int d[300100]; int q[300100]; vector<int> g[300100]; int get(int x, int y) { return y * 2 + x + 1; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; cin >> s[0] >> s[1]; int out = n * 2 + 1; for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { int x = get(j, i); timer[x] = i; if (s[j][i] != 'X') { int to = get(j ^ 1, i + k); if (to >= out) to = out; if (to == out || i + k < n && s[j ^ 1][i + k] != 'X') g[x].push_back(to); to = get(j, i + 1); if (to >= out) to = out; if (to == out || i + 1 < n && s[j][i + 1] != 'X') g[x].push_back(to); to = get(j, i - 1); if (to >= out) to = out; if (to == out || i && s[j][i - 1] != 'X') g[x].push_back(to); } } } timer[out] = n; int qs = 0, qt = 0; q[qt++] = 1; d[1] = 0; used[1] = 1; while (qs < qt) { int v = q[qs++]; for (int to : g[v]) { if (!used[to]) { used[to] = 1; d[to] = d[v] + 1; if (d[to] <= timer[to]) q[qt++] = to; } } } if (used[out] && d[out] <= timer[out]) cout << "YES"; else cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; int sol = 1 << 30; string st[2]; queue<pair<int, int> > bfs; int dist[2][100100]; int main() { cin.sync_with_stdio(false); cin >> n >> k; cin >> st[0] >> st[1]; for (int i = 0; i < n; i++) { dist[0][i] = dist[1][i] = 1 << 30; } dist[0][0] = 0; bfs.push(make_pair(0, 0)); while (!bfs.empty()) { pair<int, int> nd = bfs.front(); bfs.pop(); int di = dist[nd.first][nd.second]; if (nd.second > 0) { if (di + 1 <= nd.second - 1) { if (dist[nd.first][nd.second - 1] == 1 << 30) { if (st[nd.first][nd.second - 1] == '-') { dist[nd.first][nd.second - 1] = di + 1; pair<int, int> ch = nd; ch.second--; bfs.push(ch); } } } } if (nd.second == n - 1) { sol = min(sol, di + 1); } else { if (dist[nd.first][nd.second + 1] == 1 << 30) { if (st[nd.first][nd.second + 1] == '-') { dist[nd.first][nd.second + 1] = di + 1; pair<int, int> ch = nd; ch.second++; bfs.push(ch); } } } if (nd.second + k >= n) { sol = min(sol, di + 1); } else { if (dist[1 - nd.first][nd.second + k] == 1 << 30) { if (st[1 - nd.first][nd.second + k] == '-') { dist[1 - nd.first][nd.second + k] = di + 1; pair<int, int> ch = nd; ch.first = 1 - ch.first; ch.second += k; bfs.push(ch); } } } } if (sol == 1 << 30) { cout << "NO" << endl; } else { cout << "YES" << endl; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 10; int n, k, dis[2][maxN]; bool adj[2][maxN], mark[2][maxN]; queue<pair<int, int> > q; bool bfs(pair<int, int> st) { dis[st.first][st.second] = 1; q.push(st); mark[st.first][st.second] = true; while (!q.empty()) { pair<int, int> cur = q.front(); q.pop(); int v = cur.first, u = cur.second; if (dis[v][u] - 1 >= u) continue; if (cur.second >= n || cur.second + k > n) return true; if (adj[v][u + 1] && !mark[v][u + 1]) { dis[v][u + 1] = dis[v][u] + 1; q.push(make_pair(v, u + 1)); mark[v][u + 1] = true; } if (u - 1 >= 1 && adj[v][u - 1] && !mark[v][u - 1]) { dis[v][u - 1] = dis[v][u] + 1; q.push(make_pair(v, u - 1)); mark[v][u - 1] = true; } if (adj[1 - v][u + k] && !mark[1 - v][u + k]) { dis[1 - v][u + k] = dis[v][u] + 1; q.push(make_pair(1 - v, u + k)); mark[1 - v][u + k] = true; } } 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; j++) { char c; cin >> c; if (c == '-') adj[i][j] = 1; } if (bfs(make_pair(0, 1))) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; char l[100005], r[100005]; bool vis_left[100005], vis_right[100005]; bool bfs(int u, int cw, bool lf) { queue<pair<int, pair<int, bool> > > q; q.push(make_pair(u, make_pair(cw, lf))); vis_left[1] = 1; while (!q.empty()) { int x = q.front().first; int y = q.front().second.first; bool f = q.front().second.second; q.pop(); if (x + k > n || x + 1 > n) { return 1; } if (f) { if (!vis_right[x + 1] && r[x + 1] == '-' && x + 1 > y + 1) { q.push(make_pair(x + 1, make_pair(y + 1, f))), vis_right[x + 1] = 1; } if (x > 1 && x - 1 > y + 1 && !vis_right[x - 1] && r[x - 1] == '-') { q.push(make_pair(x - 1, make_pair(y + 1, f))), vis_right[x - 1] = 1; } if (x + k <= n && !vis_left[x + k] && l[x + k] == '-' && x + k > y + 1) { q.push(make_pair(x + k, make_pair(y + 1, 0))), vis_left[x + k] = 1; } } else { if (!vis_left[x + 1] && l[x + 1] == '-' && x + 1 > y + 1) { q.push(make_pair(x + 1, make_pair(y + 1, f))), vis_left[x + 1] = 1; } if (x > 1 && x - 1 > y + 1 && !vis_left[x - 1] && l[x - 1] == '-') { q.push(make_pair(x - 1, make_pair(y + 1, f))), vis_left[x - 1] = 1; } if (x + k <= n && !vis_right[x + k] && r[x + k] == '-' && x + k > y + 1) { q.push(make_pair(x + k, make_pair(y + 1, 1))), vis_right[x + k] = 1; } } } return 0; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> l[i]; } for (int i = 1; i <= n; ++i) { cin >> r[i]; } cout << (bfs(1, 0, 0) ? "YES" : "NO"); return 0; }
8
CPP