[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/python] ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 3์ฃผ์ฐจ : ํผ์ฆ ์กฐ๊ฐ ์ฑ„์šฐ๊ธฐ

2021. 8. 21. 11:41

๋ฌธ์ œ ์„ค๋ช…

ํ…Œ์ด๋ธ” ์œ„์— ๋†“์ธ ํผ์ฆ ์กฐ๊ฐ์„ ๊ฒŒ์ž„ ๋ณด๋“œ์˜ ๋นˆ ๊ณต๊ฐ„์— ์ ์ ˆํžˆ ์˜ฌ๋ ค๋†“์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๊ฒŒ์ž„ ๋ณด๋“œ์™€ ํ…Œ์ด๋ธ”์€ ๋ชจ๋‘ ๊ฐ ์นธ์ด 1x1 ํฌ๊ธฐ์ธ ์ •์‚ฌ๊ฐ ๊ฒฉ์ž ๋ชจ์–‘์ž…๋‹ˆ๋‹ค. ์ด๋•Œ, ๋‹ค์Œ ๊ทœ์น™์— ๋”ฐ๋ผ ํ…Œ์ด๋ธ” ์œ„์— ๋†“์ธ ํผ์ฆ ์กฐ๊ฐ์„ ๊ฒŒ์ž„ ๋ณด๋“œ์˜ ๋นˆ์นธ์— ์ฑ„์šฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

  • ์กฐ๊ฐ์€ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ์ฑ„์›Œ ๋„ฃ์Šต๋‹ˆ๋‹ค.
  • ์กฐ๊ฐ์„ ํšŒ์ „์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์กฐ๊ฐ์„ ๋’ค์ง‘์„ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • ๊ฒŒ์ž„ ๋ณด๋“œ์— ์ƒˆ๋กœ ์ฑ„์›Œ ๋„ฃ์€ ํผ์ฆ ์กฐ๊ฐ๊ณผ ์ธ์ ‘ํ•œ ์นธ์ด ๋น„์–ด์žˆ์œผ๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ํผ์ฆ ์กฐ๊ฐ์„ ์ฑ„์šฐ๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์œ„ ๊ทธ๋ฆผ์—์„œ ์™ผ์ชฝ์€ ํ˜„์žฌ ๊ฒŒ์ž„ ๋ณด๋“œ์˜ ์ƒํƒœ๋ฅผ, ์˜ค๋ฅธ์ชฝ์€ ํ…Œ์ด๋ธ” ์œ„์— ๋†“์ธ ํผ์ฆ ์กฐ๊ฐ๋“ค์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ํ…Œ์ด๋ธ” ์œ„์— ๋†“์ธ ํผ์ฆ ์กฐ๊ฐ๋“ค ๋˜ํ•œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ [์ƒ,ํ•˜,์ขŒ,์šฐ]๋กœ ์ธ์ ‘ํ•ด ๋ถ™์–ด์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์—†์œผ๋ฉฐ, ํฐ ์นธ์€ ํผ์ฆ์ด ๋†“์ด์ง€ ์•Š์€ ๋นˆ ๊ณต๊ฐ„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ชจ๋“  ํผ์ฆ ์กฐ๊ฐ์€ ๊ฒฉ์ž ์นธ์— ๋”ฑ ๋งž๊ฒŒ ๋†“์—ฌ์žˆ์œผ๋ฉฐ, ๊ฒฉ์ž ์นธ์„ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜, ๊ฑธ์ณ ์žˆ๋Š” ๋“ฑ ์ž˜๋ชป ๋†“์ธ ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.

์ด๋•Œ, ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 3,4,5๋ฒˆ ์กฐ๊ฐ์„ ๊ฒฉ์ž ์นธ์— ๋†“์œผ๋ฉด ๊ทœ์น™์— ์–ด๊ธ‹๋‚˜๋ฏ€๋กœ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

  • 3๋ฒˆ ์กฐ๊ฐ์„ ๋†“๊ณ  4๋ฒˆ ์กฐ๊ฐ์„ ๋†“๊ธฐ ์ „์— ์œ„์ชฝ์œผ๋กœ ์ธ์ ‘ํ•œ ์นธ์— ๋นˆ์นธ์ด ์ƒ๊น๋‹ˆ๋‹ค.
  • 5๋ฒˆ ์กฐ๊ฐ์˜ ์–‘ ์˜†์œผ๋กœ ์ธ์ ‘ํ•œ ์นธ์— ๋นˆ์นธ์ด ์ƒ๊น๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ๊ทœ์น™์— ๋งž๊ฒŒ ์ตœ๋Œ€ํ•œ ๋งŽ์€ ์กฐ๊ฐ์„ ๊ฒŒ์ž„ ๋ณด๋“œ์— ์ฑ„์›Œ ๋„ฃ์€ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

