solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int shuiwei, n, k, flag; char mp[2][1000005]; int vis[2][1000005]; int dfs(int shu, int heng) { if (heng > n) return 1; if (heng < shuiwei || vis[shu][heng] || mp[shu][heng] == 'X') return 0; vis[shu][heng] = 1; shuiwei++; flag = dfs(shu, heng - 1) || dfs(1 - shu, heng + k) || dfs(shu, heng + 1); shuiwei--; return flag; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (cin >> n >> k) { shuiwei = 1; for (int i = 0; i < 2; i++) for (int j = 1; j <= n; j++) { cin >> mp[i][j]; } if (dfs(0, 1)) cout << "YES" << '\n'; else cout << "NO" << '\n'; } }
8
CPP
#include <bits/stdc++.h> using namespace std; string s[2]; int n, k; bool jumped[2][100020]; void dfs(int lor, int p, int t) { if (s[lor][p] == 'X' || p <= t || jumped[lor][p]) return; if (p > n - k) { printf("YES"); exit(0); } jumped[lor][p] = 1; dfs(lor, p - 1, t + 1); dfs((lor + 1) % 2, p + k, t + 1); dfs(lor, p + 1, t + 1); } int main() { cin >> n >> k >> s[0] >> s[1]; n--; dfs(0, 0, -1); printf("NO"); }
8
CPP
f = lambda: [q == '-' for q in input()] n, k = map(int, input().split()) l, r = f(), f() u, v = [0], [] def yes(d): if d > n - 1: print('YES') exit() for i in range(n): a, b = [], [] for d in u: if l[d - 1] and d - 1 > i: a.append(d - 1) l[d - 1] = 0 yes(d + 1) if l[d + 1]: a.append(d + 1) l[d + 1] = 0 yes(d + k) if r[d + k]: b.append(d + k) r[d + k] = 0 for d in v: if r[d - 1] and d - 1 > i: b.append(d - 1) r[d - 1] = 0 yes(d + 1) if r[d + 1]: b.append(d + 1) r[d + 1] = 0 yes(d + k) if l[d + k]: a.append(d + k) l[d + k] = 0 u, v = a, b print('NO')
8
PYTHON3
#include <bits/stdc++.h> const int maxn = 210000; const int maxnum = 1000000000; struct Tdl { int x, y, step; } dl[maxn + 1]; bool done[3][maxn + 1]; bool ky[3][maxn + 1]; int n, K, head, tail; char s[maxn + 1]; bool tz(int i, int j, int step) { if (j > n) return true; if (j < 1) return false; if (done[i][j]) return false; if (step >= j) return false; if (!ky[i][j]) return false; done[i][j] = true; dl[++tail].x = i; dl[tail].y = j; dl[tail].step = step; return false; } int main() { int i, j, step; scanf("%d%d", &n, &K); for (i = 1; i <= 2; ++i) { scanf("%s", s); for (j = 1; j <= n; ++j) { if (s[j - 1] == '-') ky[i][j] = true; else ky[i][j] = false; done[i][j] = false; } } head = 0; tail = 1; dl[1].x = 1; dl[1].y = 1; dl[1].step = 0; done[1][1] = true; bool success = false; while (head < tail) { ++head; i = dl[head].x; j = dl[head].y; step = dl[head].step; success = tz(i, j + 1, step + 1); success = tz(i, j - 1, step + 1); success = tz(3 - i, j + K, step + 1); if (success) break; } if (success) printf("YES\n"); else printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int N, K; char A[3][100005]; queue<pair<int, int> > qu; int H[2][100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> K; for (int i = 1; i <= N; ++i) cin >> A[0][i]; for (int i = 1; i <= N; ++i) cin >> A[1][i]; A[0][0] = 'X'; A[1][0] = 'X'; memset(H, 1, sizeof(H)); H[0][1] = 0; qu.push(pair<int, int>(0, 1)); while (qu.size()) { int w = qu.front().first; int i = qu.front().second; qu.pop(); if (H[w][i] >= i) continue; if (A[1 - w][i + K] != 'X') if (H[1 - w][i + K] > H[w][i] + 1) { H[1 - w][i + K] = H[w][i] + 1; qu.push(pair<int, int>(1 - w, i + K)); } if (A[w][i + 1] != 'X') if (H[w][i + 1] > H[w][i] + 1) { H[w][i + 1] = H[w][i] + 1; qu.push(pair<int, int>(w, i + 1)); } if (A[w][i - 1] != 'X') if (H[w][i - 1] > H[w][i] + 1) { H[w][i - 1] = H[w][i] + 1; qu.push(pair<int, int>(w, i - 1)); } if (i + 1 > N || i + K > N) { cout << "YES"; exit(0); } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int d[100005][2]; queue<int> q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, i, j, k; string s[2]; cin >> n >> k >> s[0] >> s[1]; for (i = 0; i < n; i++) for (j = 0; j < 2; j++) d[i][j] = 1e9; d[0][0] = 0; q.push(0); while (!q.empty()) { i = q.front() / 2; j = q.front() % 2; q.pop(); if (d[i][j] > i) continue; if (i > 0 && s[j][i - 1] == '-' && d[i - 1][j] > d[i][j] + 1) { d[i - 1][j] = d[i][j] + 1; q.push(i * 2 - 2 + j); } if (i < n - 1 && s[j][i + 1] == '-' && d[i + 1][j] > d[i][j] + 1) { d[i + 1][j] = d[i][j] + 1; q.push(i * 2 + 2 + j); } if (i < n - k && s[1 - j][i + k] == '-' && d[i + k][1 - j] > d[i][j] + 1) { d[i + k][1 - j] = d[i][j] + 1; q.push(i * 2 + k * 2 + 1 - j); } } for (i = max(0, n - k); i < n; i++) for (j = 0; j < 2; j++) if (d[i][j] <= i) { cout << "YES"; return 0; } cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, vis[2][100000], ans = 0; char a[2][100000]; void dfs(int i, int w, int t) { if (i >= n) { cout << "c1 " << i << " " << w << " " << t << "\n"; ans = 1; return; } if (t > i) { cout << "ct " << i << " " << w << " " << t << "\n"; return; } if (i < 0) { cout << "c2 " << i << " " << w << " " << t << "\n"; return; } if (a[w][i] == 'X') { cout << "c3 " << i << " " << w << " " << t << "\n"; return; } if (vis[w][i]) { cout << "c4 " << i << " " << w << " " << t << "\n"; return; } cout << "c5 " << i << " " << w << " " << t << "\n"; vis[w][i] = 1; dfs(i - 1, w, t + 1); dfs(i + 1, w, t + 1); dfs(i + k, 1 - w, t + 1); } struct type { int i; int w; int t; }; bool bfs() { queue<type> q; type t; t.i = 0; t.w = 0; t.t = 0; q.push(t); while (!q.empty()) { t = q.front(); q.pop(); if (t.i >= n) { ans = 1; return true; } else if (t.t > t.i) continue; else if (t.i < 0) continue; else if (a[t.w][t.i] == 'X') continue; else if (vis[t.w][t.i]) continue; vis[t.w][t.i] = 1; t.i--; t.t++; q.push(t); t.i += 2; q.push(t); t.i += k - 1; t.w = 1 - t.w; q.push(t); } return false; } int main() { int i; cin >> n >> k; cin >> a[0] >> a[1]; bfs(); if (ans) cout << "YES"; else cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main(void) { int n, k; string wall[2]; int water = 0; cin >> n >> k; cin >> wall[0] >> wall[1]; queue<pair<int, int> > q; q.push(make_pair(0, 0)); while (!q.empty()) { queue<pair<int, int> > next; do { pair<int, int> pos = q.front(); q.pop(); if (pos.second + k >= n) { cout << "YES" << endl; return 0; } if (pos.second - 1 > water && wall[pos.first][pos.second - 1] != 'X') { wall[pos.first][pos.second - 1] = 'X'; next.push(make_pair(pos.first, pos.second - 1)); } if (wall[pos.first][pos.second + 1] != 'X') { wall[pos.first][pos.second + 1] = 'X'; next.push(make_pair(pos.first, pos.second + 1)); } if (wall[1 - pos.first][pos.second + k] != 'X') { wall[1 - pos.first][pos.second + k] = 'X'; next.push(make_pair(1 - pos.first, pos.second + k)); } } while (!q.empty()); ++water; q = next; } cout << "NO" << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, k, dp[2][N * 2 + 5]; string s[2]; set<pair<int, pair<int, int> > > q; inline void update(int wall_type, int cur_dp, int cur_pos) { if (cur_pos <= cur_dp) { return; } if (cur_pos <= n && s[wall_type][cur_pos - 1] == 'X') { return; } if (cur_pos > n) { cout << "YES"; exit(0); } if (dp[wall_type][cur_pos] > cur_dp) { q.erase(make_pair(dp[wall_type][cur_pos], make_pair(wall_type, cur_pos))); dp[wall_type][cur_pos] = cur_dp; q.insert(make_pair(dp[wall_type][cur_pos], make_pair(wall_type, cur_pos))); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> s[0] >> s[1]; for (int i = 0; i <= 100000; i++) { dp[0][i] = dp[1][i] = 1e9; } dp[0][1] = 0; q.insert(make_pair(0, make_pair(0, 1))); while (!q.empty()) { pair<int, pair<int, int> > v = *q.begin(); q.erase(q.begin()); if (v.second.second > n) { return cout << "YES", 0; } update(v.second.first, dp[v.second.first][v.second.second] + 1, v.second.second + 1); update(v.second.first, dp[v.second.first][v.second.second] + 1, v.second.second - 1); update(v.second.first ^ 1, dp[v.second.first][v.second.second] + 1, v.second.second + k); } cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k; int d[3][2 * 100005]; int mark[3][2 * 100005]; char c; struct node { int x, y, d; } tmp, tmp2; queue<node> q; int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= 2; i++) for (int j = 1; j <= n; j++) { scanf(" %c", &c); if (c == '-') d[i][j] = 1; } for (int j = n + 1; j <= n + k; j++) { d[1][j] = 1; d[2][j] = 1; } tmp.x = 1; tmp.y = 1; tmp.d = 0; q.push(tmp); while (!q.empty()) { tmp = q.front(); q.pop(); if (tmp.d >= tmp.y) continue; if (tmp.y >= n) { printf("YES\n"); return 0; } if (mark[tmp.x][tmp.y]) { continue; } mark[tmp.x][tmp.y] = 1; if (tmp.y - 2 > tmp.d && d[tmp.x][tmp.y - 1]) { tmp2 = tmp; tmp2.y -= 1; tmp2.d += 1; q.push(tmp2); } if (d[tmp.x][tmp.y + 1]) { tmp2 = tmp; tmp2.y += 1; tmp2.d += 1; q.push(tmp2); } if (d[3 - tmp.x][tmp.y + k]) { tmp2 = tmp; tmp2.x = 3 - tmp.x; tmp2.y += k; tmp2.d += 1; q.push(tmp2); } } printf("NO\n"); }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; int water = 0, flag; int visited[2][1000005]; string wall[2]; void bfs(int x, int y) { flag = 0; visited[x][y] = 1; queue<pair<int, int> > q; q.push(make_pair(x, y)); while (q.size() != 0) { int z = q.size(); while (z--) { int a = q.front().first; int b = q.front().second; q.pop(); if (b + k > n || b + 1 > n) { flag = 1; return; } if (wall[a][b + 1] != 'X' && visited[a][b + 1] != 1) { visited[a][b + 1] = 1; q.push(make_pair(a, b + 1)); } if (b - 1 >= 0 && wall[a][b - 1] != 'X' && b - 1 > water && visited[a][b - 1] != 1) { visited[a][b - 1] = 1; q.push(make_pair(a, b - 1)); } if (a == 0) { if (wall[1][b + k] != 'X' && visited[1][b + k] != 1) { visited[1][b + k] = 1; q.push(make_pair(1, b + k)); } } if (a == 1) { if (wall[0][b + k] != 'X' && visited[0][b + k] != 1) { visited[0][b + k] = 1; q.push(make_pair(0, b + k)); } } } water++; } } int main() { cin >> n >> k; for (int i = 0; i < 2; i++) { for (int j = 0; j <= n; j++) { visited[i][j] = 0; } } cin >> wall[0]; cin >> wall[1]; bfs(0, 0); if (flag == 1) cout << "YES"; else cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, dx[3] = {-1, 1}, dy[] = {0, 0, 1}; string l, r; vector<vector<int> > dyn, gr; deque<pair<int, int> > qu; bool ok(int x) { return (x > 0 && x <= n + k); } void bfs() { dyn[1][0] = 0; qu.push_back(make_pair(1, 0)); while (!qu.empty()) { pair<int, int> cur = qu.front(); qu.pop_front(); for (int i = 0; i < 3; i++) { pair<int, int> nex(cur.first + dx[i], (cur.second + dy[i]) % 2); if (ok(nex.first) && gr[nex.first][nex.second] && dyn[nex.first][nex.second] == -1 && dyn[cur.first][cur.second] + 1 < nex.first) { dyn[nex.first][nex.second] = dyn[cur.first][cur.second] + 1; qu.push_back(nex); } } } } int main() { cin >> n >> k >> l >> r; dx[2] = k; dyn.resize(n + k + 1, vector<int>(2, -1)); gr.resize(n + k + 1, vector<int>(2, 1)); for (int i = 0; i < n; i++) { gr[i + 1][0] = (l[i] == '-'); gr[i + 1][1] = (r[i] == '-'); } bfs(); for (int i = n + 1; i < n + k + 1; i++) { if (dyn[i][0] != -1 || dyn[i][1] != -1) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5 + 5; const int inf = 1e8; const double PI = acos(-1.0); const long long mod = 100000007; int n, k; set<pair<int, int> > vis; struct node { int i, j, water; void inti() { i = 0; j = 0; water = -1; } }; queue<node> que; node op1(node x) { node ss = x; ss.j--; return ss; } node op2(node x) { node ss = x; ss.j++; return ss; } node op3(node x) { node ss = x; if (ss.i == 1) ss.i = 0; else ss.i = 1; ss.j += k; return ss; } node op4(node x) { node ss = x; if (ss.i == 1) ss.i = 0; else ss.i = 1; ss.j -= k; return ss; } int main() { bool flag = false; string wall[2]; cin >> n >> k; cin >> wall[0] >> wall[1]; n--; node srt; srt.inti(); vis.insert(make_pair(0, 0)); que.push(srt); while (!que.empty() && !flag) { node now = que.front(); que.pop(); node next1 = now; node next; next1.water = now.water + 1; next = op1(next1); if (!vis.count(make_pair(next.i, next.j)) && next.water < next.j) { if (next.j > n) { flag = true; break; } if (next.j >= 0 && wall[next.i][next.j] == '-') { que.push(next); vis.insert(make_pair(next.i, next.j)); } } next = op2(next1); if (!vis.count(make_pair(next.i, next.j)) && next.water < next.j) { if (next.j > n) { flag = true; break; } if (next.j >= 0 && wall[next.i][next.j] == '-') { que.push(next); vis.insert(make_pair(next.i, next.j)); } } next = op3(next1); if (!vis.count(make_pair(next.i, next.j)) && next.water < next.j) { if (next.j > n) { flag = true; break; } if (next.j >= 0 && wall[next.i][next.j] == '-') { que.push(next); vis.insert(make_pair(next.i, next.j)); } } next = op4(next1); if (!vis.count(make_pair(next.i, next.j)) && next.water < next.j) { if (next.j > n) { flag = true; break; } if (next.j >= 0 && wall[next.i][next.j] == '-') { que.push(next); vis.insert(make_pair(next.i, next.j)); } } } if (flag) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100008; int n, k; char A[maxn][2]; queue<int> loc, lev, stp; bool bk[maxn][2]; int main() { cin >> n >> k; int i, h, l, s; getchar(); for (i = 1; i <= n; i++) { A[i][0] = getchar(); } getchar(); for (i = 1; i <= n; i++) { A[i][1] = getchar(); } if (A[1][0] == '-') { loc.push(0); lev.push(1); stp.push(0); bk[1][0] = 1; } while (!lev.empty()) { l = loc.front(); loc.pop(); h = lev.front(); lev.pop(); s = stp.front(); stp.pop(); if (h + 1 > n || h + k > n) { puts("YES"); return 0; } if (bk[h + 1][l] == 0 && A[h + 1][l] == '-') { bk[h + 1][l] = 1; loc.push(l); lev.push(h + 1); stp.push(s + 1); } if (bk[h + k][!l] == 0 && A[h + k][!l] == '-') { bk[h + k][!l] = 1; loc.push(!l); lev.push(h + k); stp.push(s + 1); } if (h - 1 > s + 1 && bk[h - 1][l] == 0 && A[h - 1][l] == '-') { bk[h - 1][l] = 1; loc.push(l); lev.push(h - 1); stp.push(s + 1); } } puts("NO"); return 0; }
8
CPP
left, right = (0, 1) undiscovered = 0 processed = 1 def up(state): water = state[0] + 1 point = state[1] + 1 l_or_r = state[2] return [water, point, l_or_r] def down(state): water = state[0] + 1 point = state[1] - 1 l_or_r = state[2] return [water, point, l_or_r] def jump(state, k): water = state[0] + 1 point = state[1] + k if state[2] == left: l_or_r = right else: l_or_r = left return [water, point, l_or_r] def push_next_states(state, k, states): states[len(states):] = [up(state), down(state), jump(state, k)] return states def solve(n, k, lwall, rwall): water = -1 point = 0 l_or_r = left state = [water, point, l_or_r] table = [[undiscovered]*n, [undiscovered]*n] states = push_next_states(state, k, []) while states != []: state = states.pop() if state is None: break water = state[0] point = state[1] if n <= point: return True if water < point: l_or_r = state[2] if table[l_or_r][point] != processed: if l_or_r == left: wall = lwall else: wall = rwall if wall[point] == '-': if n <= point+k: return True push_next_states(state, k, states) table[l_or_r][point] = processed return False def main(): import sys n, k = [int(x) for x in sys.stdin.readline().split()] lwall = sys.stdin.readline().rstrip() rwall = sys.stdin.readline().rstrip() result = solve(n, k, lwall, rwall) if result: print("YES", end="") else: print("NO", end="") main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; string s[2]; int n, k; int dis[2][100105 + 5]; int go(int sx, int sy) { queue<pair<int, int> > Q; Q.push(pair<int, int>(sx, sy)); int i; for (i = 0; i <= n; i++) dis[0][i] = dis[1][i] = 500105; dis[sx][sy] = 0; while (!Q.empty()) { int x = Q.front().first; int y = Q.front().second; Q.pop(); if (y >= n - 1) return 1; if (dis[x][y] + 1 <= y + 1 && s[x][y + 1] != 'X' && dis[x][y + 1] > dis[x][y] + 1) { dis[x][y + 1] = dis[x][y] + 1; Q.push(pair<int, int>(x, y + 1)); } if (dis[x][y] + 1 <= y - 1 && y - 1 != -1 && s[x][y - 1] != 'X' && dis[x][y - 1] > dis[x][y] + 1) { dis[x][y - 1] = dis[x][y] + 1; Q.push(pair<int, int>(x, y - 1)); } if (y + k > n - 1) return 1; if (dis[x][y] + 1 <= y + k && s[!x][y + k] != 'X' && dis[!x][y + k] > dis[x][y] + 1) { dis[!x][y + k] = dis[x][y] + 1; Q.push(pair<int, int>(!x, y + k)); } } return 0; } int main() { int i, j; while (cin >> n >> k) { for (i = 0; i <= 1; i++) cin >> s[i]; if (go(0, 0)) cout << "YES\n"; else cout << "NO\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct keycompare { bool operator()(const pair<long long, long long>& v, const long long& k) { return (v.first < k); } bool operator()(const long long& k, const pair<long long, long long>& v) { return (k < v.first); } }; long long mod1 = 998244353, mod2 = 1000000007, limit = 9223372036854775807; long double pi = 3.1415926535897932; long long modpow(long long x, long long n, long long m) { if (x > m) { x %= m; } if (n == 0) return 1 % m; long long u = modpow(x, n / 2, m); u = (u * u) % m; if (n % 2 == 1) u = (u * x) % m; return u; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool isprime(long long n) { if (n == 2) return true; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } long long power(long long x, long long n) { if (n < 0) { return 0; } long long x_n = 1; for (long long i = 0; i < n; i++) { x_n *= x; } return x_n; } int dist[200005][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); for (int i = 0; i < 200005; i++) { dist[i][0] = 100000000; dist[i][1] = 100000000; } int n, k; cin >> n >> k; string l, r; cin >> l >> r; queue<pair<int, pair<int, int>>> q; q.push({0, {0, 0}}); dist[0][0] = 0; while (!q.empty()) { int d = q.front().first, h = q.front().second.first, col = q.front().second.second; q.pop(); if (d <= h && ((col == 0 && l[h] == '-') || (col == 1 && r[h] == '-'))) { if (h + k >= n) { cout << "YES"; return 0; } else { if (dist[h + 1][col] > d + 1) { dist[h + 1][col] = d + 1; q.push({d + 1, {h + 1, col}}); } if (h - 1 >= 0 && dist[h - 1][col] > d + 1) { dist[h - 1][col] = d + 1; q.push({d + 1, {h - 1, col}}); } if (dist[h + k][!col] > d + 1) { dist[h + k][!col] = d + 1; q.push({d + 1, {h + k, !col}}); } } } } cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; string s[2]; int n, k; bool jumped[2][100020]; void dfs(int lor, int p, int t) { if (s[lor][p] == 'X' || p <= t || jumped[lor][p]) return; if (p > n - k) { printf("YES"); exit(0); } jumped[lor][p] = 1; dfs((lor + 1) % 2, p + k, t + 1); dfs(lor, p + 1, t + 1); dfs(lor, p - 1, t + 1); } int main() { cin >> n >> k >> s[0] >> s[1]; n--; dfs(0, 0, -1); printf("NO"); }
8
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/stack:64000000") using namespace std; template <typename X> inline X abs(const X& a) { return a < 0 ? -a : a; } template <typename X> inline X sqr(const X& a) { return a * a; } const int INF = INT_MAX / 2; const long double EPS = 1e-9; const long double PI = 3.1415926535897932384626433832795; const int N = 200 * 1000 + 3; int d[2][N]; int n, k; string s[2]; inline bool read() { assert(cin >> n >> k); assert(cin >> s[0] >> s[1]); return true; } int used[2][N]; inline void solve() { set<pair<int, pair<int, int> > > heap; memset(d, 63, sizeof(d)); memset(used, false, sizeof(used)); d[0][0] = 0; heap.insert(make_pair((d[0][0]), (make_pair((0), (0))))); bool ans = false; while (true) { while (!heap.empty()) { pair<int, int> v = (*heap.begin()).second; if (used[v.first][v.second]) { heap.erase(heap.begin()); continue; } else break; } if (heap.empty()) break; pair<int, int> v = (*heap.begin()).second; used[v.first][v.second] = true; int dv = (*heap.begin()).first; heap.erase(heap.begin()); if (v.second >= n) { ans = true; break; } { pair<int, int> nv = v; nv.second++; if ((nv.second >= n || (nv.second >= dv + 1 && s[nv.first][nv.second] == '-')) && (d[nv.first][nv.second] > dv + 1)) { heap.erase(make_pair((d[nv.first][nv.second]), (nv))); d[nv.first][nv.second] = dv + 1; heap.insert(make_pair((d[nv.first][nv.second]), (nv))); } } { pair<int, int> nv = v; nv.second--; if ((nv.second >= 0 && (nv.second >= dv + 1 && s[nv.first][nv.second] == '-')) && (d[nv.first][nv.second] > dv + 1)) { heap.erase(make_pair((d[nv.first][nv.second]), (nv))); d[nv.first][nv.second] = dv + 1; heap.insert(make_pair((d[nv.first][nv.second]), (nv))); } } { pair<int, int> nv = v; nv.first ^= 1; nv.second += k; if ((nv.second >= n || (nv.second >= dv + 1 && s[nv.first][nv.second] == '-')) && (d[nv.first][nv.second] > dv + 1)) { heap.erase(make_pair((d[nv.first][nv.second]), (nv))); d[nv.first][nv.second] = dv + 1; heap.insert(make_pair((d[nv.first][nv.second]), (nv))); } } } puts(ans ? "YES" : "NO"); } int main() { assert(read()); solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, jump; bool success; string left_wall = ""; string right_wall = ""; bool vis_left[100100]; bool vis_right[100100]; void rec(int height, bool wall, int water_height) { if (height > n) { success = true; return; } if (height == 0) return; if (wall) { if (left_wall[height] == 'X' || vis_left[height]) return; vis_left[height] = true; } else { if (right_wall[height] == 'X' || vis_right[height]) return; vis_right[height] = true; } if (height <= water_height && height != 0) return; rec(height + jump, !wall, water_height + 1); rec(height - 1, wall, water_height + 1); rec(height + 1, wall, water_height + 1); } int main() { cin >> n >> jump; cin >> left_wall >> right_wall; left_wall.insert(0, " "); right_wall.insert(0, " "); rec(1, true, 0); if (success) cout << "YES"; else cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; string s[2]; int f[1000000][2]; bool vis[1000000][2]; bool spfa() { memset(vis, false, sizeof(vis)); for (int i = 0; i < n + 5; i++) f[i][0] = f[i][1] = -1u >> 1; f[0][0] = 0; queue<pair<int, int> > que; que.push(make_pair(0, 0)); while (!que.empty()) { pair<int, int> now = que.front(); que.pop(); int p = now.first, id = now.second; vis[p][id] = false; if (p + 1 >= n || (p + 1 < n && s[id][p + 1] != 'X' && f[p][id] + 1 <= p + 1)) { if (p + 1 >= n) return true; if (f[p + 1][id] > f[p][id] + 1) { f[p + 1][id] = f[p][id] + 1; if (!vis[p + 1][id]) { vis[p + 1][id] = true; que.push(make_pair(p + 1, id)); } } } if (p - 1 >= 0 && s[id][p - 1] != 'X' && f[p][id] + 1 <= p - 1) { if (f[p - 1][id] > f[p][id] + 1) { f[p - 1][id] = f[p][id] + 1; if (!vis[p - 1][id]) { vis[p - 1][id] = true; que.push(make_pair(p - 1, id)); } } } if (p + k >= n || (p + k < n && s[id ^ 1][p + k] != 'X' && f[p][id] + 1 <= p + k)) { if (p + k >= n) return true; if (f[p + k][id ^ 1] > f[p][id] + 1) { f[p + k][id ^ 1] = f[p][id] + 1; if (!vis[p + k][id ^ 1]) { vis[p + k][id ^ 1] = true; que.push(make_pair(p + k, id ^ 1)); } } } } return false; } int main() { cin >> n >> k; cin >> s[0]; cin >> s[1]; s[0] += "-"; s[1] += "-"; bool ans = spfa(); if (ans) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> const int inf = 1000000000; using namespace std; char sr[2][100010]; int d[200010], cur[200010], Q[200010]; vector<int> g[200010]; int N, K; bool bfs(int u) { int s = 0, t = 0; memset(d, -1, sizeof(d)); if (sr[0][0] == 'X') return false; d[u] = 1; Q[t++] = u; while (s < t) { u = Q[s++]; if ((u >> 1) + K >= N) return true; int sz = g[u].size(); for (int i = 0; i < sz; i++) { int v = g[u][i]; if (d[v] == -1) { d[v] = d[u] + 1; if (d[v] <= cur[v]) { Q[t++] = v; } } } } return false; } bool go() { for (int i = 0; i < N; i++) { for (int o = 0; o < 2; o++) { if (sr[o][i] == '-' && sr[o][i + 1] == '-') { g[i << 1 | o].push_back(i + 1 << 1 | o); g[i + 1 << 1 | o].push_back(i << 1 | o); } if (sr[o][i] == '-' && i + K < N && sr[o ^ 1][i + K] == '-') { g[i << 1 | o].push_back(i + K << 1 | (o ^ 1)); } cur[i << 1 | o] = i + 1; } } return bfs(0); } int main() { cin >> N >> K; scanf("%s%s", sr[0], sr[1]); int ans = go(); puts(ans ? "YES" : "NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; inline int tavan(int a, int n, int mod) { int p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } int n, k, d[2][(201 * 1000)]; bool mark[2][(201 * 1000)]; string s[2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> s[0] >> s[1]; for (int i = 0; i <= k + 100; i++) s[0] += '-', s[1] += '-'; queue<pair<bool, int> > q; q.push({0, 0}); while (q.size()) { bool p = q.front().first; int x = q.front().second; q.pop(); if (x > 0 && !mark[p][x - 1] && s[p][x - 1] == '-') { mark[p][x - 1] = 1; d[p][x - 1] = d[p][x] + 1; if (d[p][x - 1] <= x - 1) q.push({p, x - 1}); } if (!mark[p][x + 1] && s[p][x + 1] == '-') { mark[p][x + 1] = 1; d[p][x + 1] = d[p][x] + 1; if (x + 1 < n) q.push({p, x + 1}); } if (!mark[!p][x + k] && s[!p][x + k] == '-') { mark[!p][x + k] = 1; d[!p][x + k] = d[p][x] + 1; if (x + k < n) q.push({!p, x + k}); } } for (int i = n; i < (201 * 1000); i++) { if (mark[0][i] && d[0][i] <= i) return cout << "YES", 0; if (mark[1][i] && d[1][i] <= i) return cout << "YES", 0; } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct data { int w, p; }; data ml[3] = {{0, 1}, {1, 0}, {0, -1}}; bool bfs(data cur); char wall[2][100100]; bool colour[2][100100]; int n, x; data cur; int main() { scanf("%d %d", &n, &x); scanf(" %s %s", &wall[0][1], &wall[1][1]); cur.w = 0, cur.p = 1; ml[1].p = x; if (wall[0][1] != 'X' && bfs(cur)) printf("YES\n"); else printf("NO\n"); return 0; } bool bfs(data cur) { data tmp, level = cur; int wl = 1, i; colour[cur.w][cur.p] = true; queue<data> q; q.push(cur); while (!q.empty()) { cur = q.front(); for (i = 0; i < 3; i++) { tmp = cur; tmp.w ^= ml[i].w; tmp.p += ml[i].p; if (tmp.p > n) return true; if (tmp.p > wl && !colour[tmp.w][tmp.p] && wall[tmp.w][tmp.p] != 'X') { q.push(tmp); colour[tmp.w][tmp.p] = true; } } if (level.w == cur.w && level.p == cur.p) { wl++; level = q.back(); } q.pop(); } return false; }
8
CPP
#include <bits/stdc++.h> using namespace std; string s[2]; int n, k, dp[2][100001], ans; void bfs(int wall, int height, int seconds) { if (height >= n) ans = true; if (height >= n || s[wall][height] == 'X' || dp[wall][height] <= seconds || seconds > height) return; dp[wall][height] = seconds; bfs((wall + 1) % 2, height + k, seconds + 1); bfs(wall, height + 1, seconds + 1); bfs(wall, height - 1, seconds + 1); } int main() { for (auto &i : dp) for (auto &j : i) j = 1e9; cin >> n >> k; cin >> s[0] >> s[1]; bfs(0, 0, 0); cout << (ans ? "YES\n" : "NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char c[2][100001]; int s[100001][2], q[100001][2], si; int main() { int n, k, i, j, u = 0, x, y; cin >> n >> k >> c[0] >> c[1]; q[0][0] = 0; q[0][1] = 0; for (i = 0; i < n; i++) s[i][0] = s[i][1] = 100000000; s[0][0] = 0; si = 1; while (u < si) { x = q[u][0]; y = q[u][1]; if (x == n - 1 || x + k >= n) { cout << "YES"; return 0; } if (c[y][x + 1] != 'X' && s[x + 1][y] > s[x][y] + 1) { s[x + 1][y] = s[x][y] + 1; q[si][0] = x + 1; q[si][1] = y; si++; } if (x + k < n && c[1 - y][x + k] != 'X' && s[x + k][1 - y] > s[x][y] + 1) { s[x + k][1 - y] = s[x][y] + 1; q[si][0] = x + k; q[si][1] = 1 - y; si++; } if (x - 1 > 0 && c[y][x - 1] != 'X' && s[x][y] <= x && s[x - 1][y] > s[x][y] + 1 && s[x][y] + 1 <= x - 1) { s[x - 1][y] = s[x][y] + 1; q[si][0] = x - 1; q[si][1] = y; si++; } u++; } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> char str[2][1000000]; int d[2][1000000]; struct { int lor; int s; } queue[2000000]; int qt = 0, qh = 0; int n, exitf = 0; void add(int lor, int s, int z) { if (s >= n) { exitf = 1; return; } if (s >= 0 && str[lor][s] == '-' && !d[lor][s] && s >= z) { queue[qh].lor = lor; queue[qh].s = s; qh++; d[lor][s] = z; } return; } int main() { int k; scanf("%d %d\n%s\n%s", &n, &k, str[0], str[1]); add(0, 0, 0); while (qt < qh && !exitf) { add(queue[qt].lor, queue[qt].s + 1, d[queue[qt].lor][queue[qt].s] + 1); add(queue[qt].lor, queue[qt].s - 1, d[queue[qt].lor][queue[qt].s] + 1); add((queue[qt].lor) ? 0 : 1, queue[qt].s + k, d[queue[qt].lor][queue[qt].s] + 1); qt++; } printf("%s", ((exitf) ? "YES" : "NO")); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <class T1> inline void debug(T1 _x) { cout << _x << '\n'; } template <class T1, class T2> inline void debug(T1 _x, T2 _y) { cout << _x << ' ' << _y << '\n'; } template <class T1, class T2, class T3> inline void debug(T1 _x, T2 _y, T3 _z) { cout << _x << ' ' << _y << ' ' << _z << '\n'; } template <class T1, class T2, class T3, class T4> inline void debug(T1 _x, T2 _y, T3 _z, T4 _zz) { cout << _x << ' ' << _y << ' ' << _z << ' ' << _zz << '\n'; } template <class T1> inline void debug(T1 _array, int _size) { cout << "["; for (int i = 0; i < _size; ++i) { cout << ' ' << _array[i]; } puts(" ]"); } inline bool CI(int &_x) { return scanf("%d", &_x) == 1; } inline bool CI(int &_x, int &_y) { return CI(_x) && CI(_y); } inline bool CI(int &_x, int &_y, int &_z) { return CI(_x) && CI(_y) && CI(_z); } inline bool CI(int &_x, int &_y, int &_z, int &_zz) { return CI(_x) && CI(_y) && CI(_z) && CI(_zz); } inline void wait(double seconds) { double endtime = clock() + (seconds * CLOCKS_PER_SEC); while (clock() < endtime) { ; } } const int mxn = (int)1e5 + 20; char grid[2][mxn]; int N, K; inline void read() { scanf("%d %d\n", &N, &K); gets(grid[0]); gets(grid[1]); } int water[mxn]; int path[2][mxn]; int vis[2][mxn]; struct NODE { int at; int pos; int tim; NODE() {} NODE(int at_, int pos_, int tim_) { at = at_; pos = pos_; tim = tim_; } }; inline bool possible(int at) { if (!path[at][1]) return false; memset(vis, 0, sizeof(vis)); queue<NODE> q; q.push(NODE(at, 1, 0)); vis[at][1] = 1; while (!q.empty()) { int cur_pile = q.front().at; int cur_pos = q.front().pos; int cur_tim = q.front().tim; q.pop(); int nxt_pos = cur_pos + 1; int nxt_tim = cur_tim + 1; if (nxt_pos > N) return true; if (path[cur_pile][nxt_pos] && water[nxt_pos] > nxt_tim && !vis[cur_pile][nxt_pos]) { vis[cur_pile][nxt_pos] = 1; q.push(NODE(cur_pile, nxt_pos, nxt_tim)); } nxt_pos = cur_pos - 1; if (nxt_pos > N) return true; if (path[cur_pile][nxt_pos] && water[nxt_pos] > nxt_tim && !vis[cur_pile][nxt_pos]) { vis[cur_pile][nxt_pos] = 1; q.push(NODE(cur_pile, nxt_pos, nxt_tim)); } nxt_pos = cur_pos + K; if (nxt_pos > N) return true; if (path[cur_pile ^ 1][nxt_pos] && water[nxt_pos] > nxt_tim && !vis[cur_pile ^ 1][nxt_pos]) { vis[cur_pile ^ 1][nxt_pos] = 1; q.push(NODE(cur_pile ^ 1, nxt_pos, nxt_tim)); } } return false; } inline void proc() { memset(water, 0, sizeof(water)); for (int i = 1, j1 = N + 1; i < j1; ++i) { water[i] = water[i - 1] + 1; } memset(path, -1, sizeof(path)); path[0][0] = path[1][0] = 0; for (int i = 1, j1 = N + 1; i < j1; ++i) { if (grid[0][i - 1] == 'X') path[0][i] = 0; if (grid[1][i - 1] == 'X') path[1][i] = 0; } int f = possible(0); puts((f ? "YES" : "NO")); } int main() { int kase = 1; for (int i = 0, j1 = kase; i < j1; ++i) { read(); proc(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[100005]; char b[100005]; int vis1[100005], vis2[100005]; int valid = 0; int n, x, k, y; struct pos { int t, i, k; }; int val(struct pos cur) { if (cur.k < 0) return 0; if (cur.k >= n) { valid = 1; return 0; } if (cur.i == 0 && vis1[cur.k]) return 0; if (cur.i == 1 && vis2[cur.k]) return 0; if (cur.i == 0 && a[cur.k] == 'X') return 0; if (cur.i == 1 && b[cur.k] == 'X') return 0; if (cur.k < cur.t) return 0; if (cur.i == 0) vis1[cur.k] = 1; if (cur.i == 1) vis2[cur.k] = 1; return 1; } void pr(struct pos cur) {} int main() { scanf("%d%d", &n, &k); scanf("%s%s", &a, &b); int i; queue<struct pos> q; struct pos cur; cur.t = 0; cur.i = 0; cur.k = 0; q.push(cur); vis1[0] = 1; while (!q.empty()) { cur = q.front(); pr(cur); q.pop(); if (cur.k >= n - 1) { valid = 1; break; } if (cur.k + k > n) { valid = 1; break; } struct pos ne; ne.i = 1 - cur.i; ne.t = cur.t + 1; ne.k = cur.k + k; if (val(ne)) q.push(ne); ne.k = cur.k - k; if (val(ne)) q.push(ne); ne.i = cur.i; ne.k = cur.k + 1; if (val(ne)) q.push(ne); ne.k = cur.k - 1; if (val(ne)) q.push(ne); } if (valid) printf("YES\n"); else printf("NO\n"); }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int n, k; queue<pair<int, int>> q; vector<vector<char>> a(2, vector<char>(MAXN)); vector<vector<int>> dst(2, vector<int>(MAXN)); bool add(int x, int y, int l) { if (y > n) { return true; } else if (dst[x][y] != 0 || y + 1 <= l || a[x][y] == 'X') { return false; } dst[x][y] = l; q.push({x, y}); return false; } int main() { cin >> n >> k; for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } dst[0][0] = 0; q.push({0, 0}); while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); int l = dst[x][y] + 1; if (add(x, y + 1, l) || add(x, y - 1, l) || add(1 - x, y + k, l)) { cout << "YES"; return 0; } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pll = pair<ll, ll>; using vll = vector<ll>; using vpll = vector<pll>; void ga(ll N, int *A) { for (ll i(0); i < N; i++) cin >> A[i]; } ll n, k; string w[2]; int main(void) { ios_base::sync_with_stdio(false); cin >> n >> k; cin >> w[0] >> w[1]; queue<pll> q; q.push({0, 0}); bool ok = 0; bitset<200007> cl; while (q.size()) { pll act = q.front(); q.pop(); ll pos = act.first / 2; bool left = act.first % 2; if (pos == n - 1 || pos + k >= n) { ok = 1; break; } ll j1 = (pos + k) * 2 + (!left); if (pos + k < n && !cl[j1] && w[!left][pos + k] != 'X') { cl[j1] = 1; q.push({j1, act.second + 1}); } if (!cl[(pos + 1) * 2 + left] && w[left][pos + 1] != 'X') { ll j2 = (pos + 1) * 2 + left; cl[j2] = 1; q.push({j2, act.second + 1}); } if (pos && !cl[(pos - 1) * 2 + left] && w[left][pos - 1] != 'X' && pos - 1 >= act.second + 1) { ll j3 = (pos - 1) * 2 + left; cl[j3] = 1; q.push({j3, act.second + 1}); } } cout << (ok ? "YES" : "NO") << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100004; int v[maxn][2]; int w, n, k; char a[maxn]; char c[maxn]; int dfs(int i, int b) { if (i > n) { cout << "YES"; exit(0); } if (i < w) return 0; if (b) { if (c[i] == 'X') return 0; } else { if (a[i] == 'X') return 0; } if (v[i][b]) return 0; v[i][b] = 1; w++; dfs(i - 1, b); dfs(i + k, !b); dfs(i + 1, b); w--; } int main() { cin >> n >> k; cin >> a; cin >> c; dfs(0, 0); cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int vis[2][200001]; int main() { int n, k, f = 0; cin >> n >> k; char c[2][200001]; for (int i = 0; i <= 200000; i++) c[0][i] = c[1][i] = '!'; for (int i = 0; i < n; i++) cin >> c[0][i]; for (int i = 0; i < n; i++) cin >> c[1][i]; queue<pair<int, int> > q; if (c[0][0] == '-') q.push(make_pair(0, 0)); vis[0][0] = vis[1][0] = 1; while (!q.empty()) { int x = q.front().first, y = q.front().second; if (!vis[x][y + 1] and c[x][y + 1] == '-') { if (y >= n - 2) { cout << "YES"; return 0; } q.push(make_pair(x, y + 1)); vis[x][y + 1] = vis[x][y] + 1; } if (!vis[1 - x][y + k]) { if (y + k < n and c[1 - x][y + k] == '-') q.push(make_pair(1 - x, y + k)), vis[1 - x][y + k] = vis[x][y] + 1; if (y + k >= n) { cout << "YES"; return 0; } } if (y - 1 >= 0 and !vis[x][y - 1] and c[x][y - 1] == '-' and vis[x][y] <= y - 1) q.push(make_pair(x, y - 1)), vis[x][y - 1] = vis[x][y] + 1; q.pop(); } cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> dx = {0, 0, 1}; vector<int> dy = {1, -1, k}; vector<vector<bool> > wall(2); vector<vector<bool> > visited(2); string line; for (int i = 0; i < 2; i++) { cin >> line; for (int j = 0; j < n; j++) { if (line[j] == '-') wall[i].push_back(true); else wall[i].push_back(false); } } visited[0].resize(n); visited[1].resize(n); queue<pair<pair<int, int>, int> > q; q.push({{0, 0}, 0}); visited[0][0] = true; while (!q.empty()) { int side = q.front().first.first; int h = q.front().first.second; int d = q.front().second; q.pop(); for (int i = 0; i < 3; i++) { int ns = (side + dx[i]) % 2; int nh = h + dy[i]; if (d + 1 > nh) continue; if (nh >= n) { cout << "YES" << endl; return 0; } if (nh < 0) continue; if (!wall[ns][nh]) continue; if (visited[ns][nh]) continue; visited[ns][nh] = true; q.push({{ns, nh}, d + 1}); } } cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; queue<pair<long long, long long> > q; int main() { char ar[2][100004]; long long use[2][100004]; long long i, n, k, c, r; scanf("%lld%lld", &n, &k); for (i = 0; i < 2; i++) scanf("%s", ar[i]); memset(use, -1, sizeof(use)); use[0][0] = 0; q.push(make_pair(0, 0)); while (!q.empty()) { pair<long long, long long> cur = q.front(); q.pop(); r = cur.first; c = cur.second; if (c < use[r][c]) continue; if (c + k >= n) { printf("YES\n"); return 0; } if (c < n - 1 && use[r][c + 1] == -1 && ar[r][c + 1] == '-') { q.push(make_pair(r, c + 1)); use[r][c + 1] = use[r][c] + 1; } if (c && use[r][c - 1] == -1 && ar[r][c - 1] == '-') { q.push(make_pair(r, c - 1)); use[r][c - 1] = use[r][c] + 1; } if (ar[!r][c + k] == '-' && use[!r][c + k] == -1) { q.push(make_pair(!r, c + k)); use[!r][c + k] = use[r][c] + 1; } } printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n; int k; char lw[100001]; char rw[100001]; bool get(int l, int r, int level) { bool ret = false; if (l >= 0) { if (l >= n) { ret = true; } else { if (lw[l] != 'X' && l >= level) { lw[l] = 'X'; ++level; if (!ret) { ret = get(-1, l + k, level); } if (!ret) { ret = get(l + 1, -1, level); } if (!ret) { ret = get(l - 1, -1, level); } } } } else if (r >= 0) { if (r >= n) { ret = true; } else { if (rw[r] != 'X' && r >= level) { rw[r] = 'X'; ++level; if (!ret) { ret = get(r + k, -1, level); } if (!ret) { ret = get(-1, r + 1, level); } if (!ret) { ret = get(-1, r - 1, level); } } } } return ret; } int main() { cin >> n; cin >> k; for (int i = 0; i < n; i++) { cin >> lw[i]; } for (int i = 0; i < n; i++) { cin >> rw[i]; } bool res = get(0, -1, 0); cout << (res ? "YES" : "NO") << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline void mini(T &a, T b) { if (b < a) a = b; } template <class T> inline void maxi(T &a, T b) { if (b > a) a = b; } 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; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, m; string ss[2]; int d[2][100005]; set<pair<int, pair<int, int> > > s; int ans = 1e9; void upd(int x, int y, int z) { if (y < 1) return; if (y > n) { ans = min(ans, z); return; } if (ss[x][y] == 'X') return; if (d[x][y] < z) return; s.erase({d[x][y], {x, y}}); d[x][y] = z; s.insert({d[x][y], {x, y}}); return; } int main(int argc, const char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; cin >> ss[0] >> ss[1]; ss[0] = "#" + ss[0]; ss[1] = "#" + ss[1]; for (int i = 1; i <= n; ++i) { d[0][i] = d[1][i] = 1e9; } d[0][1] = 0; s.insert({0, {0, 1}}); while (!s.empty()) { pair<int, pair<int, int> > cur = *s.begin(); s.erase(s.begin()); if (cur.first >= cur.second.second) continue; int x = cur.second.first; int y = cur.second.second; upd(x, y - 1, cur.first + 1); upd(x, y + 1, cur.first + 1); upd(1 - x, y + m, cur.first + 1); } if (ans < 1e9) cout << "YES\n"; else cout << "NO\n"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; char ch[2][100010]; int dis[2][100010]; queue<pair<int, int> > q; bool isposs(int idx, int st, int tm) { return ch[st][idx] == '-' && tm <= idx && dis[st][idx] == -1; } int main() { memset(dis, -1, sizeof dis); cin >> n >> k; scanf("%s", ch[0]); scanf("%s", ch[1]); q.push(pair<int, int>(0, 0)); dis[0][0] = 0; string res = "NO"; while (!q.empty()) { pair<int, int> tmp = q.front(); q.pop(); int idx = tmp.first; int st = tmp.second; int tm = dis[st][idx]; if (idx + k >= n) { res = "YES"; break; } if (isposs(idx - 1, st, tm + 1)) { dis[st][idx - 1] = tm + 1; q.push(pair<int, int>(idx - 1, st)); } if (isposs(idx + 1, st, tm + 1)) { dis[st][idx + 1] = tm + 1; q.push(pair<int, int>(idx + 1, st)); } if (isposs(idx + k, 1 - st, tm + 1)) { dis[1 - st][idx + k] = tm + 1; q.push(pair<int, int>(idx + k, 1 - st)); } } cout << res << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char a[200100]; char b[200100]; int n, k; int visit[200100]; int visit2[200100]; int mn = 0; int cal(int x, int turn, int up, int lvl) { if (lvl >= x) return 0; if (x > n) { mn = 1; return 1; } if (turn == 1 && x <= 100050) { if (visit[x] == 1) return 0; } else if (x <= 100050) { if (visit2[x] == 1) return 0; } if (turn == 1 && x <= 100050) visit[x] = 1; else if (x <= 100050) visit2[x] = 1; int f1 = 0; int f2 = 0; int f3 = 0; if (turn == 1) { if (up == 1) { if (b[x + k] != 'X') { f2 = cal(x + k, 2, 3, lvl + 1); } if (a[x + 1] != 'X') { f1 = cal(x + 1, 1, 1, lvl + 1); } } else if (up == 2) { if (b[x + k] != 'X') { f2 = cal(x + k, 2, 3, lvl + 1); } if (x - 1 >= 1 && a[x - 1] != 'X') { f1 = cal(x - 1, 1, 2, lvl + 1); } } else { if (b[x + k] != 'X') { f3 = cal(x + k, 2, 3, lvl + 1); } if (a[x + 1] != 'X') { f1 = cal(x + 1, 1, 1, lvl + 1); } if (x - 1 >= 1 && a[x - 1] != 'X') { f1 = cal(x - 1, 1, 2, lvl + 1); } } } else { if (up == 1) { if (a[x + k] != 'X') { f2 = cal(x + k, 1, 3, lvl + 1); } if (b[x + 1] != 'X') { f1 = cal(x + 1, 2, 1, lvl + 1); } } else if (up == 2) { if (a[x + k] != 'X') { f2 = cal(x + k, 1, 3, lvl + 1); } if (x - 1 >= 1 && b[x - 1] != 'X') { f1 = cal(x - 1, 2, 2, lvl + 1); } } else { if (a[x + k] != 'X') { f3 = cal(x + k, 1, 3, lvl + 1); } if (b[x + 1] != 'X') { f1 = cal(x + 1, 2, 1, lvl + 1); } if (x - 1 >= 1 && b[x - 1] != 'X') { f1 = cal(x - 1, 2, 2, lvl + 1); } } } return (f1 | f2 | f3); } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; } int ans = cal(1, 1, 3, 0); if (mn) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, i, j, k, INF = 1E9, id, x, d; bool solved = false; string s[2]; cin >> n >> k >> s[0] >> s[1]; vector<vector<int>> dp(2, vector<int>(n, INF)); queue<pair<int, int>> q; q.push({0, 0}); dp[0][0] = 1; while (!q.empty()) { tie(id, x) = q.front(); q.pop(); d = dp[id][x]; if (x + 1 < d) continue; if ((x > 0) && (dp[id][x - 1] > d + 1) && (s[id][x - 1] != 'X')) { dp[id][x - 1] = d + 1; q.push({id, x - 1}); } if (x + k >= n) { solved = true; break; } if ((x < n - 1) && (dp[id][x + 1] > d + 1) && (s[id][x + 1] != 'X')) { dp[id][x + 1] = d + 1; q.push({id, x + 1}); } if ((x + k < n) && (dp[1 - id][x + k] > d + 1) && (s[1 - id][x + k] != 'X')) { dp[1 - id][x + k] = d + 1; q.push({1 - id, x + k}); } } if (solved) cout << "YES\n"; else cout << "NO\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct rec { int x, y; } p; char a[2][100010]; queue<rec> q; int d[2][100010], n, k; bool bfs() { memset(d, -1, sizeof(d)); q.push((rec){0, 1}); d[0][1] = 0; while (q.size()) { p = q.front(); q.pop(); if (p.y + k > n) return 1; if (a[p.x][p.y + 1] == '-' && d[p.x][p.y + 1] == -1 && d[p.x][p.y] + 1 < p.y + 1) q.push((rec){p.x, p.y + 1}), d[p.x][p.y + 1] = d[p.x][p.y] + 1; if (a[p.x][p.y - 1] == '-' && d[p.x][p.y - 1] == -1 && d[p.x][p.y] + 1 < p.y - 1) q.push((rec){p.x, p.y - 1}), d[p.x][p.y - 1] = d[p.x][p.y] + 1; if (a[p.x ^ 1][p.y + k] == '-' && d[p.x ^ 1][p.y + k] == -1 && d[p.x][p.y] + 1 < p.y + k) q.push((rec){p.x ^ 1, p.y + k}), d[p.x ^ 1][p.y + k] = d[p.x][p.y] + 1; } return 0; } int main() { cin >> n >> k; scanf("%s%s", a[0] + 1, a[1] + 1); if (bfs()) puts("YES"); else puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; bool vst[200000]; int dist[200000]; char str[100010]; int dat[200000]; int q[210000], l, r; int main() { scanf("%d%d", &n, &k); memset(dat, 0, sizeof(dat)); memset(vst, 0, sizeof(vst)); for (int i = 0; i < 200000; ++i) dist[i] = 1000000000; for (int i = 0; i <= 1; ++i) { scanf("%s", str); for (int j = 0; j < n; ++j) dat[(j << 1) | i] = (str[j] == '-'); } vst[0] = 1; l = 1; r = 1; q[1] = 0; dist[0] = 0; while (l <= r) { int now = q[l++], next; next = now - 2; if (((next) >= 0) && (!vst[next]) && dat[next] && (dist[now] < next / 2)) { vst[next] = 1; q[++r] = next; dist[next] = dist[now] + 1; } next = now + 2; if (((next) < (n << 1)) && (!vst[next]) && dat[next] && (dist[now] < next / 2)) { vst[next] = 1; q[++r] = next; dist[next] = dist[now] + 1; } next = (now + (k << 1)) ^ 1; if ((next >= 0) && (next < (n << 1)) && (!vst[next]) && dat[next] && (dist[now] < next / 2)) { vst[next] = 1; q[++r] = next; dist[next] = dist[now] + 1; } } int res = 1000000000; for (int i = n - k; i < n; ++i) res = min(res, min(dist[i << 1], dist[(i << 1) | 1])); printf("%s\n", (res < 1000000000) ? "YES" : "NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int toint(string s) { int n; istringstream is(s); is >> n; return n; } string tostring(int n) { stringstream is; is << n; return is.str(); } long long int modulo(int a, int b, int c) { long long x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y) % c; } y = (y * y) % c; b /= 2; } return x % c; } long long mulmod(long long a, long long b, long long c) { long long x = 0, y = a % c; while (b > 0) { if (b % 2 == 1) { x = (x + y) % c; } y = (y * 2) % c; b /= 2; } return x % c; } int query(const vector<int> &tree, int a, int b) { if (a == 0) { int sum = 0; for (; b >= 0; b = (b & (b + 1)) - 1) sum += tree[b]; return sum; } else { return query(tree, 0, b) - query(tree, 0, a - 1); } } void increase(vector<int> &tree, int k, int inc) { for (; k < (int)tree.size(); k |= k + 1) tree[k] += inc; } typedef struct nn { int wall; int h; int t; } node; queue<node> q; int best[200000][3]; int main() { node tmp; int n, k; cin >> n; cin >> k; tmp.wall = 1; tmp.h = 0; tmp.t = 0; string b[3]; cin >> b[1]; cin >> b[2]; int flag = 0; int i; for (i = 0; i < n; i++) { best[i][1] = 1000000000; best[i][2] = 1000000000; } best[0][1] = 0; q.push(tmp); node tmp1; while (!q.empty()) { tmp = q.front(); q.pop(); tmp1.t = tmp.t + 1; tmp1.wall = tmp.wall; tmp1.h = tmp.h + 1; if (tmp1.h < n && best[tmp1.h][tmp1.wall] > best[tmp.h][tmp.wall] + 1 && b[tmp1.wall][tmp1.h] != 'X' && tmp1.h >= tmp1.t) { q.push(tmp1); best[tmp1.h][tmp1.wall] = best[tmp.h][tmp.wall] + 1; } if (tmp1.h >= n && tmp1.h >= tmp1.t) { flag = 1; break; } tmp1.h = tmp.h - 1; if (tmp1.h >= 0 && tmp1.h < n && b[tmp1.wall][tmp1.h] != 'X' && best[tmp1.h][tmp1.wall] > best[tmp.h][tmp.wall] && tmp1.h >= tmp1.t) { q.push(tmp1); best[tmp1.h][tmp1.wall] = best[tmp.h][tmp.wall] + 1; } if (tmp1.h >= n && tmp1.h >= tmp1.t) { flag = 1; break; } if (tmp.wall == 1) tmp1.wall = 2; else tmp1.wall = 1; tmp1.h = tmp.h + k; if (tmp1.h < n && b[tmp1.wall][tmp1.h] != 'X' && tmp1.h >= tmp1.t && best[tmp1.h][tmp1.wall] > best[tmp.h][tmp.wall] + 1) { q.push(tmp1); best[tmp1.h][tmp1.wall] = best[tmp.h][tmp.wall] + 1; } if (tmp1.h >= n && tmp1.h >= tmp1.t) { flag = 1; break; } } if (flag == 0) printf("NO\n"); else printf("YES\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; bool dd[100009][2]; string s[2]; queue<tuple<int, int, int>> q; void xl() { cin >> n >> k; cin >> s[0] >> s[1]; s[0] = '#' + s[0]; s[1] = '#' + s[1]; q.push({1, 0, 0}); dd[1][0] = 1; while (q.size()) { auto p = q.front(); q.pop(); int x, y, z; tie(x, y, z) = p; if (x + k > n) { cout << "YES" << endl; return; } if (x > 1 && dd[x - 1][y] == 0 && s[y][x - 1] == '-') { if (x - 1 > z + 1) { dd[x - 1][y] = 1; q.push({x - 1, y, z + 1}); } } if (x < n && dd[x + 1][y] == 0 && s[y][x + 1] == '-') { dd[x + 1][y] = 1; q.push({x + 1, y, z + 1}); } if (x + k <= n && dd[x + k][1 - y] == 0 && s[1 - y][x + k] == '-') { dd[x + k][1 - y] = 1; q.push({x + k, 1 - y, z + 1}); } } cout << "NO" << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); xl(); }
8
CPP
#include <bits/stdc++.h> using namespace std; struct pos { int h, id, t; }; queue<pos> q; int n, k, pd[100005][2]; string l, r; int main() { cin >> n >> k >> l >> r; for (int i = 1; i <= n; i++) { if (l[i - 1] == 'X') pd[i][0] = 1; if (r[i - 1] == 'X') pd[i][1] = 1; } q.push((pos){1, 0, 0}), pd[1][0] = 1; while (!q.empty()) { pos t = q.front(), nw; q.pop(); nw = t; nw.h--; nw.t++; if (nw.h > 0 && !pd[nw.h][nw.id] && nw.h > nw.t) pd[nw.h][nw.id] = 1, q.push(nw); nw = t; nw.h++; nw.t++; if (nw.h > n) cout << "YES", exit(0); if (!pd[nw.h][nw.id] && nw.h > nw.t) pd[nw.h][nw.id] = 1, q.push(nw); nw = t; nw.h += k; nw.id = !nw.id; nw.t++; if (nw.h > n) cout << "YES", exit(0); if (!pd[nw.h][nw.id] && nw.h > nw.t) pd[nw.h][nw.id] = 1, q.push(nw); } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; long long n, k; string s[2]; map<pair<long long, long long>, long long> dis, vis; long long bfs() { queue<pair<long long, long long> > q; q.push(make_pair(0LL, 0LL)); dis[make_pair(0LL, 0LL)] = 0; vis[make_pair(0LL, 0LL)] = 1; pair<long long, long long> p; while (!q.empty()) { p = q.front(); q.pop(); if (!vis[make_pair(p.first, p.second + 1)] && s[p.first][p.second + 1] == '-' && dis[p] + 1 <= p.second + 1) { q.push(make_pair(p.first, p.second + 1)); vis[make_pair(p.first, p.second + 1)] = 1; dis[make_pair(p.first, p.second + 1)] = dis[p] + 1; if (p.second + 1 >= n) return cout << "YES", 0; } if (p.second - 1 >= 0 && !vis[make_pair(p.first, p.second - 1)] && s[p.first][p.second - 1] == '-' && dis[p] + 1 <= p.second - 1) { q.push(make_pair(p.first, p.second - 1)); vis[make_pair(p.first, p.second - 1)] = 1; dis[make_pair(p.first, p.second - 1)] = dis[p] + 1; if (p.second - 1 >= n) return cout << "YES", 0; } if (!vis[make_pair(abs(p.first - 1), p.second + k)] && s[abs(p.first - 1)][p.second + k] == '-' && dis[p] + 1 <= p.second + k) { q.push(make_pair(abs(p.first - 1), p.second + k)); vis[make_pair(abs(p.first - 1), p.second + k)] = 1; dis[make_pair(abs(p.first - 1), p.second + k)] = dis[p] + 1; if (p.second + k >= n) return cout << "YES", 0; } } return cout << "NO", 0; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k; cin >> s[0] >> s[1]; for (long long i = 0; i < 100002; i++) { s[0] += '-'; s[1] += '-'; } bfs(); }
8
CPP
#include <bits/stdc++.h> using namespace std; char G[2][100000]; bool visited[2][100000]; int n, k, level = 0; bool dfs(int i, int j) { if (j > n) return true; if (G[i][j] == 'X' || j < level || visited[i][j]) return false; visited[i][j] = true; level++; bool f = dfs(i, j - 1) || dfs(1 - i, j + k) || dfs(i, j + 1); level--; return f; } int main() { cin >> n >> k; for (int i = 0; i < 2; i++) for (int j = 0; j < n; j++) cin >> G[i][j]; n--; if (dfs(0, 0)) cout << "YES"; else cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int best[2][100010]; char s[2][100010]; queue<int> qu; int main() { int i, j, n, k, res; scanf("%d %d ", &n, &k); scanf("%s %s ", s[0], s[1]); memset(best, 9, sizeof(best)); best[0][0] = 0; qu.push(0); qu.push(0); res = 0; while (!res && !qu.empty()) { i = qu.front(); qu.pop(); j = qu.front(); qu.pop(); if (j + k >= n) res = 1; if (j + 1 < n) { if (s[i][j + 1] == '-' && best[i][j + 1] > best[i][j] + 1) { best[i][j + 1] = best[i][j] + 1; if (best[i][j + 1] - 1 < j + 1) { qu.push(i); qu.push(j + 1); } } } if (j) { if (s[i][j - 1] == '-' && best[i][j - 1] > best[i][j] + 1) { best[i][j - 1] = best[i][j] + 1; if (best[i][j - 1] - 1 < j - 1) { qu.push(i); qu.push(j - 1); } } } if (j + k < n) { if (s[1 - i][j + k] == '-' && best[1 - i][j + k] > best[i][j] + 1) { best[1 - i][j + k] = best[i][j] + 1; if (best[1 - i][j + k] - 1 < j + k) { qu.push(1 - i); qu.push(j + k); } } } } if (res) puts("YES"); else puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int dp[4][210000]; bool b[4][210000], mas[4][210000]; int n, k; char s[100100]; int inf = -100000000; int rec(int x, int y) { if (b[x][y] == 1) return dp[x][y]; b[x][y] = 1; if (mas[x][y] == 1) { dp[x][y] = inf; return inf; } if (y == 0) { dp[x][y] = inf; return inf; } dp[x][y] = inf; dp[x][y] = max(dp[x][y], rec(x, y - 1)); if (y + 1 <= n) dp[x][y] = max(dp[x][y], rec(x, y + 1) - 2); int z; if (x == 1) z = 2; else z = 1; if (y >= k) dp[x][y] = max(dp[x][y], rec(z, y - k) + k - 1); if (dp[x][y] <= 0) dp[x][y] = inf; return dp[x][y]; } int main() { cin >> n >> k; scanf("%s", s); for (int i = 0; i < n; i++) { mas[1][i + 1] = (s[i] == 'X'); } dp[1][1] = 1; b[1][1] = 1; scanf("%s", s); for (int i = 0; i < n; i++) { mas[2][i + 1] = (s[i] == 'X'); } for (int i = n + 1; i <= n + k; i++) { if (rec(1, i) > 0) { cout << "YES" << endl; return 0; } if (rec(2, i) > 0) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct state { int wall, ht, t; }; int N, K; string W[2]; bool seen[2][100001]; int main() { cin >> N >> K; cin >> W[0] >> W[1]; queue<state> Q; { state start = {0, 0, 0}; Q.push(start); seen[0][0] = 1; } while (Q.size()) { state R = Q.front(); Q.pop(); if (R.ht >= N - K) { cout << "YES\n"; return 0; } if (R.ht > 0 && W[R.wall][R.ht - 1] == '-' && R.ht - 1 >= R.t + 1) { state tmp = {R.wall, R.ht - 1, R.t + 1}; if (!seen[R.wall][R.ht - 1]) { seen[R.wall][R.ht - 1] = 1; Q.push(tmp); } } if (R.ht < N - 1 && W[R.wall][R.ht + 1] == '-') { state tmp = {R.wall, R.ht + 1, R.t + 1}; if (!seen[R.wall][R.ht + 1]) { seen[R.wall][R.ht + 1] = 1; Q.push(tmp); } } if (R.ht + K < N && W[1 - R.wall][R.ht + K] == '-') { state tmp = {1 - R.wall, R.ht + K, R.t + 1}; if (!seen[1 - R.wall][R.ht + K]) { seen[1 - R.wall][R.ht + K] = 1; Q.push(tmp); } } } cout << "NO\n"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; queue<pair<int, int> > que; vector<string> cnt; cin >> n >> k; for (int i = 0; i < 2; i++) { string a; cin >> a; cnt.push_back(a); } int dx[4] = {0, 0, -1, 1}; int dy[4] = {1, -1, k, k}; int sx = 0, sy = 0; que.push(pair<int, int>(sx, sy)); int map[2][100000]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 100000; j++) map[i][j] = 10000; } map[sx][sy] = 0; int water; while (que.size()) { pair<int, int> p = que.front(); water = map[p.first][p.second]; que.pop(); if ((water > p.second)) { continue; } if (p.second + k >= n) { cout << "YES" << endl; return 0; } for (int i = 0; i < 4; i++) { int nx = p.first + dx[i], ny = p.second + dy[i]; if ((ny >= n) || (nx < 2 && nx >= 0 && ny >= 0 && cnt[nx][ny] == '-' && map[nx][ny] == 10000)) { map[nx][ny] = map[p.first][p.second] + 1; que.push(pair<int, int>(nx, ny)); } } } cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100010; struct El { int w, h, dist; El(int w_, int h_, int dist_) : w(w_), h(h_), dist(dist_) {} bool operator<(const El& o) const { return dist > o.dist; } }; int main() { int n, k; cin >> n >> k; string walls[2]; cin >> walls[0] >> walls[1]; walls[0] += '-'; walls[1] += '-'; int dist[2][MAX_N]; memset(dist, 1, sizeof(dist)); priority_queue<El> pq; pq.push(El(0, 0, 0)); while (!pq.empty()) { El el = pq.top(); pq.pop(); if (dist[el.w][el.h] <= el.dist) continue; if (el.dist > el.h) continue; dist[el.w][el.h] = el.dist; int up = min(n, el.h + 1); if (walls[el.w][up] == '-') pq.push(El(el.w, up, el.dist + 1)); if (el.h > 0 && walls[el.w][el.h - 1] == '-') pq.push(El(el.w, el.h - 1, el.dist + 1)); int jump = min(n, el.h + k); if (walls[el.w ^ 1][jump] == '-') pq.push(El(el.w ^ 1, jump, el.dist + 1)); } if (min(dist[0][n], dist[1][n]) <= 10 * n) cout << "YES\n"; else cout << "NO\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; using pii = pair<long long, long long>; const long long N = 1e6 + 7; long long n, k, sp = N; char s[2][N]; bool vis[2][N]; long long dist[2][N]; struct state { long long id, seg, water; } t; bool go(long long id, long long seg, long long water) { if (seg == -1) return 0; if (vis[id][seg] || s[id][seg] == 'X' || seg < water) return 0; else return 1; } void bfs() { queue<state> q; t = {0, 0, 0}; q.emplace(t); vis[0][0] = 1; dist[0][0] = 0; while (!q.empty()) { state v = q.front(); q.pop(); long long id = v.id, seg = v.seg, water = v.water; long long idn, segn, watern; idn = id, segn = seg + 1, watern = water + 1; if (go(idn, segn, watern)) { dist[idn][segn] = dist[id][seg] + 1; if (segn >= n) { sp = dist[idn][segn]; return; } vis[idn][segn] = 1; t = {idn, segn, watern}; q.emplace(t); } idn = id, segn = seg - 1; if (go(idn, segn, watern)) { dist[idn][segn] = dist[id][seg] + 1; if (segn >= n) { sp = dist[idn][segn]; return; } vis[idn][segn] = 1; t = {idn, segn, watern}; q.emplace(t); } idn = 1 ^ id, segn = seg + k; if (go(idn, segn, watern)) { dist[idn][segn] = dist[id][seg] + 1; if (segn >= n) { sp = dist[idn][segn]; return; } vis[idn][segn] = 1; t = {idn, segn, watern}; q.emplace(t); } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (long long i = 0; i < 2; ++i) { cin >> s[i]; } bfs(); cout << (sp <= n ? "YES" : "NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; char wall[2][100000]; bool dfs(int x, int y, int h) { if (y >= n) return true; if (y < 0 || y <= h) return false; if (wall[x][y] == 'X') return false; wall[x][y] = 'X'; if (dfs(1 - x, y + k, h + 1)) return true; if (dfs(x, y + 1, h + 1)) return true; if (dfs(x, y - 1, h + 1)) return true; return false; } int main() { while (cin >> n >> k) { for (int i = 0; i < 2; i++) cin >> wall[i]; if (dfs(0, 0, -1)) cout << "YES" << endl; else cout << "NO" << endl; } }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; string s[2]; int height[100000][2]; void solve() { memset(height, 0x3f, sizeof(height)); queue<pair<int, int>> q; q.push({0, 0}); height[0][0] = -1; while (!q.empty()) { int p = q.front().first, side = q.front().second; int w = height[p][side]; q.pop(); if (p + k >= n) { cout << "YES"; exit(0); } if (p - 1 >= 0 && height[p - 1][side] > 1000000 && s[side][p - 1] == '-' && w + 1 < p - 1) { q.push({p - 1, side}); height[p - 1][side] = w + 1; } if (height[p + 1][side] > 1000000 && s[side][p + 1] == '-' && w + 1 < p + 1) { q.push({p + 1, side}); height[p + 1][side] = w + 1; } if (height[p + k][side ^ 1] > 1000000 && s[side ^ 1][p + k] == '-' && w + 1 < p + k) { q.push({p + k, side ^ 1}); height[p + k][side ^ 1] = w + 1; } } cout << "NO"; exit(0); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; cin >> s[0] >> s[1]; solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = int(1.5 * 1e7); const int inf = ~0U >> 1; const long long ll_inf = 1e18 + 420; const double eps = 1e-4; const int N = 1e6 + 7; const int MAX = 2e5 + 9; const int MOD = 1e9 + 7; const long double pi = 3.14159265359; int n, k; bool a[10][N]; string s[10]; void rec(int v, int u, int pos) { if (s[v][u] == 'X' || u < pos || a[v][u]) { return; } if (u + k > n) { cout << "YES"; exit(0); } a[v][u] = 1; rec((v + 1) % 2, u + k, pos + 1); rec(v, u + 1, pos + 1); rec(v, u - 1, pos + 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; cin >> s[0] >> s[1]; rec(0, 0, 0); cout << "NO"; }
8
CPP
n,k = map(int,input().split()) l = input() r = input() data = [0, ' '+l,' '+r] dist = [[1000000]*100005 for _ in range(3)] visited = [[False]*100005 for _ in range(3)] dist[1][1]=0 visited[1][1]=True qx,qy = [1],[1] while qy: x,y = qx.pop(),qy.pop() if dist[x][y]>=y: continue if x==1: poss = [[1,y+1],[1,y-1],[2,y+k]] else: poss = [[2,y+1],[2,y-1],[1,y+k]] for i,e in enumerate(poss): newx,newy = e[0],e[1] if newy>n: print('YES') from sys import exit exit() if 0<newy<=n and not visited[newx][newy] and data[newx][newy]=='-': visited[newx][newy]=True dist[newx][newy]=dist[x][y]+1 qx=[newx]+qx qy=[newy]+qy print('NO')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int way[3][2] = {{0, 1}, {0, -1}, 1}; char str[2][100005]; int n, k; struct Node { int x, y, step; bool check() { if (x >= 0 && y >= 0 && x < 2 && y < n) return true; } } s, e, u, v; bool bfs() { queue<Node> que; bool flag[2][100005]; memset(flag, false, sizeof(flag)); flag[s.x][s.y] = true; que.push(s); while (!que.empty()) { u = que.front(); que.pop(); for (int i = 0; i < 3; i++) { v = u; v.x = (v.x + way[i][0]) % 2; v.y += way[i][1]; v.step++; if (v.y >= n) return true; if (v.check() && v.y >= v.step && str[v.x][v.y] == '-' && flag[v.x][v.y] == false) { flag[v.x][v.y] = true; que.push(v); } } } return false; } int main() { while (scanf("%d%d", &n, &k) != EOF) { scanf("%s%s", str[0], str[1]); way[2][1] = k; s.x = 0; s.y = 0; s.step = 0; if (bfs()) printf("YES\n"); else printf("NO\n"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int nmax = 200009; priority_queue<pair<int, int> > Q; char s[2][nmax]; int F[2][nmax]; void go(int v, int w, int t) { if (v > 0 && (F[w][v] == -1 || t < F[w][v]) && s[w][v] != 'X') { if (t < v) { F[w][v] = t; Q.push(make_pair(-t, (v << 1) + w)); } } } int n, k; int main() { scanf("%d %d", &n, &k); scanf("%s", &s[0][1]); scanf("%s", &s[1][1]); memset(F, -1, sizeof(F)); F[0][1] = 0; Q.push(make_pair(0, 1 << 1)); bool ok = false; while (!Q.empty()) { int v = Q.top().second; int w = v & 1; v >>= 1; int d = -Q.top().first; Q.pop(); if (v > n) { ok = true; break; } if (d == F[w][v]) { go(v + 1, w, d + 1); go(v - 1, w, d + 1); go(v + k, 1 - w, d + 1); } } if (ok) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; string s[2]; int n, k; const int INF = 1e9 + 5, maxn = 1e5 + 5; bool valid(int sd, int v) { return v >= 0 && v <= n && s[sd][v] == '-'; } int main() { cin >> n >> k; cin >> s[0] >> s[1]; s[0] += "-"; s[1] += "-"; vector<vector<int>> d(2, vector<int>(maxn, INF)); queue<pair<int, int>> q; d[0][0] = 0; q.push({0, 0}); bool is = false; while (!q.empty()) { int v = q.front().second; int sd = q.front().first; q.pop(); if (v >= n) { is = true; break; } if (d[sd][v] > v) continue; if (valid(sd, v + 1) && d[sd][v] + 1 < d[sd][v + 1]) { d[sd][v + 1] = d[sd][v] + 1; q.push({sd, v + 1}); } if (valid(sd, v - 1) && d[sd][v] + 1 < d[sd][v - 1]) { d[sd][v - 1] = d[sd][v] + 1; q.push({sd, v - 1}); } if (valid(sd ^ 1, min(n, v + k)) && d[sd][v] + 1 < d[sd ^ 1][min(n, v + k)]) { d[sd ^ 1][min(n, v + k)] = d[sd][v] + 1; q.push({sd ^ 1, min(n, v + k)}); } } if (is) cout << "YES"; else cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long n, k; cin >> n >> k; long long d[n][2]; string s[2]; cin >> s[0] >> s[1]; for (int i = 0; i < n; ++i) { d[i][0] = d[i][1] = INT_MAX; } d[0][0] = 0; queue<pair<long long, long long>> q; q.push(make_pair(0, 0)); pair<long long, long long> source; while (!q.empty()) { source = q.front(); q.pop(); long long curr = source.first; long long curr2 = source.second; if ((curr + k) >= n) { cout << "YES\n"; return 0; } if ((curr - 1) >= 0 && s[curr2][curr - 1] == '-' && d[curr - 1][curr2] == INT_MAX && (d[curr][curr2] + 1) <= (curr - 1)) { q.push(make_pair(curr - 1, curr2)); d[curr - 1][curr2] = d[curr][curr2] + 1; } if ((curr + 1) < n && s[curr2][curr + 1] == '-' && d[curr + 1][curr2] == INT_MAX && (d[curr][curr2] + 1) <= (curr + 1)) { q.push(make_pair(curr + 1, curr2)); d[curr + 1][curr2] = d[curr][curr2] + 1; } if ((curr + k) < n && s[(curr2 + 1) % 2][curr + k] == '-' && d[curr + k][(curr2 + 1) % 2] == INT_MAX && (d[curr][curr2] + 1) <= (curr + k)) { q.push(make_pair(curr + k, (curr2 + 1) % 2)); d[curr + k][(curr2 + 1) % 2] = d[curr][curr2] + 1; } } cout << "NO\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k; char l[100005], d[100005]; vector<int> niz[100005 + 100005 + 1]; bool poseceno[100005 + 100005 + 1]; int dub[100005 + 100005 + 1]; queue<int> q; void bfs() { q.push(0); while (!q.empty()) { int t = q.front(); if (t == 100005 + 100005) { printf("YES"); exit(0); } q.pop(); if (t % n < dub[t]) continue; for (int i = 0; i < niz[t].size(); i++) { if (!poseceno[niz[t][i]]) q.push(niz[t][i]); dub[niz[t][i]] = dub[t] + 1; poseceno[niz[t][i]] = true; } } } int main() { scanf("%d %d", &n, &k); scanf("%s", l); scanf("%s", d); for (int i = 1; i < n; i++) { if (!(l[i] == 'X' || l[i - 1] == 'X')) { niz[i].push_back(i - 1); niz[i - 1].push_back(i); } if (!(d[i] == 'X' || d[i - 1] == 'X')) { niz[n + i].push_back(n + i - 1); niz[n + i - 1].push_back(n + i); } int s = i - 1; if (l[s] != 'X') { if (s + k >= n) niz[s].push_back(100005 + 100005); else if (d[s + k] != 'X') niz[s].push_back(n + s + k); } if (d[s] != 'X') { if (s + k >= n) niz[n + s].push_back(100005 + 100005); else if (l[s + k] != 'X') niz[n + s].push_back(s + k); } } if (l[n - 1] != 'X') niz[n - 1].push_back(100005 + 100005); if (d[n - 1] != 'X') niz[n + n - 1].push_back(100005 + 100005); dub[0] = 0; bfs(); printf("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> int left_wall[100010]; int right_wall[100010]; int left_wall_steps[100010]; int right_wall_steps[100010]; int left_que[100010]; int right_que[100010]; int head_left; int head_right; int tail_left; int tail_right; int ninja_jump; int wall_height; bool ExitExist; int last_step; void push_left(int x) { left_que[head_left] = x; head_left++; } void push_right(int x) { right_que[head_right] = x; head_right++; } int pop_left() { if (head_left == tail_left) return -1; int res = left_que[tail_left]; tail_left++; return res; } int pop_right() { if (head_right == tail_right) return -1; int res = right_que[tail_right]; tail_right++; return res; } int main() { char temp_char; std::cin >> wall_height >> ninja_jump; for (int i = 1; i <= wall_height; i++) { std::cin >> temp_char; if (temp_char == 'X') left_wall[i] = 1; else if (temp_char == '-') left_wall[i] = 0; } for (int i = 1; i <= wall_height; i++) { std::cin >> temp_char; if (temp_char == 'X') right_wall[i] = 1; else if (temp_char == '-') right_wall[i] = 0; } push_left(1); left_wall_steps[1] = 1; last_step = 1; int curr_left = 0; int curr_right = 0; while (last_step < wall_height && head_left != tail_left || head_right != tail_right) { curr_left = pop_left(); curr_right = pop_right(); if (last_step + ninja_jump > wall_height) { last_step = wall_height; continue; } if (curr_left != -1) { if (left_wall[curr_left + 1] == 0 && left_wall_steps[curr_left + 1] == 0) { push_left(curr_left + 1); left_wall_steps[curr_left + 1] = left_wall_steps[curr_left] + 1; if (last_step < curr_left + 1) last_step = curr_left + 1; } if (right_wall[curr_left + ninja_jump] == 0 && right_wall_steps[curr_left + ninja_jump] == 0) { push_right(curr_left + ninja_jump); right_wall_steps[curr_left + ninja_jump] = left_wall_steps[curr_left] + 1; if (last_step < curr_left + ninja_jump) last_step = curr_left + ninja_jump; } if (left_wall[curr_left - 1] == 0 && left_wall_steps[curr_left - 1] == 0 && (curr_left - 1) >= left_wall_steps[curr_left] + 1) { push_left(curr_left - 1); left_wall_steps[curr_left - 1] = left_wall_steps[curr_left] + 1; } } if (curr_right != -1) { if (right_wall[curr_right + 1] == 0 && right_wall_steps[curr_right + 1] == 0) { push_right(curr_right + 1); right_wall_steps[curr_right + 1] = right_wall_steps[curr_right] + 1; if (last_step < curr_right + 1) last_step = curr_right + 1; } if (left_wall[curr_right + ninja_jump] == 0 && left_wall_steps[curr_right + ninja_jump] == 0) { push_left(curr_right + ninja_jump); left_wall_steps[curr_right + ninja_jump] = right_wall_steps[curr_right] + 1; if (last_step < curr_right + ninja_jump) last_step = curr_right + ninja_jump; } if (right_wall[curr_right - 1] == 0 && right_wall_steps[curr_right - 1] == 0 && (curr_right - 1) >= right_wall_steps[curr_right] + 1) { push_right(curr_right - 1); right_wall_steps[curr_right - 1] = right_wall_steps[curr_right] + 1; } } } if (last_step >= wall_height) { std::cout << "YES\n"; } else { std::cout << "NO\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int i, j, n, m; bool inside(int i, int n) { return (i >= 0 && i < n && j >= 0 && j < n); } const int N = 3e5 + 5; bool vis[2][N]; bool f = 0; string s[2]; void dfs(int i, int j, int k) { if (k > j || s[i][j] == 'X' || f || vis[i][j]) return; vis[i][j] = 1; if (j + m >= n) { f = 1; return; } dfs((i + 1) % 2, j + m, k + 1); dfs(i, j + 1, k + 1); dfs(i, j - 1, k + 1); } int main() { cin >> n >> m; cin >> s[0] >> s[1]; dfs(0, 0, 0); cout << (f ? "YES" : "NO"); return 0; }
8
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long MAX = 1e5 + 5; const long long MAX2 = 11; const int MOD = 1000000000 + 7; const long long INF = 1e18; const int nr[] = {1, 0, 0, -1, 1, 1, -1, -1}; const int nc[] = {0, 1, -1, 0, 1, -1, 1, -1}; int n, k, nx, lr, nw, vis[2][MAX]; char x[2][MAX]; queue<pair<bool, int> > q; bool ans; pair<bool, int> cur; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (long long j = 0; j <= 1; j++) for (long long i = 1; i <= n; i++) cin >> x[j][i]; memset(vis, -1, sizeof vis); q.push({0, 1}); vis[0][1] = 1; while (!q.empty()) { cur = q.front(); q.pop(); lr = cur.first, nw = cur.second; if (nw + k > n) { ans = 1; break; } for (long long i = 1; i <= 2; i++) { nx = nw + nc[i]; if (x[lr][nx] == '-' && vis[lr][nx] == -1 && nx > vis[lr][nw]) { vis[lr][nx] = vis[lr][nw] + 1; q.push({lr, nx}); } } if (x[lr ^ 1][nw + k] == '-' && vis[lr ^ 1][nw + k] == -1) { vis[lr ^ 1][nw + k] = vis[lr][nw] + 1; q.push({lr ^ 1, nw + k}); } } if (ans) cout << "YES"; else cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long f[2][100005]; long n, k, flag = 0; char s[2][100005]; void dfs(long wall, long h, long water) { f[wall][h] = 1; if (h + k >= n) { flag = 1; return; } if (s[!wall][h + k] != 'X' && !f[!wall][h + k]) { dfs(!wall, h + k, water + 1); } if (s[wall][h + 1] != 'X' && !f[wall][h + 1]) { dfs(wall, h + 1, water + 1); } if (h > 0) { if (s[wall][h - 1] != 'X' && !f[wall][h - 1] && water + 1 <= h - 1) { dfs(wall, h - 1, water + 1); } } } int main() { scanf("%ld %ld", &n, &k); scanf("%s %s", s[0], s[1]); memset(f, 0, sizeof(f)); dfs(0, 0, 0); if (flag) { printf("YES"); } else { printf("NO"); } }
8
CPP
#include <bits/stdc++.h> using namespace std; template <class T> bool umin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool umax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } vector<int> adj[200009]; char s[200009], t[200009]; void add(int u, int v) { adj[u].push_back(v); } int dis[200009], n, k, son; int flood(int x) { if (x == son) return 1000000007; if (x % n == 0) return n; return x % n; } int main() { scanf("%d%d", &n, &k); scanf("%s", s + 1); scanf("%s", t + 1); son = 2 * n + 1; for (int i = 1; i <= n; i++) { if (s[i] == '-' and s[i + 1] != 'X') { if (i < n) add(i, i + 1); else add(i, son); } if (s[i] == '-' and s[i - 1] == '-' and i > 1) add(i, i - 1); if (s[i] == '-' and t[i + k] != 'X') { if (i + k <= n) add(i, n + i + k); else add(i, son); } if (t[i] == '-' and t[i + 1] != 'X') { if (i < n) add(i + n, i + n + 1); else add(i + n, son); } if (t[i] == '-' and t[i - 1] == '-' and i > 1) add(i + n, i + n - 1); if (t[i] == '-' and s[i + k] != 'X') { if (i + k <= n) add(n + i, i + k); else add(n + i, son); } } fill(dis, dis + 200009, 1000000007); dis[1] = 0; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; q.push(make_pair(0, 1)); while (!q.empty()) { int u = q.top().first; int nd = q.top().second; q.pop(); if (nd == son) break; if (dis[nd] != u) continue; for (int i = 0; i < adj[nd].size(); i++) { int to = adj[nd][i]; if (dis[to] > dis[nd] + 1 and dis[nd] + 1 < flood(to)) { dis[to] = dis[nd] + 1; q.push(make_pair(dis[to], to)); } } } if (dis[son] != 1000000007) printf("YES\n"); else printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; char s[3][100005]; vector<int> g[100005 * 2]; int f, n, k; set<pair<int, int> > Q; int dp[100005 * 2]; bool ok(int v, int u) { int i = u == f ? n : u % n; return (dp[v] < i); } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < 2; ++i) scanf("%s", s[i]); f = n * 2; for (int i = 0; i < 2; ++i) { for (int j = 0; j < n; ++j) { if (s[i][j] == 'X') continue; int v = i * n + j; g[v].push_back(j + 1 == n ? f : v + 1); if (j > 0) g[v].push_back(v - 1); g[v].push_back(j + k >= n ? f : j + k + (!i * n)); } } for (int i = 1; i <= f; ++i) dp[i] = int(1e9); Q.insert(make_pair(dp[0], 0)); while (!Q.empty()) { int v = Q.begin()->second; Q.erase(Q.begin()); for (int j = 0; j < int((g[v]).size()); ++j) { int to = g[v][j]; if (dp[v] + 1 < dp[to] && ok(v, to)) { Q.erase(make_pair(dp[to], to)); dp[to] = dp[v] + 1; Q.insert(make_pair(dp[to], to)); } } } puts(dp[f] == int(1e9) ? "NO" : "YES"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool ch[2][100005]; string s[2]; int n, k; void dfs(int x, int y, int h) { if (s[x][y] != '-' || y < h || ch[x][y]) { return; } if (y + k >= n) { cout << "YES" << endl; exit(0); } ch[x][y] = 1; dfs((x + 1) % 2, y + k, h + 1); dfs(x, y + 1, h + 1); dfs(x, y - 1, h + 1); } int main() { cin >> n >> k; cin >> s[0] >> s[1]; dfs(0, 0, 0); cout << "NO" << endl; }
8
CPP
from sys import stdin, stdout from collections import deque n, k = map(int, stdin.readline().split()) maps = [] maps.append(list(stdin.readline() + '-')) maps.append(list(stdin.readline() + '-')) visit = [[0, 0] for i in range(n + 1)] visit[0][0] = 1 queue = deque() label = 0 queue.append((0, -1, 0))#ر‚ذ²ذ¾ذ¹ رƒر€ذ¾ذ²ذµذ½رŒ, رƒر€ذ¾ذ²ذµذ½رŒ ذ²ذ¾ذ´ر‹, ذ½ذ¾ذ¼ذµر€ رپر‚ذµذ½ر‹ while queue: mine, line, num = queue.popleft() if line >= mine: continue if mine + k >= n: label = 1 if mine + 1 < n and not visit[mine + 1][num] and maps[num][mine + 1] == '-': queue.append((mine + 1, line + 1, num)) visit[mine + 1][num] = 1 if mine and mine - line > 1 and not visit[mine - 1][num] and maps[num][mine - 1] == '-': queue.append((mine - 1, line + 1, num)) visit[mine - 1][num] = 1 if mine + k < n and not visit[mine + k][(num + 1) % 2] and maps[(num + 1) % 2][mine + k] == '-': queue.append((min(mine + k, n), line + 1, (num + 1) % 2)) visit[min(mine + k, n)][(num + 1) % 2] = 1 if label: stdout.write('YES') else: stdout.write('NO') # Made By Mostafa_Khaled
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, k, vis[200010][2]; string s[2]; bool rec(int w, int wl, int h) { if (h < 0) return false; if (h >= n) return true; if (wl >= h) return false; if (s[w][h] == 'X') return false; if (vis[h][w]) return false; vis[h][w] = 1; return (rec(w, wl + 1, h - 1) || rec(w ^ 1, wl + 1, h + k) || rec(w, wl + 1, h + 1)); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); memset(vis, 0, sizeof vis); cin >> n >> k >> s[0] >> s[1]; if (rec(0, -1, 0)) { cout << "YES"; } else { cout << "NO"; } return 0; }
8
CPP
#include <bits/stdc++.h> #pragma GCC optimize "O3" using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int N = 1e5 + 5; int dp[N][2]; int n, k; string s[2]; queue<pair<int, int> > stany; void solve() { memset(dp, inf, sizeof(dp)); cin >> n >> k >> s[0] >> s[1]; dp[1][0] = 0; stany.push({1, 0}); while (!stany.empty()) { auto v = stany.front(); stany.pop(); for (auto x : {make_pair(v.first + 1, v.second), make_pair(v.first - 1, v.second), make_pair(v.first + k, v.second ^ 1)}) { x.first = max(1, min(n + 1, x.first)); if (s[x.second][x.first - 1] == 'X') continue; if (dp[x.first][x.second] == inf && dp[v.first][v.second] + 1 < x.first) { dp[x.first][x.second] = dp[v.first][v.second] + 1; stany.push({x.first, x.second}); } } } cout << (dp[n + 1][0] != inf || dp[n + 1][1] != inf ? "YES" : "NO") << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); 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 = 998244353; 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 vis[2][100005]; int can[2][100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; string s[2]; cin >> s[0] >> s[1]; queue<pair<int, int> > q; q.push(make_pair(0, 0)); while (!q.empty()) { pair<int, int> 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 && !vis[y][x + ii] && s[y][x + ii] == '-') { vis[y][x + ii] = true; can[y][x + ii] = can[y][x] + 1; q.push(make_pair(x + ii, y)); } } if (!vis[1 - y][x + k] && s[1 - y][x + k] == '-') { vis[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
#include <bits/stdc++.h> using namespace std; const int N = 2 * 1e5 + 100, INF = 1e9; int dis[2][N], n, k; bool mp[2][N]; int main() { for (int i = 0; i < N; i++) dis[0][i] = INF, dis[1][i] = INF; cin >> n >> k; string s; cin >> s; for (int i = 0; i < n; i++) if (s[i] == '-') mp[0][i] = true; else mp[0][i] = false; cin >> s; for (int i = 0; i < n; i++) if (s[i] == '-') mp[1][i] = true; else mp[1][i] = false; queue<pair<int, int> > q; q.push(pair<int, int>(0, 0)); dis[0][0] = 0; while (q.size()) { pair<int, int> p = q.front(); q.pop(); int d = dis[p.first][p.second]; if (p.second <= d - 1) continue; if (p.second >= n) { cerr << p.second << endl; cout << "YES" << endl; return 0; } if (mp[p.first][p.second] == false) continue; if (p.second - 1 >= 0 && dis[p.first][p.second - 1] > d + 1) { q.push(pair<int, int>(p.first, p.second - 1)); dis[p.first][p.second - 1] = d + 1; } if (p.second + 1 >= n || dis[p.first][p.second + 1] > d + 1) { q.push(pair<int, int>(p.first, p.second + 1)); dis[p.first][p.second + 1] = d + 1; } if (p.second + k >= n || dis[(p.first + 1) % 2][p.second + k] > d + 1) { q.push(pair<int, int>((p.first + 1) % 2, p.second + k)); dis[(p.first + 1) % 2][p.second + k] = d + 1; } } cout << "NO" << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433832795; const long double eps = 1e-9; string l, r; vector<int> v[200005]; bool visit[200005]; set<pair<int, int> > s; int main() { int n, k, i; cin >> n >> k; cin >> l >> r; for (i = 0; i < l.length() - 1; i++) { if (l[i] == '-' && l[i + 1] == '-') { v[i].push_back((i + 1)); v[i + 1].push_back((i)); } if (r[i] == '-' && r[i + 1] == '-') { v[n + i].push_back((n + i + 1)); v[n + i + 1].push_back(n + i); } if (i + k < n) { if (l[i] == '-' && r[i + k] == '-') { v[i].push_back(n + i + k); } if (r[i] == '-' && l[i + k] == '-') { v[n + i].push_back(i + k); } } } s.insert(make_pair(0, 0)); while (!s.empty()) { int node = s.begin()->first; int water = s.begin()->second; visit[node] = true; if (node < n) { if (node + k >= n) { cout << "YES" << endl; return 0; } } if (node >= n) { if (node + k >= (2 * n)) { cout << "YES" << endl; return 0; } } s.erase(s.begin()); for (int i = 0; i < v[node].size(); i++) { if (visit[v[node][i]] == false) { if (v[node][i] >= n) { if ((v[node][i] - n) > water) s.insert(make_pair(v[node][i], water + 1)); } if (v[node][i] < n) { if (v[node][i] > water) s.insert(make_pair(v[node][i], water + 1)); } } } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, q0, k; int q[500010], f[500010], d[500010]; char s1[500010], s2[500010]; vector<int> v[500010]; int main() { scanf("%d%d\n", &n, &k); scanf("%s\n", s1); scanf("%s\n", s2); for (int i = 0; i < n; ++i) { if (s1[i] == 'X') f[i] = 1; if (s2[i] == 'X') f[i + n] = 1; if (i > 0) { v[i].push_back(i - 1); v[i + n].push_back(i + n - 1); } if (i < n - 1) { v[i].push_back(i + 1); v[i + n].push_back(i + n + 1); } if (i + k < n) { v[i].push_back(i + n + k); v[i + n].push_back(i + k); } else { v[i].push_back(2 * n); v[i + n].push_back(2 * n); } } q0 = 1; f[0] = 1; int nod; for (int i = 1; i <= q0; ++i) { nod = q[i]; if (nod == 2 * n) { printf("YES\n"); return 0; } if (d[nod] > nod % n) continue; for (int j = 0; j < v[nod].size(); ++j) { if (f[v[nod][j]] == 0) { d[v[nod][j]] = d[nod] + 1; q[++q0] = v[nod][j]; f[v[nod][j]] = 1; } } } printf("NO\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int max_n = 10000010; int vis[2][max_n]; int d[2][max_n]; int n, k, ans; void dfs(int i, int j, int water) { if (water >= j) { return; } if (j >= n) { ans = 1; return; } vis[i][j] = 1; if (d[i][j - 1] != 1 && vis[i][j - 1] == 0) { dfs(i, j - 1, water + 1); } if ((d[1 - i][j + k] != 1 && vis[1 - i][j + k] == 0)) { dfs(1 - i, j + k, water + 1); } if ((d[i][j + 1] != 1 && vis[i][j + 1] == 0)) { dfs(i, j + 1, water + 1); } } int main() { cin >> n >> k; for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { char x; cin >> x; if (x == '-') { } else { d[i][j] = 1; } } } dfs(0, 0, -1); if (ans == 1) { cout << "YES"; return 0; } cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-9; int n, k; string s[3]; bool vis[3][100010]; struct J { int w, lr, lv; }; bool valid(int x, int i, int u, int w) { if (x > n) { cout << "YES"; exit(0); } if (i == 2) { if (x > 1 && x > w && s[u ^ 3][x] != 'X' && vis[u ^ 3][x] == false) return true; else return false; } if (x > 1 && x > w && s[u][x] != 'X' && vis[u][x] == false) return true; else return false; } string bfs() { queue<J> q; J x; x.w = 0, x.lr = 1, x.lv = 1; q.push(x); int fx[] = {1, -1, k}; while (!q.empty()) { J u = q.front(); q.pop(); for (int i = 0; i < 3; i++) { int xx = u.lv + fx[i]; if (valid(xx, i, u.lr, u.w + 1)) { J v; v.w = u.w + 1, v.lv = xx; if (i == 2) v.lr = u.lr ^ 3; else v.lr = u.lr; vis[v.lr][xx] = 1; q.push(v); } } } return "NO"; } int main() { cin >> n >> k; cin >> s[1] >> s[2]; s[1] = '#' + s[1]; s[2] = '#' + s[2]; if (s[1][1] == 'X') { cout << "NO"; return 0; } cout << bfs() << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const long long MOD = 1e9 + 7; int n, k; bool col[N]; vector<int> g[N]; int get(int i) { if (i >= n) return i - n; return i; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n >> k; string s, t; cin >> s >> t; s += t; for (int i = 0; i < (int)(n); ++i) { if (i + k >= n) g[i].push_back(2 * n); else { g[i].push_back(n + i + k); g[n + i + k].push_back(i); g[i].push_back(i + 1); g[i + 1].push_back(i); } } for (int i = (n); i <= (int)(2 * n - 1); ++i) { if (i + k >= 2 * n) g[i].push_back(2 * n); else { g[i].push_back(i + k - n); g[i + k - n].push_back(i); g[i].push_back(i + 1); g[i + 1].push_back(i); } } queue<pair<int, int> > q; q.push({0, 0}); while (!q.empty()) { int v = q.front().first; int brdz = q.front().second; q.pop(); col[v] = true; if (s[v] == 'X' || brdz > get(v)) { continue; } for (auto to : g[v]) { if (col[to]) continue; col[to] = true; q.push({to, brdz + 1}); } } if (col[2 * n]) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, d[2][100100], di; string s[2]; int main() { cin >> n >> k; const int dx[] = {0, 0, 1}, dy[] = {1, -1, k}; cin >> s[0]; cin >> s[1]; queue<pair<bool, int> > q; q.push(make_pair(0, 0)); memset(d, -1, sizeof(d)); d[0][0] = 0; while (!q.empty()) { pair<bool, int> u = q.front(); q.pop(); int x = u.first, y = u.second; if (y + k >= n) { cout << "YES"; return 0; } for (int i = 0; i < 3; i++) { int nx = x ^ dx[i], ny = y + dy[i]; if (ny < 0) continue; if (s[nx][ny] == 'X') continue; if (d[nx][ny] != -1) continue; if (d[x][y] + 1 > ny) continue; d[nx][ny] = d[x][y] + 1; q.push(make_pair(nx, ny)); } } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { queue<pair<int, int> > que; int n, k; vector<string> cnt; cin >> n >> k; int mp[2][100000]; int dx[4] = {0, 0, -1, 1}; int dy[4] = {1, -1, k, k}; for (int i = 0; i < 2; i++) { string a; cin >> a; cnt.push_back(a); } for (int i = 0; i < 2; i++) for (int j = 0; j < 100000; j++) { mp[i][j] = 10000000; } for (int i = 0; i < 2; i++) { for (int j = 0; j < cnt[0].size(); j++) { if (cnt[i][j] == 'X') mp[i][j] = 100000000; } } int sx = 0, sy = 0; mp[sx][sy] = 0; que.push(pair<int, int>(sx, sy)); while (!que.empty()) { pair<int, int> p = que.front(); que.pop(); int water = mp[p.first][p.second]; if (water > p.second) { continue; } if (p.second + k >= n) { cout << "YES" << endl; return 0; } for (int i = 0; i < 4; i++) { int nx = p.first + dx[i]; int ny = p.second + dy[i]; if (((ny >= n) && (nx >= 0) && (nx < 2)) || (nx >= 0 && nx < 2 && ny >= 0 && ny < n && mp[nx][ny] == 10000000)) { mp[nx][ny] = mp[p.first][p.second] + 1; que.push(pair<int, int>(nx, ny)); } } } cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; int n, k; char g[2][N]; bool vis[2][N]; queue<pair<int, int> > qu; bool BFS(int x, int y) { qu.push(make_pair(x, y)); vis[x][y] = true; int lev = 0; while (!qu.empty()) { int siz = qu.size(); while (siz--) { int x = qu.front().first, y = qu.front().second; qu.pop(); if (y + k >= n || y + 1 >= n) return true; if (y - 1 >= 0 && y - 1 > lev && g[x][y - 1] != 'X' && !vis[x][y - 1]) { vis[x][y - 1] = true; qu.push(make_pair(x, y - 1)); } if (g[x][y + 1] != 'X' && !vis[x][y + 1]) { vis[x][y + 1] = true; qu.push(make_pair(x, y + 1)); } if (g[x ^ 1][y + k] != 'X' && !vis[x ^ 1][y + k]) { vis[x ^ 1][y + k] = true; qu.push(make_pair(x ^ 1, y + k)); } } ++lev; } return false; } int main() { scanf("%d%d%s%s", &n, &k, g[0], g[1]); if (BFS(0, 0)) puts("YES"); else puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void print(char* A, char* B, int h) { for (int i = 0; i < h; i++) { cout << A[i]; } cout << endl; for (int i = 0; i < h; i++) { cout << B[i]; } } bool backtrack(int pos, char* A, char* B, int h, int jump, int water) { if (pos < h) { if (pos < water) return false; water++; if (pos < 0) return false; if (A[pos] != 'X') { A[pos] = 'X'; if (pos + jump >= h) return true; return backtrack(pos - 1, A, B, h, jump, water) || backtrack(pos + jump, B, A, h, jump, water) || backtrack(pos + 1, A, B, h, jump, water); } else { return false; } } return true; } int main() { int h, j; cin >> h >> j; char A[h], B[h]; for (int i = 0; i < h; i++) cin >> A[i]; for (int i = 0; i < h; i++) cin >> B[i]; bool result = backtrack(0, A, B, h, j, 0); if (result) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; char s[2][maxn]; bool visit[2][maxn]; int n = 2; int m; inline bool check(int x, int y) { return y >= 0 && y < m; } int main() { ios::sync_with_stdio(false); int k; int d = 0; queue<pair<int, int> > Q; pair<int, int> p(0, 0); int x, y; int sz; bool flag = false; scanf("%d %d", &m, &k); for (int i = 0; i < 2; ++i) scanf("%s", s[i]); visit[0][0] = true; Q.push(p); while (1) { sz = Q.size(); if (sz == 0) break; while (sz--) { p = Q.front(); Q.pop(); if (p.second < d) continue; x = 1 - p.first; y = p.second + k; if (y >= m) { flag = true; goto _output; } if (check(x, y) && !visit[x][y]) { visit[x][y] = true; if (s[x][y] == '-' && y >= d) { Q.push(make_pair(x, y)); } } x = p.first; y = p.second + 1; if (y >= m) { flag = true; goto _output; } if (check(x, y) && !visit[x][y]) { visit[x][y] = true; if (s[x][y] == '-' && y >= d) { Q.push(make_pair(x, y)); } } y = p.second - 1; if (y >= m) { flag = true; goto _output; } if (check(x, y) && !visit[x][y]) { visit[x][y] = true; if (s[x][y] == '-' && y >= d) { Q.push(make_pair(x, y)); } } } ++d; } _output: puts(flag ? "YES" : "NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; queue<int> q; int n, k, d[5][100005]; string c[2]; inline bool ok(int t, int x, int y) { return (c[y][x] == '-' && x > t); } int main() { cin >> n >> k; cin >> c[0] >> c[1]; q.push(0); q.push(0); q.push(-1); while (!q.empty()) { int x = q.front(); q.pop(); int y = q.front(); q.pop(); if (d[y][x] != 0) { q.pop(); continue; } d[y][x] = 1; if (x + k >= n) { cout << "YES"; return 0; } q.front()++; if (ok(q.front(), x + k, abs(y - 1))) q.push(x + k), q.push(abs(y - 1)), q.push(q.front()); if (ok(q.front(), x - 1, y)) q.push(x - 1), q.push(y), q.push(q.front()); if (ok(q.front(), x + 1, y)) q.push(x + 1), q.push(y), q.push(q.front()); q.pop(); } cout << "NO"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 100005; int N; int k; char wall[2][MAX]; int mark[2][MAX]; bool dfs() { queue<int> Q; Q.push(1), Q.push(0); mark[0][1] = 1; int x, w, h; while (!Q.empty()) { x = Q.front(), Q.pop(); w = Q.front(), Q.pop(); h = mark[w][x]; if (x + 1 > N) return true; if (!mark[w][x + 1] && h < x + 1 && wall[w][x + 1] == '-') { Q.push(x + 1); Q.push(w); mark[w][x + 1] = h + 1; } if (x - 1 > 0 && !mark[w][x - 1] && h < x - 1 && wall[w][x - 1] == '-') { Q.push(x - 1); Q.push(w); mark[w][x - 1] = h + 1; } if (x + k > N) return true; if (!mark[w ^ 1][x + k] && h < x + k && wall[w ^ 1][x + k] == '-') { Q.push(x + k); Q.push(1 ^ w); mark[1 ^ w][x + k] = h + 1; } } return false; } int main() { scanf("%d%d", &N, &k); scanf("%s%s", wall[0] + 1, wall[1] + 1); if (dfs()) puts("YES"); else puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 1e6 + 10; int dis[2][N]; char str[2][N]; int main() { int n, k; scanf("%d%d", &n, &k); scanf("%s%s", str[0], str[1]); queue<pair<int, int>> q; q.push({0, 0}); for (int o = 0; o < 2; o++) { for (int i = 0; i < n; i++) { dis[o][i] = i + 1; if (str[o][i] == 'X') dis[o][i] = 0; } } dis[0][0] = 0; while (!q.empty()) { int o, x; tie(o, x) = q.front(); q.pop(); if (x == n - 1) { cout << "YES" << endl; return 0; } if (dis[o][x + 1] > dis[o][x] + 1) { dis[o][x + 1] = dis[o][x] + 1; q.push({o, x + 1}); } if (x > 0 && dis[o][x - 1] > dis[o][x] + 1) { dis[o][x - 1] = dis[o][x] + 1; q.push({o, x - 1}); } if (x + k > n - 1) { cout << "YES" << endl; return 0; } if (dis[o ^ 1][x + k] > dis[o][x] + 1) { dis[o ^ 1][x + k] = dis[o][x] + 1; q.push({o ^ 1, x + k}); } } cout << "NO" << endl; return 0; }
8
CPP
n, k = map(int, input().split()) lzid = input() dzid = input() zidovi = [lzid, dzid] q = [[-1, [False,0]]] #[koraci, [zid, visina]] izasao = 0 bio = [[0 for i in range(n+k+100)], [0 for i in range(n+k+100)]] while len(q) != 0: trenutni = q.pop(0) korak = trenutni[0] pozicija = trenutni[1] tren_zid = pozicija[0] tren_visina = pozicija[1] if bio[tren_zid][tren_visina] == 0: bio[tren_zid][tren_visina] = 1 if tren_visina > n-1: print("YES") izasao = 1 break elif tren_visina == n-1 and zidovi[tren_zid][tren_visina] != 'X' and tren_visina > korak: print("YES") izasao = 1 break elif zidovi[tren_zid][tren_visina] != 'X' and tren_visina > korak: q.append([korak+1, [tren_zid, tren_visina-1]]) q.append([korak+1, [tren_zid, tren_visina+1]]) q.append([korak+1, [not(tren_zid), tren_visina+k]]) ## if tren_visina - 1 > korak+1: ## if zidovi[tren_zid][tren_visina-1] != 'X': ## q.append([korak+1, [tren_zid, tren_visina-1]]) ## if tren_visina + 1 > korak: ## if tren_visina + k <= n-1: ## if zidovi[tren_zid][tren_visina+1] != 'X': ## q.append([korak+1, [tren_zid, tren_visina+1]]) ## else: ## print("YES") ## izasao = 1 ## break ## if tren_visina + k > korak: ## if tren_visina + k <= n-1: ## if zidovi[not(tren_zid)][tren_visina+k] != 'X': ## q.append([korak+1, [not(tren_zid), tren_visina+k]]) ## else: ## print("YES") ## izasao = 1 if izasao == 0: print("NO")
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n, k; string s[3]; int mark[3][maxn]; void dfs(int v, int w, int b) { if (v > n - 1) { cout << "YES"; exit(0); } if (w >= v or v < 0 or mark[b][v] or s[b][v] == 'X') return; mark[b][v] = 1; dfs(v + k, w + 1, 3 - b); dfs(v + 1, w + 1, b); if (v > 0) { dfs(v - 1, w + 1, b); } } int main() { cin >> n >> k; cin >> s[1] >> s[2]; dfs(0, -1, 1); cout << "NO" << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct Node { int h; int tr; int x; }; bool tt[2][100000 + 1]; bool vis[2][100000 + 1]; int tout = 100000 * 2; int n, k; deque<Node> dq; int main() { string line; cin >> n >> k; memset(tt, 1, sizeof tt); getline(cin, line); for (int cc = 0, _n = 2; cc < _n; cc++) { getline(cin, line); for (int i = 0, _n = n; i < _n; i++) if (line[i] == 'X') tt[cc][i + 1] = false; } dq.push_back((Node){0, 0, 1}); while (!dq.empty()) { Node top = (Node)dq.front(); dq.pop_front(); if (vis[top.tr][top.x]) continue; vis[top.tr][top.x] = true; if (top.x <= top.h) continue; bool ok = false; if (top.x + 1 > n) { tout = min(tout, top.h + 1); ok = true; } if (top.x + k > n) { tout = min(tout, top.h + 1); ok = true; } if (ok) continue; int x = top.x, h = top.h, tr = top.tr; if (x + 1 <= n && tt[tr][x + 1]) dq.push_back((Node){h + 1, tr, x + 1}); if (x + k <= n && tt[(tr + 1) % 2][x + k]) dq.push_back((Node){h + 1, (tr + 1) % 2, x + k}); if (x - 1 >= 1 && tt[tr][x - 1]) dq.push_back((Node){h + 1, tr, x - 1}); } if (tout <= n) cout << "YES\n"; else cout << "NO\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000; const int INF = 1 << 30; char ss[2][MAX_N + 4]; int ds[2][MAX_N]; inline void check(int i, int j, int d, queue<pair<int, int> > &q) { if (ss[i][j] == '-' && ds[i][j] > d) { ds[i][j] = d; q.push(pair<int, int>(i, j)); } } int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < 2; i++) { scanf("%s", ss[i]); fill(ds[i], ds[i] + n, INF); } ds[0][0] = 0; queue<pair<int, int> > q; q.push(pair<int, int>(0, 0)); while (!q.empty()) { pair<int, int> u = q.front(); q.pop(); int &ui = u.first, &uj = u.second; int vd = ds[ui][uj] + 1; int vi, vj; vi = ui, vj = uj + 1; if (vj >= n) { puts("YES"); return 0; } check(vi, vj, vd, q); vi = ui, vj = uj - 1; if (vd <= vj) check(vi, vj, vd, q); vi = ui ^ 1, vj = uj + k; if (vj >= n) { puts("YES"); return 0; } check(vi, vj, vd, q); } puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAX_N = 100000 + 7; struct Node { int t, x, lv; Node() {} Node(int _t, int _x, int _lv) : t(_t), x(_x), lv(_lv) {} bool operator<(const Node &rhs) const { return lv < rhs.lv; } }; int dp[2][MAX_N]; char S[2][MAX_N]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < 2; i++) scanf("%s", S[i]); priority_queue<Node, vector<Node>, less<Node> > pq; memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; pq.push(Node(0, 0, 0)); while (!pq.empty()) { int t, x, lv; tie(t, x, lv) = tie(pq.top().t, pq.top().x, pq.top().lv); pq.pop(); if (lv + 1 == n || lv + k >= n) { puts("YES"); return 0; } if (S[x][lv + 1] == '-') { if (dp[x][lv + 1] > dp[x][lv] + 1) { dp[x][lv + 1] = dp[x][lv] + 1; pq.push(Node(dp[x][lv + 1], x, lv + 1)); } } if (lv - 1 >= 0 && S[x][lv - 1] == '-') { if (dp[x][lv] + 1 < lv) { if (dp[x][lv - 1] > dp[x][lv] + 1) { dp[x][lv - 1] = dp[x][lv] + 1; pq.push(Node(dp[x][lv - 1], x, lv - 1)); } } } if (S[x ^ 1][lv + k] == '-') { if (dp[x ^ 1][lv + k] > dp[x][lv] + 1) { dp[x ^ 1][lv + k] = dp[x][lv] + 1; pq.push(Node(dp[x ^ 1][lv + k], x ^ 1, lv + k)); } } } puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> char A[210000][3]; int n, v, flag = 0; struct node { int yer; int duvar; int su; struct node *nxt, *prv; } * front, *back; void cikar() { struct node *k; k = front->prv; free(front); front = k; if (k) k->nxt = NULL; } void ekle(int a, int b, int s) { struct node *k; k = (struct node *)malloc(1 * sizeof(struct node)); k->yer = a; k->duvar = b; k->su = s; k->prv = NULL; if (!front) { front = k; back = k; k->nxt = NULL; } else { back->prv = k; k->nxt = back; back = k; } return; } void Do_it() { int a, b, s; while (front) { a = front->yer; b = front->duvar; s = front->su; cikar(); if (a > n && flag == 0) { printf("YES"); flag++; break; } if (A[a][b] == '-' && a > s) A[a][b] = 's'; else continue; if (A[a + 1][b] != 'X' && A[a + 1][b] != 's' && a + 1 > s) ekle(a + 1, b, s + 1); if (A[a - 1][b] != 'X' && A[a - 1][b] != 's' && a - 1 > s) ekle(a - 1, b, s + 1); if (b == 1) { if (A[a + v][2] != 'X' && A[a + v][2] != 's' && a + v > s) ekle(a + v, 2, s + 1); } else if (A[a + v][1] != 'X' && A[a + v][1] != 's' && a + v > s) ekle(a + v, 1, s + 1); } return; } int main() { int i, j; char l; A[0][1] = 's'; A[0][2] = 's'; scanf("%d%d", &n, &v); for (j = 1; j <= 2; j++) { for (i = 1; i <= n; i++) { scanf(" %c", &l); A[i][j] = l; } } ekle(1, 1, 0); Do_it(); if (flag == 0) printf("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; string L, R; int dis[100005][2], n, k; queue<pair<int, int> > q; int main() { cin >> n >> k; cin >> L >> R; for (int i = 0; i < n; i++) { dis[i][0] = dis[i][1] = INT_MAX; } dis[0][0] = 0; q.push(make_pair(0, 0)); while (!q.empty()) { pair<int, int> x = q.front(); q.pop(); int ndis = dis[x.first][x.second]; if (x.first + k >= n) { cout << "YES\n"; return 0; } if ((x.second == 0 && L[x.first + 1] == '-') || (x.second == 1 && R[x.first + 1] == '-')) { if (dis[x.first + 1][x.second] == INT_MAX) { dis[x.first + 1][x.second] = ndis + 1; q.push(make_pair(x.first + 1, x.second)); } } if (x.first - 1 >= ndis + 1) { if ((x.second == 0 && L[x.first - 1] == '-') || (x.second == 1 && R[x.first - 1] == '-')) { if (dis[x.first - 1][x.second] == INT_MAX) { dis[x.first - 1][x.second] = ndis + 1; q.push(make_pair(x.first - 1, x.second)); } } } if ((x.second == 0 && R[x.first + k] == '-') || (x.second == 1 && L[x.first + k] == '-')) { if (dis[x.first + k][1 - x.second] == INT_MAX) { dis[x.first + k][1 - x.second] = ndis + 1; q.push(make_pair(x.first + k, 1 - x.second)); } } } cout << "NO\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxint = -1u >> 1; const double pi = 3.14159265358979323; const double eps = 1e-8; int n, k; string s[2]; int f[101000][2]; bool vis[101000][2]; bool spfa() { memset(vis, false, sizeof(vis)); for (int i = (0); i < (n + 5); i++) f[i][0] = f[i][1] = maxint; f[0][0] = 0; queue<pair<int, int> > que; que.push(make_pair(0, 0)); while (!que.empty()) { pair<int, int> now = que.front(); que.pop(); int p = now.first, id = now.second; vis[p][id] = false; if (p + 1 >= n || (p + 1 < n && s[id][p + 1] != 'X' && f[p][id] + 1 <= p + 1)) { if (p + 1 >= n) return true; if (f[p + 1][id] > f[p][id] + 1) { f[p + 1][id] = f[p][id] + 1; if (!vis[p + 1][id]) { vis[p + 1][id] = true; que.push(make_pair(p + 1, id)); } } } if (p - 1 >= 0 && s[id][p - 1] != 'X' && f[p][id] + 1 <= p - 1) { if (f[p - 1][id] > f[p][id] + 1) { f[p - 1][id] = f[p][id] + 1; if (!vis[p - 1][id]) { vis[p - 1][id] = true; que.push(make_pair(p - 1, id)); } } } if (p + k >= n || (p + k < n && s[id ^ 1][p + k] != 'X' && f[p][id] + 1 <= p + k)) { if (p + k >= n) return true; if (f[p + k][id ^ 1] > f[p][id] + 1) { f[p + k][id ^ 1] = f[p][id] + 1; if (!vis[p + k][id ^ 1]) { vis[p + k][id ^ 1] = true; que.push(make_pair(p + k, id ^ 1)); } } } } return false; } int main() { ios::sync_with_stdio(false); cin >> n >> k; cin >> s[0]; cin >> s[1]; s[0] += "-"; s[1] += "-"; bool ans = spfa(); if (ans) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool vis[2][100005]; char c[2][100005]; int main() { queue<pair<int, pair<int, int> > > q; bool flag = 0; int n, k; cin >> n >> k; scanf("%s%s", c[0], c[1]); q.push(make_pair(0, make_pair(0, 0))); while (!q.empty()) { int x = q.front().first, y = q.front().second.first, de = q.front().second.second; q.pop(); if (y >= n) { flag = 1; break; } if (vis[x][y] || de > y) continue; vis[x][y] = 1; if (y - 1 >= 0 && c[x][y - 1] != 'X' && y - 1 >= de + 1 && !vis[x][y - 1]) { q.push(make_pair(x, make_pair(y - 1, de + 1))); } if (y + 1 >= n || (c[x][y + 1] != 'X' && !vis[x][y + 1])) { q.push(make_pair(x, make_pair(y + 1, de + 1))); } if (y + k >= n || (c[x ^ 1][y + k] != 'X' && !vis[x ^ 1][y + k])) { q.push(make_pair(x ^ 1, make_pair(y + k, de + 1))); } } if (flag) cout << "YES"; else cout << "NO"; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int main() { string a[2]; int n, k; cin >> n >> k >> a[0] >> a[1]; int d[2][100010]; for (int i = 0; i < 2; i++) for (int j = 0; j < 100010; j++) d[i][j] = 1e9; queue<int> whi, high; whi.push(0); high.push(0); d[0][0] = 0; while (!whi.empty()) { int x = whi.front(), y = high.front(); whi.pop(); high.pop(); if (d[x][y] > y) continue; if (y + k >= n) { puts("YES"); return 0; } if ((a[x][y + 1] == '-') && (d[x][y + 1] == 1e9)) { d[x][y + 1] = d[x][y] + 1; whi.push(x); high.push(y + 1); } if ((y > 0) && (a[x][y - 1] == '-') && (d[x][y - 1] == 1e9)) { d[x][y - 1] = d[x][y] + 1; whi.push(x); high.push(y - 1); } if ((a[x ^ 1][y + k] == '-') && (d[x ^ 1][y + k] > (d[x][y] + 1))) { d[x ^ 1][y + k] = d[x][y] + 1; whi.push(x ^ 1); high.push(y + k); } } puts("NO"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; bool visited[2][maxn]; int d[2][maxn]; string s[2]; int main() { int n, k; cin >> n >> k; cin >> s[0] >> s[1]; int dx[3] = {0, 0, 1}; int dy[3] = {1, -1, k}; queue<pair<bool, int>> q; q.push(make_pair(0, 0)); visited[0][0] = 1; while (q.size()) { pair<bool, int> k = q.front(); q.pop(); bool x = k.first; int y = k.second; for (int i = 0; i < 3; i++) { bool xx = x ^ dx[i]; int yy = y + dy[i]; if (yy >= n) { cout << "YES" << endl; exit(0); } if (yy >= 0 && s[xx][yy] == '-' && !visited[xx][yy] && yy > d[x][y]) { q.push(make_pair(xx, yy)); visited[xx][yy] = 1; d[xx][yy] = d[x][y] + 1; } } } cout << "NO" << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; bool vl[100010], vr[100010]; int main() { int n, k; cin >> n >> k; queue<pair<pair<int, int>, bool> > q; string s1, s2; cin >> s1 >> s2; q.push(make_pair(make_pair(0, 0), true)); vl[0] = true; while (!q.empty()) { pair<pair<int, int>, bool> p = q.front(); q.pop(); if (p.second) { if (p.first.first + k >= n) { cout << "YES" << endl; return 0; } if (s1[p.first.first + 1] == '-' && !vl[p.first.first + 1]) { q.push( make_pair(make_pair(p.first.first + 1, p.first.second + 1), true)); vl[p.first.first + 1] = true; } if (s1[p.first.first - 1] == '-' && p.first.first - 1 >= p.first.second + 1 && !vl[p.first.first - 1]) { q.push( make_pair(make_pair(p.first.first - 1, p.first.second + 1), true)); vl[p.first.first - 1] = true; } if (s2[p.first.first + k] == '-' && !vr[p.first.first + k]) { q.push( make_pair(make_pair(p.first.first + k, p.first.second + 1), false)); vr[p.first.first + k] = true; } } else { if (p.first.first + k >= n) { cout << "YES" << endl; return 0; } if (s2[p.first.first + 1] == '-' && !vr[p.first.first + 1]) { q.push( make_pair(make_pair(p.first.first + 1, p.first.second + 1), false)); vr[p.first.first + 1] = true; } if (s2[p.first.first - 1] == '-' && p.first.first - 1 >= p.first.second + 1 && !vr[p.first.first - 1]) { q.push( make_pair(make_pair(p.first.first - 1, p.first.second + 1), false)); vr[p.first.first - 1] = true; } if (s1[p.first.first + k] == '-' && !vl[p.first.first + k]) { q.push( make_pair(make_pair(p.first.first + k, p.first.second + 1), true)); vl[p.first.first + k] = true; } } } cout << "NO" << endl; }
8
CPP