Skip to content

Commit da02209

Browse files
committed
Python Algorithm
1 parent a182be5 commit da02209

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

.idea/workspace.xml

+13-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SWEA_Python/1227-bfs.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 미로 2
2+
from collections import deque
3+
4+
5+
def bfs(x, y):
6+
global ans
7+
queue.append([x, y])
8+
visited[x][y] = True
9+
10+
while queue:
11+
q = queue.popleft()
12+
for i in range(4):
13+
nx, ny = q[0] + dx[i], q[1] + dy[i]
14+
if 0 <= ny < 100 and 0 <= nx < 100 and not visited[nx][ny] and graph[nx][ny] != 1:
15+
if graph[nx][ny] == 3:
16+
ans = 1
17+
return
18+
visited[nx][ny] = True
19+
queue.append([nx, ny])
20+
21+
22+
for _ in range(1,11):
23+
t = int(input())
24+
graph = list()
25+
26+
for i in range(100):
27+
line = list(map(int, list(input().strip())))
28+
graph.append(line)
29+
30+
visited = [[False] * 100 for _ in range(100)]
31+
ans = 0
32+
33+
queue = deque()
34+
bfs(1, 1)
35+
print(f'#{t} {ans}')

SWEA_Python/1227-dfs.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 미로 2
2+
import sys
3+
sys.setrecursionlimit()
4+
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
5+
6+
7+
def dfs(x, y):
8+
global ans
9+
visited[x][y] = True
10+
if ans: return
11+
12+
for i in range(4):
13+
nx, ny = x + dx[i], y + dy[i]
14+
if 0 <= nx < 100 and 0 <= ny < 100:
15+
if not visited[nx][ny] and graph[nx][ny] != 1:
16+
visited[nx][ny] = True
17+
if graph[nx][ny] == 3:
18+
ans = 1
19+
dfs(nx, ny)
20+
21+
22+
for _ in range(1,11):
23+
t = int(input())
24+
graph = list()
25+
26+
for i in range(100):
27+
line = list(map(int, list(input().strip())))
28+
graph.append(line)
29+
30+
visited = [[False] * 100 for _ in range(100)]
31+
ans = 0
32+
dfs(1, 1)
33+
print(f'#{t} {ans}')

0 commit comments

Comments
 (0)