์ตœ๋Œ€ํ•œ ๋งŽ์€ ์กฐ๊ฐ์„ ์ฑ„์›Œ ๋„ฃ์œผ๋ฉด ์ด 14์นธ์„ ์ฑ„์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ˜„์žฌ ๊ฒŒ์ž„ ๋ณด๋“œ์˜ ์ƒํƒœ game_board, ํ…Œ์ด๋ธ” ์œ„์— ๋†“์ธ ํผ์ฆ ์กฐ๊ฐ์˜ ์ƒํƒœ table์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ทœ์น™์— ๋งž๊ฒŒ ์ตœ๋Œ€ํ•œ ๋งŽ์€ ํผ์ฆ ์กฐ๊ฐ์„ ์ฑ„์›Œ ๋„ฃ์„ ๊ฒฝ์šฐ, ์ด ๋ช‡ ์นธ์„ ์ฑ„์šธ ์ˆ˜ ์žˆ๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ

  • 3 ≤ game_board์˜ ํ–‰ ๊ธธ์ด ≤ 50
  • game_board์˜ ๊ฐ ์—ด ๊ธธ์ด = game_board์˜ ํ–‰ ๊ธธ์ด
    • ์ฆ‰, ๊ฒŒ์ž„ ๋ณด๋“œ๋Š” ์ •์‚ฌ๊ฐ ๊ฒฉ์ž ๋ชจ์–‘์ž…๋‹ˆ๋‹ค.
    • game_board์˜ ๋ชจ๋“  ์›์†Œ๋Š” 0 ๋˜๋Š” 1์ž…๋‹ˆ๋‹ค.
    • 0์€ ๋นˆ์นธ, 1์€ ์ด๋ฏธ ์ฑ„์›Œ์ง„ ์นธ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • ํผ์ฆ ์กฐ๊ฐ์ด ๋†“์ผ ๋นˆ์นธ์€ 1 x 1 ํฌ๊ธฐ ์ •์‚ฌ๊ฐํ˜•์ด ์ตœ์†Œ 1๊ฐœ์—์„œ ์ตœ๋Œ€ 6๊ฐœ๊นŒ์ง€ ์—ฐ๊ฒฐ๋œ ํ˜•ํƒœ๋กœ๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
  • table์˜ ํ–‰ ๊ธธ์ด = game_board์˜ ํ–‰ ๊ธธ์ด
  • table์˜ ๊ฐ ์—ด ๊ธธ์ด = table์˜ ํ–‰ ๊ธธ์ด
    • ์ฆ‰, ํ…Œ์ด๋ธ”์€ game_board์™€ ๊ฐ™์€ ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐ ๊ฒฉ์ž ๋ชจ์–‘์ž…๋‹ˆ๋‹ค.
    • table์˜ ๋ชจ๋“  ์›์†Œ๋Š” 0 ๋˜๋Š” 1์ž…๋‹ˆ๋‹ค.
    • 0์€ ๋นˆ์นธ, 1์€ ์กฐ๊ฐ์ด ๋†“์ธ ์นธ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • ํผ์ฆ ์กฐ๊ฐ์€ 1 x 1 ํฌ๊ธฐ ์ •์‚ฌ๊ฐํ˜•์ด ์ตœ์†Œ 1๊ฐœ์—์„œ ์ตœ๋Œ€ 6๊ฐœ๊นŒ์ง€ ์—ฐ๊ฒฐ๋œ ํ˜•ํƒœ๋กœ๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
  • game_board์—๋Š” ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋นˆ์นธ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • table์—๋Š” ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ธ”๋ก์ด ๋†“์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ •๋‹ต

# dfs ์ด์šฉํ•˜์—ฌ ์ด์ฐจ์› ๋ฆฌ์ŠคํŠธ์˜ ๋ธ”๋ก ๋ชจ์–‘ ๋ฆฌ์ŠคํŠธ ์ €์žฅ ํ•จ์ˆ˜
def dfs(i, j, board, visited, empty, n):
    section = []
    stack = [(i, j)]
    while stack:
        x, y = stack.pop()
        if x >= 0 and y >= 0:   # ์ธ๋ฑ์Šค ์ฒดํฌ
            try:
                if visited[x][y] == False and board[x][y] == n:
                    visited[x][y] = True
                    section.append((x,y))
                    
                    stack.append((x-1, y))
                    stack.append((x+1, y))
                    stack.append((x, y-1))
                    stack.append((x, y+1))
                    
            except IndexError:
                continue
    empty.append(sorted(section))

