Skip to content

Commit aee2b99

Browse files
committed
Add recursion and bfs problem solution
1 parent 149d47c commit aee2b99

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sys
2+
sys.setrecursionlimit(10**9)
3+
input = sys.stdin.readline
4+
5+
def star(r, c, N):
6+
if N == 3:
7+
graph[r][c] = "*"
8+
graph[r+1][c-1] = "*"
9+
graph[r+1][c+1] = "*"
10+
for i in range(-2, 3):
11+
graph[r+2][c+i] = "*"
12+
else:
13+
n = N // 2
14+
star(r, c, n)
15+
star(r+n, c-n, n)
16+
star(r+n, c+n, n)
17+
18+
N = int(input())
19+
graph = [[' ']*(N*2) for _ in range(N)]
20+
star(0, N-1, N)
21+
for row in graph:
22+
print ("".join(row))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
def bfs(start):
6+
count = 0
7+
queue = deque([start])
8+
visited[start] = 1
9+
while queue:
10+
x = queue.popleft()
11+
if x == K:
12+
count += 1
13+
for nx in [x-1, x+1, x*2]:
14+
if 0<=nx<100001:
15+
if not visited[nx] or visited[nx] >= visited[x]+1: # I don't get it why post condition is required?
16+
visited[nx] = visited[x]+1
17+
queue.append(nx)
18+
return count
19+
20+
N, K = map(int, input().split())
21+
visited = [0] * (100_001)
22+
count = bfs(N)
23+
print(visited[K]-1) # because it was started from 1.
24+
print(count)

0 commit comments

Comments
 (0)