File tree Expand file tree Collapse file tree 3 files changed +62
-0
lines changed
Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+
4+ int N, k;
5+ string arr[2 ];
6+
7+ bool OOB (int rx, int ry, int t)
8+ {
9+ if (0 <= rx && rx < 2 && t < ry && ry < N)
10+ return true ;
11+ return false ;
12+ }
13+ int bfs ()
14+ {
15+ queue<pair<pair<int , int >, int >> q;
16+ int dx[] = {0 , 0 , 1 , -1 };
17+ int dy[] = {1 , -1 , k, k};
18+
19+
20+ bool visited[2 ][200001 ];
21+ fill (&visited[0 ][0 ], &visited[1 ][200000 ], false );
22+
23+ q.push ({{0 , 0 }, 0 });
24+ visited[0 ][0 ] = true ;
25+
26+ while (!q.empty ()){
27+ int x = q.front ().first .first , y = q.front ().first .second ;
28+ int time = q.front ().second ;
29+ q.pop ();
30+ if (time >= N)
31+ break ;
32+
33+ for (int i = 0 ; i < 4 ;i++){
34+ int rx = x + dx[i];
35+ int ry = y + dy[i];
36+ if (ry >= N)
37+ return 1 ;
38+ if (OOB (rx, ry, time)){
39+ if (arr[rx][ry] == ' 1' && !visited[rx][ry] )
40+ {
41+ visited[rx][ry] = true ;
42+ q.push ({{rx, ry}, time + 1 });
43+ }
44+ }
45+ }
46+ }
47+ return 0 ;
48+ }
49+
50+ int main ()
51+ {
52+
53+ ios::sync_with_stdio (0 );
54+ cin.tie (0 );
55+
56+ cin >> N >> k;
57+ cin >> arr[0 ];
58+ cin >> arr[1 ];
59+ cout << bfs ();
60+
61+ return 0 ;
62+ }
You can’t perform that action at this time.
0 commit comments