๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/Leetcode

[Leetcode] Game of Life (#289)

 

 

๋ฌธ์ œ 

 

According to Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

  1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population.
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.

 

 

์˜ˆ์ œ 

Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]

 

 

ํ•ด์„ค ์ฝ”๋“œ 
class Solution(object):
    def gameOfLife(self, b):
        m,n = len(b),len(b[0])
        dirs = [(1,0),(-1,0),(0,1),(0,-1),(-1,1),(-1,-1),(1,-1),(1,1)]
        
        for i in range(m):
            for t in range(n):
                livecount = 0
                
                # ์ •ํ•ด์ง„ ๊ตฌ์—ญ ๋‚ด์— ์žˆ๋Š” 1์˜ ์ˆซ์ž๋ฅผ ์„ผ๋‹ค. 
                for r,c in dirs:
                    nr,nc = i+r,t+c
                    if 0 <= nr < m and 0 <= nc < n and abs(b[nr][nc]) == 1:
                        livecount += 1
                
                # ์ˆ˜๊ฐ€ ๋ณ€ํ•  ๋•Œ์—๋งŒ ์ƒˆ๋กœ์šด ์ˆ˜๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. 
                if b[i][t] == 1:
                    if livecount < 2 or livecount > 3:
                        b[i][t] = -1
                else:
                    if livecount == 3:
                        b[i][t] = 2
        
        # ๋ฐ”๋€Œ์—ˆ๋˜ ์ˆ˜๋ฅผ ๋‹ค์‹œ 0 ํ˜น์€ 1๋กœ ๋ฐ”๊พธ์–ด์ค€๋‹ค. 
        for i in range(m):
            for t in range(n):
                if b[i][t] == -1:
                    b[i][t] = 0
                if b[i][t] == 2:
                    b[i][t] = 1
        print(b)
๋ถ„์„ > ์ด ์ฝ”๋“œ์˜ ๊ธฐ๋ณธ์ ์ธ ์ „๊ฐœ ๊ณผ์ •์€ ์šฐ์„  ์ด์›ƒํ•˜๋Š” 8๊ฐœ์˜ ์ˆ˜ ์ค‘์—์„œ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ณ  ๊ทธ์— ๋”ฐ๋ผ ์ˆ˜๊ฐ€ ๋ณ€ํ™”ํ•œ๋‹ค๋ฉด 1์ด ์•„๋‹Œ ์ˆ˜ (์™œ๋ƒํ•˜๋ฉด ๊ทธ ์ž๋ฆฌ์—์„œ ์ˆ˜๋ฅผ ๋ฐ”๊พผ๋‹ค๋ฉด ์ฐพ๊ณ ์ž ํ•˜๋Š” 1์˜ ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ) ๋กœ ๋ฐ”๊พธ์–ด ์ฃผ๊ณ  ํ›„์— ๋ฐ”๋€ ์ˆ˜๋ฅผ ๋‹ค์‹œ 0 ํ˜น์€ 1๋กœ ๋ฐ”๊พธ์–ด์คŒ์œผ๋กœ์จ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ์ด์›ƒํ•˜๋Š” 8๊ฐœ์˜ ์ˆ˜ ์ค‘์—์„œ 1์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ์„ ๋•Œ๋Š” ์ •ํ•ด์ง„ ๊ตฌ์—ญ ๋‚ด, ์ฆ‰ ๋ฐฉํ–ฅํ‚ค๋ฅผ ์ด์šฉํ•ด ์›€์ง์˜€์„ ๋•Œ ์ตœ์†Œ์ธ 0 ์ด์ƒ ์ตœ๋Œ€์ธ m ๊ณผ n ๋ฏธ๋งŒ์˜ ๊ตฌ์—ญ ๋‚ด์— ํฌํ•จ๋˜๋ฉด์„œ ( n๊ณผ m์ด ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์ž๊ธฐ ์ž์‹ ๋„ ์…€ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ) ์ ˆ๋Œ€๊ฐ’์ด 1์ธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งŒ ์„ธ์–ด lifecount ์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ์ฒ˜์Œ ์ด ๋ฌธ์ œ๋ฅผ ์ ‘ํ–ˆ์„ ๋•Œ, ์ด์›ƒํ•˜๋Š” 8๊ฐœ์˜ ์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ํƒ์ƒ‰ํ• ์ง€ ์ข€์ฒ˜๋Ÿผ ๊ฐ์„ ์žก์„ ์ˆ˜ ์—†์—ˆ๋Š”๋ฐ ์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด ํŠœํ”Œ์„ ์ด์šฉํ•ด ๋ฐฉํ–ฅํ‚ค๋ฅผ ๋งŒ๋“ค์–ด ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

 

 

 

 

Game of Life - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

'Algorithm > Leetcode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leetcode] Print Words Vertically (#1324)  (0) 2022.04.20
[Leetcode] Reshape the Matrix (#566)  (0) 2022.04.20
[Leetcode] Complex Number Multiplication (#537)  (0) 2022.04.20
[Leetcode] Transpose Matrix (#867)  (0) 2022.04.18
[Leetcode] Last Stone Weight (#1046)  (0) 2022.04.08