diff --git a/_WeeklyChallenges/W24-[BFS+Implementation]/Assignment_BOJ_11559_Puyo Puyo.py b/_WeeklyChallenges/W24-[BFS+Implementation]/Assignment_BOJ_11559_Puyo Puyo.py new file mode 100644 index 0000000..e8f56c4 --- /dev/null +++ b/_WeeklyChallenges/W24-[BFS+Implementation]/Assignment_BOJ_11559_Puyo Puyo.py @@ -0,0 +1,66 @@ +""" +BOJ #11559. Puyo Puyo (κ³¨λ“œ 4) +https://www.acmicpc.net/problem/11559 +μœ ν˜•: BFS + Implementation +""" + +from collections import deque +from sys import stdin +input = stdin.readline + +EMPTY = '.' +dx = (1, -1, 0, 0) +dy = (0, 0, 1, -1) + + +def delete_block(): + for x, y in blocks: + board[x][y] = EMPTY + + +def update_board(): + for y in range(6): + for t in range(10, -1, -1): + for x in range(11, t, -1): + if board[x][y] == EMPTY and board[t][y] != EMPTY: + board[x][y], board[t][y] = board[t][y], EMPTY + + +def is_in_area(x, y): + return 0 <= x < 12 and 0 <= y < 6 + + +def bfs(x, y): + queue = deque([(x, y)]) + visited[x][y] = True + same_blocks = [(x, y)] + while queue: + x, y = queue.popleft() + for d in range(4): + nx = x + dx[d] + ny = y + dy[d] + if is_in_area(nx, ny) and not visited[nx][ny] and board[x][y] == board[nx][ny]: + queue.append((nx, ny)) + visited[nx][ny] = True + same_blocks.append((nx, ny)) + return same_blocks + + + +board = [[*input().rstrip()] for _ in range(12)] +flag = True +res = 0 +while flag: + flag = False + visited = [[False] * 6 for _ in range(12)] + for i in range(12): + for j in range(6): + if board[i][j] != EMPTY and not visited[i][j]: + blocks = bfs(i, j) + if len(blocks) >= 4: + flag = True + delete_block() + if flag: + update_board() + res += 1 +print(res) \ No newline at end of file diff --git a/_WeeklyChallenges/W24-[BFS+Implementation]/README.md b/_WeeklyChallenges/W24-[BFS+Implementation]/README.md new file mode 100644 index 0000000..bba32f0 --- /dev/null +++ b/_WeeklyChallenges/W24-[BFS+Implementation]/README.md @@ -0,0 +1,16 @@ +## πŸš€5μ›” 4μ£Όμ°¨ (5/26) μŠ€ν„°λ”” 발제 주제: BFS + Implementation +> 발제자: μ‘°μœ€μƒ (@YoonYn9915) + +> [!NOTE] +> 주제: BFS + Implementation + +### πŸ—‚οΈ μŠ€ν„°λ”” 자료 +- PDF: [λ°”λ‘œκ°€κΈ°](Study_BOJ_16509.pdf) + +### πŸ“– 문제 +- [λ°±μ€€ #16509. μž₯κ΅°](https://www.acmicpc.net/problem/16509): BFS + Implementation / κ³¨λ“œ5 +- μ •λ‹΅ μ½”λ“œ: [Study_BOJ_16509_μž₯κ΅°.py](Study_BOJ_16509_μž₯κ΅°.py) + +### πŸ’» 과제 +- [λ°±μ€€ #11559. Puyo Puyo](https://www.acmicpc.net/problem/11559): BFS + Implementation/ κ³¨λ“œ4 +- μ •λ‹΅ μ½”λ“œ: [Assignment_BOJ_11559_Puyo Puyo.py](Assignment_BOJ_11559_Puyo Puyo.py) diff --git a/_WeeklyChallenges/W24-[BFS+Implementation]/Study_BOJ_16509.pdf b/_WeeklyChallenges/W24-[BFS+Implementation]/Study_BOJ_16509.pdf new file mode 100644 index 0000000..48f774e Binary files /dev/null and b/_WeeklyChallenges/W24-[BFS+Implementation]/Study_BOJ_16509.pdf differ diff --git "a/_WeeklyChallenges/W24-[BFS+Implementation]/Study_BOJ_16509_\354\236\245\352\265\260.py" "b/_WeeklyChallenges/W24-[BFS+Implementation]/Study_BOJ_16509_\354\236\245\352\265\260.py" new file mode 100644 index 0000000..65dd6c6 --- /dev/null +++ "b/_WeeklyChallenges/W24-[BFS+Implementation]/Study_BOJ_16509_\354\236\245\352\265\260.py" @@ -0,0 +1,62 @@ +""" +BOJ #16509. μž₯κ΅° (κ³¨λ“œ 5) +https://www.acmicpc.net/problem/16509 +μœ ν˜•: BFS + Implementation +""" + +from collections import deque + +# μž…λ ₯ λ°›κΈ° +R1, C1 = map(int, input().split()) # μƒμ˜ μ‹œμž‘ μœ„μΉ˜ +R2, C2 = map(int, input().split()) # μ™•μ˜ μœ„μΉ˜ + +# λ°©λ¬Έ μ—¬λΆ€λ₯Ό -1둜 μ΄ˆκΈ°ν™” (10ν–‰ x 9μ—΄) +visited = [[-1] * 9 for _ in range(10)] +visited[R1][C1] = 0 # μ‹œμž‘ μœ„μΉ˜λŠ” 0으둜 ν‘œμ‹œ + +# μƒμ˜ 8κ°€μ§€ 이동 λ°©ν–₯ 및 그에 λ”°λ₯Έ 경유 μ’Œν‘œ μ •μ˜ +# 각각: 경유1, 경유2, μ΅œμ’… λͺ©μ μ§€ (총 3단계 이동) +paths = [ + [(-1, 0), (-2, -1), (-3, -2)], + [(-1, 0), (-2, 1), (-3, 2)], + [(1, 0), (2, -1), (3, -2)], + [(1, 0), (2, 1), (3, 2)], + [(0, -1), (-1, -2), (-2, -3)], + [(0, -1), (1, -2), (2, -3)], + [(0, 1), (-1, 2), (-2, 3)], + [(0, 1), (1, 2), (2, 3)], +] + +# 이동 κ²½λ‘œμ— 왕이 μžˆλŠ”μ§€ 확인 +def check(x, y, i): + for dx, dy in paths[i][:2]: # κ²½μœ μ§€ 두 곳만 확인 + mx, my = x + dx, y + dy + if mx == R2 and my == C2: + return False + return True + +# BFS둜 μ΅œμ†Œ 이동 횟수 μ°ΎκΈ° +def bfs(): + queue = deque([(R1, C1)]) + + while queue: + x, y = queue.popleft() + + # μ™•μ˜ μœ„μΉ˜μ— λ„λ‹¬ν•œ 경우 + if x == R2 and y == C2: + print(visited[x][y]) + return + + for i in range(8): + nx = x + paths[i][2][0] + ny = y + paths[i][2][1] + + # λ²”μœ„ μ•ˆμ΄κ³ , λ°©λ¬Έν•˜μ§€ μ•Šμ•˜μœΌλ©°, κ²½λ‘œμ— 왕이 μ—†λ‹€λ©΄ + if 0 <= nx < 10 and 0 <= ny < 9 and visited[nx][ny] == -1 and check(x, y, i): + visited[nx][ny] = visited[x][y] + 1 + queue.append((nx, ny)) + + # 도달할 수 μ—†λŠ” 경우 + print(-1) + +bfs() \ No newline at end of file