ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 1525 ํผ์ฆ with Python
    PS 2022. 6. 14. 16:47
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 1525 ํผ์ฆ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. 3ร—3 ํ‘œ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜๊ฐ€ ์ฑ„์›Œ์ ธ ์žˆ๋‹ค. ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ๊ฐ€์žฅ ๋ ์นธ์€ ๋น„์–ด ์žˆ๋Š” ์นธ์ด๋‹ค.
      1 2 3
      4 5 6
      7 8
    1. ์–ด๋–ค ์ˆ˜์™€ ์ธ์ ‘ํ•ด ์žˆ๋Š” ๋„ค ๊ฐœ์˜ ์นธ ์ค‘์— ํ•˜๋‚˜๊ฐ€ ๋น„์–ด ์žˆ์œผ๋ฉด, ์ˆ˜๋ฅผ ๊ทธ ์นธ์œผ๋กœ ์ด๋™์‹œํ‚ฌ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.
      ๋ฌผ๋ก  ํ‘œ ๋ฐ”๊นฅ์œผ๋กœ ๋‚˜๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
      ์šฐ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” ์ดˆ๊ธฐ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ตœ์†Œ์˜ ์ด๋™์œผ๋กœ ์œ„์™€ ๊ฐ™์€ ์ •๋ฆฌ๋œ ์ƒํƒœ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ๋‹ค์Œ์˜ ์˜ˆ๋ฅผ ๋ณด์ž.
      1 3
      4 2 5
      7 8 6

    1 2 3
    4 5
    7 8 6

    1 2 3
    4 5
    7 8 6

    1 2 3
    4 5 6
    7 8

    1. ๊ฐ€์žฅ ์œ— ์ƒํƒœ์—์„œ ์„ธ ๋ฒˆ์˜ ์ด๋™์„ ํ†ตํ•ด ์ •๋ฆฌ๋œ ์ƒํƒœ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ด์™€ ๊ฐ™์ด ์ตœ์†Œ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
    1. ์„ธ ์ค„์— ๊ฑธ์ณ์„œ ํ‘œ์— ์ฑ„์›Œ์ ธ ์žˆ๋Š” ์•„ํ™‰ ๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ํ•œ ์ค„์— ์„ธ ๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, ๋นˆ ์นธ์€ 0์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    1. ์ฒซ์งธ ์ค„์— ์ตœ์†Œ์˜ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ด๋™์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
    1. BFS, ๊ทธ๋ž˜ํ”„ํƒ์ƒ‰ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from collections import deque
    from sys import stdin
    
    arr = []
    c = [1, 2, 3, 4, 5, 6, 7, 8, 0]
    x, y = 0, 0
    for i in range(3):
        data = list(map(int, stdin.readline().split()))
        arr.append(data)
        for j in range(3):
            if data[j] == 0:
                x, y = i, j
    
    dx, dy = [1, 0, 0, -1], [0, 1, -1, 0]
    
    
    def swap(a, b): return b, a
    
    
    def solve(x, y):
        q = deque()
        temp = []
        for p in arr:
            temp.extend(p)
    
        q.append((0, x, y, temp))
        visited = set()
    
        while q:
            cnt, x, y, check_list = q.popleft()
            # visited.discard(tuple(check_list))
            if check_list == c:
                return cnt
    
            else:
                temp = [check_list[idx:idx + 3] for idx in range(0, 7, 3)]
                for i in range(4):
                    nx, ny = x + dx[i], y + dy[i]
                    if 0 <= nx < 3 and 0 <= ny < 3:
    
                        temp[x][y], temp[nx][ny] = swap(temp[x][y], temp[nx][ny])
    
                        concat_list = []
                        for i in temp:
                            concat_list.extend(i)
    
                        if tuple(concat_list) not in visited:
                            visited.add(tuple(concat_list))
                            q.append((cnt + 1, nx, ny, concat_list))
                        temp[x][y], temp[nx][ny] = swap(temp[x][y], temp[nx][ny])
    
        return -1
    
    
    print(solve(x, y))

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    1 0 3
    4 2 5
    7 8 6

    ์‹คํ–‰๊ฒฐ๊ณผ

    3

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ํผ์ฆ์˜ ์ˆซ์ž๋ฅผ ์˜ฎ๊ธฐ๋ฉด์„œ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๊ณ , ์ •๋ ฌ๋œ ์ƒํƒœ๊ฐ€ ํ™•์ธ๋˜์—ˆ์œผ๋ฉด cnt๋ฅผ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.
    1. ์ตœ์†Œ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ ค๊ณ  ํ–ˆ์œผ๋‚˜, ์ด๋™์„ ํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” -1๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.