# ๋ธ”๋ก์˜ ์œ„์น˜๋ฅผ (0,0)์„ ๊ธฐ์ค€์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜
def standard_0(b):
    tmp = []
    std_x = b[0][0]
    std_y = b[0][1]
    for x, y in b:
        tmp.append((x-std_x, y-std_y))
    return sorted(tmp)


def solution(game_board, table):
    answer = []
    N = len(game_board)

    visited_board = [list(False for _ in range(N)) for _ in range(N)]
    empty_board = []    # game_board์˜ ๋นˆ์ž๋ฆฌ ์ขŒํ‘œ ๋ฆฌ์ŠคํŠธ 
    
    visited_table = [list(False for _ in range(N)) for _ in range(N)]
    empty_table = []    # table์˜ ๋ธ”๋ก ์ขŒํ‘œ ๋ฆฌ์ŠคํŠธ

    # dfs ์ด์šฉํ•˜์—ฌ game_board์˜ ๋นˆ ์ž๋ฆฌ ์ขŒํ‘œ ๋ฆฌ์ŠคํŠธ empty_board์— ์ €์žฅ
    for i in range(N):
        for j in range(N):
            if visited_board[i][j] == False and game_board[i][j] == 0:
                dfs(i, j, game_board, visited_board, empty_board, 0)

    # dfs ์ด์šฉํ•˜์—ฌ table์˜ ๋ธ”๋ก ์ขŒํ‘œ ๋ฆฌ์ŠคํŠธ empty_table์— ์ €์žฅ
    for i in range(N):
        for j in range(N):
            if visited_table[i][j] == False and table[i][j] == 1:
                dfs(i, j, table, visited_table, empty_table, 1)
    
    # standard ํ•จ์ˆ˜ ์ด์šฉํ•˜์—ฌ empty_table์˜ ๋ธ”๋ก ์ขŒํ‘œ๋ฅผ (0,0) ๊ธฐ์ค€์œผ๋กœ ๋ณ€๊ฒฝ ํ›„ blocks์— ์ €์žฅ
    blocks = []
    for block in empty_table:
        blocks.append(standard_0(block))


    # ์ด์ฐจ์› ๋ฆฌ์ŠคํŠธ ํ•œ์นธ์”ฉ ๋Œ์•„๋ณด๋ฉฐ ๋งž๋Š” ์ž๋ฆฌ ์žˆ๋Š”์ง€ ํ™•์ธ ํ•จ์ˆ˜
    def match(block):  
        for x in range(N):
            for y in range(N):
                move = []
                for _x, _y in block:
                    new_x = x+_x
                    new_y = y+_y
                    if new_x >= 0 and new_y >=0:    # ์ธ๋ฑ์Šค ์ฒดํฌ
                        try:
                            _ = game_board[x+_x][y+_y]  # ์ธ๋ฑ์Šค ์ฒดํฌ
                            move.append((x+_x, y+_y))
                        except IndexError:
                            break
                    else:
                        break
                if len(block) == len(move) and move in empty_board: # ๋งž๋Š” ์ž๋ฆฌ ์ฐพ์Œ
                    empty_board.remove(move)    # ๋นˆ์ž๋ฆฌ ์‚ญ์ œ
                    answer.extend(move)
                    return True
        return False

    # ๋ธ”๋ก 90๋„ ํšŒ์ „ ํ•จ์ˆ˜
    def rotate_90(block):
        new = []
        for x, y in block:
            new.append((y, N-1 - x))
        return standard_0(new)
        

    # ๋ธ”๋ก ํ•˜๋‚˜์”ฉ ๋งž๋Š” ์ž๋ฆฌ ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ณ  ์—†์œผ๋ฉด ํšŒ์ „ ํ›„ ์ฐพ๊ธฐ ๋ฐ˜๋ณต
    for block in blocks:
        for _ in range(4):
            if match(block) == False:       # ๋งž๋Š” ์ž๋ฆฌ ์—†์Œ
                block = rotate_90(block)    # ๋ธ”๋ก ํšŒ์ „
            else:   # ๋งž๋Š” ์ž๋ฆฌ ์ฐพ์Œ
                break

    return len(answer)

โœ” ์šฐ์„  game_board์˜ ๋นˆ์นธ๊ณผ table์— ์žˆ๋Š” ๋ธ”๋ก์˜ ์ขŒํ‘œ๊ฐ’์„ ๋ธ”๋ก ๋‹จ์œ„๋กœ ๊ฐ๊ฐ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜๊ณ 

