Skip to content

Commit 15495e8

Browse files
Add files via upload
1 parent 380c213 commit 15495e8

File tree

1 file changed

+143
-7
lines changed

1 file changed

+143
-7
lines changed

chpt5_dfs_bfs_review.ipynb

+143-7
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
},
200200
{
201201
"cell_type": "code",
202-
"execution_count": 28,
202+
"execution_count": 4,
203203
"id": "f5b7ce69",
204204
"metadata": {},
205205
"outputs": [
@@ -212,7 +212,8 @@
212212
"0 0 0 1 1\n",
213213
"1 1 1 1 1\n",
214214
"0 0 0 0 0\n",
215-
"1\n"
215+
"3\n",
216+
"[[True, True, False, False, True], [True, True, True, False, False], [False, False, False, False, False], [True, True, True, True, True]]\n"
216217
]
217218
}
218219
],
@@ -221,9 +222,9 @@
221222
"\n",
222223
"n, m = map(int, input().split())\n",
223224
"\n",
224-
"graph = []\n",
225+
"array = []\n",
225226
"for i in range(n):\n",
226-
" graph.append(list(map(int, input().split())))\n",
227+
" array.append(list(map(int, input().split())))\n",
227228
"\n",
228229
"visited = [[False]*m for _ in range(n)]\n",
229230
"\n",
@@ -234,7 +235,7 @@
234235
" if x<0 or y <0 or x>=n or y>=m:\n",
235236
" return False\n",
236237
" \n",
237-
" if visited[x][y] == False:\n",
238+
" if array[x][y]==0 and visited[x][y] == False:\n",
238239
" visited[x][y] = True\n",
239240
" dfs(array, x-1, y)\n",
240241
" dfs(array, x, y-1)\n",
@@ -254,14 +255,149 @@
254255
" \n",
255256
" \n",
256257
"print(count)\n",
258+
"print(visited)\n",
259+
"# 다시 한 번 dfs로 구현해보기! 재귀함수 사용해서.\n",
260+
"# -> solve!!"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": 19,
266+
"id": "3b3a520d",
267+
"metadata": {},
268+
"outputs": [
269+
{
270+
"name": "stdout",
271+
"output_type": "stream",
272+
"text": [
273+
"5 6\n",
274+
"101010\n",
275+
"111111\n",
276+
"000001\n",
277+
"111111\n",
278+
"111111\n",
279+
"10\n"
280+
]
281+
}
282+
],
283+
"source": [
284+
"# 8-4 / 실전 문제 / 미로 탈출 written by me!\n",
285+
"\n",
286+
"from collections import deque\n",
287+
"\n",
288+
"queue = deque()\n",
289+
"\n",
290+
"n, m = map(int, input().split())\n",
291+
"array = []\n",
292+
"\n",
293+
"value = [[-1]*m for _ in range(n)]\n",
294+
"\n",
295+
"for i in range (n):\n",
296+
" array.append(list(input()))\n",
297+
"\n",
298+
"for i in range(n):\n",
299+
" for j in range(m):\n",
300+
" array[i][j] = int(array[i][j])\n",
301+
" \n",
302+
"x, y = 0, 0\n",
303+
"dx = [0, -1, 0, 1]\n",
304+
"dy = [-1, 0, 1, 0]\n",
305+
"\n",
306+
"queue.append((x, y))\n",
307+
"value[x][y] = 1\n",
308+
"while queue:\n",
309+
" x, y = queue.popleft()\n",
310+
" \n",
311+
" for i in range(4):\n",
312+
" nx = x + dx[i]\n",
313+
" ny = y + dy[i]\n",
314+
" \n",
315+
" # 첫 번째 조건문 : 범위를 벗어나거나 괴물이 있어 이동할 수 없는 경우\n",
316+
" \n",
317+
" if nx < 0 or ny < 0 or nx >= n or ny >= m or array[nx][ny] == 0:\n",
318+
" continue\n",
319+
" \n",
320+
" # 두 번째 조건문 : 이동할 수 있는 곳이나, 현재 노드에서 이동한 값보다 큰 경우(다른 먼 경로로 돌아온 경우)\n",
321+
" \n",
322+
" if value[nx][ny] != -1 and value[nx][ny] > value[x][y]+1:\n",
323+
" value[nx][ny] = value[x][y] + 1\n",
324+
" \n",
325+
" # 세 번째 조건문 : 방문하지 않은 노드인 경우\n",
326+
" \n",
327+
" elif value[nx][ny] == -1:\n",
328+
" value[nx][ny] = value[x][y] + 1\n",
329+
" \n",
330+
" else: continue\n",
331+
" queue.append((nx, ny))\n",
332+
"\n",
333+
"\n",
334+
"print(value[n-1][m-1])\n",
335+
"\n",
336+
"# solve!"
337+
]
338+
},
339+
{
340+
"cell_type": "code",
341+
"execution_count": 20,
342+
"id": "baa1f976",
343+
"metadata": {},
344+
"outputs": [
345+
{
346+
"name": "stdout",
347+
"output_type": "stream",
348+
"text": [
349+
"5 6\n",
350+
"101010\n",
351+
"111111\n",
352+
"000001\n",
353+
"111111\n",
354+
"111111\n",
355+
"10\n"
356+
]
357+
}
358+
],
359+
"source": [
360+
"# 5-4 / written by author!\n",
361+
"\n",
362+
"from collections import deque\n",
363+
"\n",
364+
"n, m = map(int, input().split())\n",
365+
"\n",
366+
"graph = []\n",
367+
"\n",
368+
"for i in range(n):\n",
369+
" graph.append(list(map(int, input()))) # split을 안쓰면 그냥 나눠 주는 거였음! 내가 했던 것처럼 미련하게 하지 말자\n",
370+
" \n",
371+
"dx = [-1, 1, 0, 0]\n",
372+
"dy = [0, 0, -1, 1]\n",
373+
"\n",
374+
"def bfs(x, y):\n",
375+
" queue = deque()\n",
376+
" queue.append((x, y))\n",
377+
" \n",
378+
" while queue:\n",
379+
" x, y = queue.popleft()\n",
380+
" for i in range(4):\n",
381+
" nx = x + dx[i]\n",
382+
" ny = y + dy[i]\n",
383+
" \n",
384+
" if nx < 0 or ny < 0 or nx >= n or ny >= m:\n",
385+
" continue\n",
386+
" if graph[nx][ny] == 0:\n",
387+
" continue\n",
388+
" if graph[nx][ny] == 1:\n",
389+
" graph[nx][ny] = graph[x][y]+1\n",
390+
" queue.append((nx, ny))\n",
391+
" \n",
392+
" return graph[n-1][m-1]\n",
257393
"\n",
258-
"# 다시 한 번 dfs로 구현해보기! 재귀함수 사용해서."
394+
"print(bfs(0, 0))"
259395
]
260396
},
261397
{
262398
"cell_type": "code",
263399
"execution_count": null,
264-
"id": "3b3a520d",
400+
"id": "44fe590d",
265401
"metadata": {},
266402
"outputs": [],
267403
"source": []

0 commit comments

Comments
 (0)