table์— ์žˆ๋Š” ๋ธ”๋ก ๋ฆฌ์ŠคํŠธ๋ฅผ game_board์˜ ๋ชจ๋“  ์นธ์— ๋Œ€์ž…ํ•ด๋ณด๋ฉฐ game_board์˜ ๋นˆ์นธ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํšŒ์ „ ํ›„ ๋‹ค์‹œ ๋Œ€์ž…ํ•˜๋ฉฐ ํ™•์ธํ•ด๋ณธ๋‹ค.

 

๐Ÿ’ฅ ํŠœํ”Œ์˜ ๊ฐ’์€ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค

๐Ÿ’ฅ ๋ธ”๋ก์˜ ์ขŒํ‘œ๋ฅผ (0,0) ๊ธฐ์ค€์œผ๋กœ ์˜ฎ๊ฒจ์•ผ board์˜ ๋ชจ๋“  ์นธ ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ’ฅ ๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค๊ฐ€ ์Œ์ˆ˜์ธ ๊ฒฝ์šฐ๋Š” IndexError์— ๊ฑธ๋ฆฌ์ง€ ์•Š์œผ๋ฏ€๋กœ ๋”ฐ๋กœ ํ™•์ธํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

1. dfs ํ•จ์ˆ˜

dfs๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด์ฐจ์› ๋ฆฌ์ŠคํŠธ board์— ์žˆ๋Š” ๋ธ”๋ก ๋ชจ์–‘์˜ ์ขŒํ‘œ ๋ฆฌ์ŠคํŠธ๋ฅผ empty ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค.

game_board์—์„œ๋Š” ๊ฐ’์ด 0์ธ ๋ธ”๋ก์„, table์—์„œ๋Š” ๊ฐ’์ด 1์ธ ๋ธ”๋ก์„ ํ™•์ธํ•ด์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ

์ธ์ž n์„ ํ†ตํ•ด ๊ตฌ๋ณ„ํ•ด์ค€๋‹ค.

stack.pop() ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ์˜ ์ขŒํ‘œ๋ฅผ stack์— ๋„ฃ์–ด์ค€๋‹ค.

์ด๋•Œ stack.pop()์˜ ๊ฐ’์ด board ์ธ๋ฑ์Šค ์•ˆ์— ์žˆ๋Š”์ง€ ํ™•์ธ ํ›„ ์ง„ํ–‰ํ•ด์•ผ ํ•œ๋‹ค.

์ธ๋ฑ์Šค๊ฐ€ ์Œ์ˆ˜์ธ ๊ฐ’์ผ ๋•Œ๋Š” if๋ฌธ์„ ํ†ตํ•ด ํ™•์ธํ•˜๊ณ , ์ธ๋ฑ์Šค๊ฐ€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฒ—์–ด๋‚ฌ์„ ๋•Œ๋Š” except๋ฅผ ํ†ตํ•ด ํ™•์ธํ•œ๋‹ค.

def dfs(i, j, board, visited, empty, n):
    section = []
    stack = [(i, j)]
    while stack:
        x, y = stack.pop()
        if x >= 0 and y >= 0:   # ์ธ๋ฑ์Šค ์ฒดํฌ
            try:
                if visited[x][y] == False and board[x][y] == n:
                    visited[x][y] = True
                    section.append((x,y))
                    
                    stack.append((x-1, y))
                    stack.append((x+1, y))
                    stack.append((x, y-1))
                    stack.append((x, y+1))
                    
            except IndexError:
                continue
    empty.append(sorted(section))

 

2. standard_0 ํ•จ์ˆ˜

dfs ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด table์—์„œ ์–ป์€ ๋ธ”๋ก ์ขŒํ‘œ ๋ฆฌ์ŠคํŠธ empty_table์„

(0,0)์—์„œ๋ถ€ํ„ฐ ๋งž์ถฐ๋ณด๋ฉฐ ๋นˆ์นธ ๋ฆฌ์ŠคํŠธ์ธ empty_board์™€ ๋น„๊ตํ•ด๋ณผ ๊ฒƒ์ด๋‹ค.

(0,0)์—์„œ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ชจ๋“  ์นธ์„ ์‚ดํŽด๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š”

empty_table์˜ ๋ธ”๋ก์ด (0,0)์„ ๊ธฐ์ค€์œผ๋กœ ์œ„์น˜ํ•ด์žˆ์–ด์•ผํ•œ๋‹ค.

๋ธ”๋ก b์˜ ์ฒซ๋ฒˆ์งธ ์ขŒํ‘œ ๊ฐ’์„ (0,0)์— ๋งž์ถ”๊ณ  ๋‚˜๋จธ์ง€ ์ขŒํ‘œ๋„ ์ด์— ๋”ฐ๋ผ ์˜ฎ๊ฒจ์ค€๋‹ค.

๐Ÿ’ฅ ์ด๋•Œ ์ขŒํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํŠœํ”Œ ๊ฐ’์€ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๋ณ€๊ฒฝ์ด ์•„๋‹Œ ๋‹ค์‹œ ๋งŒ๋“ค์–ด return ํ•œ๋‹ค.

def standard_0(b):
    tmp = []
    std_x = b[0][0]
    std_y = b[0][1]
    for x, y in b:
        tmp.append((x-std_x, y-std_y))
    return sorted(tmp)

 

3. match ํ•จ์ˆ˜

๋ชจ๋“  ์นธ์„ ๋Œ๋ฉด์„œ empty_board์— ์ €์žฅ๋œ ๋นˆ์ž๋ฆฌ์™€ ๊ฐ™์€ ๋ชจ์–‘์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

๋ชจ๋“  ์นธ์„ ๋Œ ๋•Œ ์šฐ์„  ์ธ๋ฑ์Šค ๋ฒ”์œ„๊ฐ€ ๋งž๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

if ๋ฌธ์„ ํ†ตํ•ด ์ธ๋ฑ์Šค๊ฐ€ ์Œ์ˆ˜๋กœ ๋„˜์–ด๊ฐ€๋Š”์ง€ ํ™•์ธํ•˜๊ณ  

_ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค ๋ฒ”์œ„๊ฐ€ ๋„˜์–ด๊ฐˆ ๊ฒฝ์šฐ except์— ๊ฑธ๋ฆฌ๋„๋ก ํ•œ๋‹ค.

๐Ÿ’ฅ except ๋•Œ๋ฌธ์— block์˜ ์ผ๋ถ€๋งŒ move์— ์ €์žฅ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ 

block์˜ ๊ธธ์ด์™€ move์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์€์ง€ ํ™•์ธํ•˜์—ฌ block์˜ ์ „๋ถ€๊ฐ€ move์— ์ €์žฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ ํ›„

empty_board์— ๋งž๋Š” ์ž๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

๋งž๋Š” ์ž๋ฆฌ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋”์ด์ƒ ๋น„์–ด์žˆ๋Š” ์ž๋ฆฌ๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— empty_board์—์„œ ์‚ญ์ œํ•˜๊ณ  answer์— ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•œ๋‹ค.

def match(block):  
        for x in range(N):
            for y in range(N):
                move = []
                for _x, _y in block:
                    new_x = x+_x
                    new_y = y+_y
                    if new_x >= 0 and new_y >=0:    # ์ธ๋ฑ์Šค ์ฒดํฌ
                        try:
                            _ = game_board[x+_x][y+_y]  # ์ธ๋ฑ์Šค ์ฒดํฌ
                            move.append((x+_x, y+_y))
                        except IndexError:
                            break
                    else:
                        break
                if len(block) == len(move) and move in empty_board: # ๋งž๋Š” ์ž๋ฆฌ ์ฐพ์Œ
                    empty_board.remove(move)    # ๋นˆ์ž๋ฆฌ ์‚ญ์ œ
                    answer.extend(move)
                    return True
        return False

 

4. rotate_90 ํ•จ์ˆ˜

๊ธฐ์กด ๋ธ”๋ก์ด ๋งž๋Š” ์ž๋ฆฌ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ํšŒ์ „ํ•˜์—ฌ ๋‹ค์‹œ ๋งž๋Š” ์ž๋ฆฌ๊ฐ€ ์—†๋Š”์ง€ ์ฐพ์•„์•ผํ•œ๋‹ค.

์ขŒํ‘œ๋ฅผ 90๋„ ํšŒ์ „ํ•˜๋ฉด x ๊ฐ’์€ y, y ๊ฐ’์€ N-1-x๊ฐ€ ๋œ๋‹ค.

๐Ÿ’ฅ ์ด๋•Œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํŠœํ”Œ ๊ฐ’์€ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ๋งŒ๋“ค์–ด return ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

def rotate_90(block):
        new = []
        for x, y in block:
            new.append((y, N-1 - x))
        return standard_0(new)

 

728x90

BELATED ARTICLES

more