๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค [1์ฐจ] ํ”„๋ Œ์ฆˆ4๋ธ”๋ก

๐ŸŽฎinspirer9 2023. 2. 21. 13:04
728x90
๋ฐ˜์‘ํ˜•
  • ์ด๊ฑฐ ๋ฌธ์ œ ์ œ๋ชฉ์— ์™œ 1์ฐจ๊ฐ€ ์žˆ๋‚˜ ํ–ˆ๋”๋‹ˆ...
    • 2018๋…„ ์นด์นด์˜ค ๋ธ”๋ผ์ธํŠธ ์ฑ„์šฉ ํ…Œ์ŠคํŠธ 1์ฐจ ๋ฌธ์ œ๋ผ์„œ [1์ฐจ]๋ผ๊ณ  ์จ์žˆ๋Š”๊ฑฐ๊ตฌ๋‚˜.
    • 2018๋…„์—๋งŒ 3๋ฒˆ์„ ํ–ˆ๋„ค... ๊ทธ๋ž˜์„œ [3์ฐจ]๊นŒ์ง€ ์žˆ๋‹ค.
    • ๊ทผ๋ฐ 2019๋…„ ๋ถ€ํ„ฐ๋Š” ์ € ํ‘œ์‹œ๊ฐ€ ์—†์—‰.
      • ์•”ํŠผ...

๋ฌธ์ œ ์„ค๋ช…

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


๋งŒ์•ฝ ํŒ์ด ์œ„์™€ ๊ฐ™์ด ์ฃผ์–ด์งˆ ๊ฒฝ์šฐ, ๋ผ์ด์–ธ์ด 2×2๋กœ ๋ฐฐ์น˜๋œ 7๊ฐœ ๋ธ”๋ก๊ณผ ์ฝ˜์ด 2×2๋กœ ๋ฐฐ์น˜๋œ 4๊ฐœ ๋ธ”๋ก์ด ์ง€์›Œ์ง„๋‹ค. ๊ฐ™์€ ๋ธ”๋ก์€ ์—ฌ๋Ÿฌ 2×2์— ํฌํ•จ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ง€์›Œ์ง€๋Š” ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” 2×2 ๋ชจ์–‘์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค๋ฉด ํ•œ๊บผ๋ฒˆ์— ์ง€์›Œ์ง„๋‹ค.

๋ธ”๋ก์ด ์ง€์›Œ์ง„ ํ›„์— ์œ„์— ์žˆ๋Š” ๋ธ”๋ก์ด ์•„๋ž˜๋กœ ๋–จ์–ด์ ธ ๋นˆ ๊ณต๊ฐ„์„ ์ฑ„์šฐ๊ฒŒ ๋œ๋‹ค.

๋งŒ์•ฝ ๋นˆ ๊ณต๊ฐ„์„ ์ฑ„์šด ํ›„์— ๋‹ค์‹œ 2×2 ํ˜•ํƒœ๋กœ ๊ฐ™์€ ๋ชจ์–‘์˜ ๋ธ”๋ก์ด ๋ชจ์ด๋ฉด ๋‹ค์‹œ ์ง€์›Œ์ง€๊ณ  ๋–จ์–ด์ง€๊ณ ๋ฅผ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋œ๋‹ค.

์œ„ ์ดˆ๊ธฐ ๋ฐฐ์น˜๋ฅผ ๋ฌธ์ž๋กœ ํ‘œ์‹œํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

TTTANT
RRFACC
RRRFCC
TRRRAA
TTMMMF
TMMTTJ

๊ฐ ๋ฌธ์ž๋Š” ๋ผ์ด์–ธ(R), ๋ฌด์ง€(M), ์–ดํ”ผ์น˜(A), ํ”„๋กœ๋„(F), ๋„ค์˜ค(N), ํŠœ๋ธŒ(T), ์ œ์ด์ง€(J), ์ฝ˜(C)์„ ์˜๋ฏธํ•œ๋‹ค

์ž…๋ ฅ์œผ๋กœ ๋ธ”๋ก์˜ ์ฒซ ๋ฐฐ์น˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ง€์›Œ์ง€๋Š” ๋ธ”๋ก์€ ๋ชจ๋‘ ๋ช‡ ๊ฐœ์ธ์ง€ ํŒ๋‹จํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ œ์ž‘ํ•˜๋ผ.

์ œํ•œ ์‚ฌํ•ญ

์ž…๋ ฅํ˜•์‹

  • ์ž…๋ ฅ์œผ๋กœ ํŒ์˜ ๋†’์ด m, ํญ n๊ณผ ํŒ์˜ ๋ฐฐ์น˜ ์ •๋ณด board๊ฐ€ ๋“ค์–ด์˜จ๋‹ค.
  • 2 โ‰ฆ n, m โ‰ฆ 30
  • board๋Š” ๊ธธ์ด n์ธ ๋ฌธ์ž์—ด m๊ฐœ์˜ ๋ฐฐ์—ด๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋ธ”๋ก์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž A์—์„œ Z๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.

์ถœ๋ ฅํ˜•์‹

  • ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ํŒ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ๋ช‡ ๊ฐœ์˜ ๋ธ”๋ก์ด ์ง€์›Œ์งˆ์ง€ ์ถœ๋ ฅํ•˜๋ผ.

์ž…์ถœ๋ ฅ ์˜ˆ

m n board answer
4 5 ["CCBDE", "AAADE", "AAABF", "CCBBF"] 14
6 6 ["TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"] 15

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

  • ์ž…์ถœ๋ ฅ ์˜ˆ์ œ 1์˜ ๊ฒฝ์šฐ, ์ฒซ ๋ฒˆ์งธ์—๋Š” A ๋ธ”๋ก 6๊ฐœ๊ฐ€ ์ง€์›Œ์ง€๊ณ , ๋‘ ๋ฒˆ์งธ์—๋Š” B ๋ธ”๋ก 4๊ฐœ์™€ C ๋ธ”๋ก 4๊ฐœ๊ฐ€ ์ง€์›Œ์ ธ, ๋ชจ๋‘ 14๊ฐœ์˜ ๋ธ”๋ก์ด ์ง€์›Œ์ง„๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ์ œ 2๋Š” ๋ณธ๋ฌธ ์„ค๋ช…์— ์žˆ๋Š” ๊ทธ๋ฆผ์„ ์˜ฎ๊ธด ๊ฒƒ์ด๋‹ค. 11๊ฐœ์™€ 4๊ฐœ์˜ ๋ธ”๋ก์ด ์ฐจ๋ก€๋กœ ์ง€์›Œ์ง€๋ฉฐ, ๋ชจ๋‘ 15๊ฐœ์˜ ๋ธ”๋ก์ด ์ง€์›Œ์ง„๋‹ค.

ํ’€์ด

  • ์ด๊ฑด ๋ญ ํ’€์ด๊ฐ€ ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ๊ทธ๋ƒฅ ์–ด๋ฆด ๋•Œ ๊ฒŒ์ž„ ๋งŒ๋“ค์—ˆ๋˜ ์‹์œผ๋กœ ๋ฌด์ง€์„ฑ์œผ๋กœ ๋งŒ๋“ค์–ด๋ฒ„๋ฆฌ๋ฉด ๋จ.
  • ๊ตฌํ˜„ ๋ฌธ์ œ์ž–์•„...? ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋Š” ์•„๋‹Œ ๋“ฏ...
def solution(m, n, board):
    # ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ€๋Š” ๋ฐฉ์‹์ด๋‹ˆ๊นŒ ํ๋ฅผ ์ˆ˜์ง์œผ๋กœ ์„ธ์›Œ์„œ ๋งŒ๋“œ๋Š”๊ฒŒ ์ข‹์„ ๋“ฏ
    ๊ฒŒ์ž„๋ณด๋“œ = [[0]*m for _ in range(n)]
    ๋น„ํŠธ๋ณด๋“œ = [[0]*m for _ in range(n)]
    
    # ๊ฒŒ์ž„๋ณด๋“œ ๋ฐ์ดํ„ฐ ์ฑ„์šฐ๊ธฐ
    for h in range(n):
        for w in range(m):
            ๊ฒŒ์ž„๋ณด๋“œ[h][w] = board[w][h]

    def ๋น„ํŠธ๋ณด๋“œ์ดˆ๊ธฐํ™”():
        for h in range(n):
            for w in range(m):
                ๋น„ํŠธ๋ณด๋“œ[h][w] = 0
    ๋น„ํŠธ๋ณด๋“œ์ดˆ๊ธฐํ™”()
    
    def ๊ฒŒ์ž„๋ณด๋“œ๊ทธ๋ฆฌ๊ธฐ():
        for h in range(n):
            for w in range(m):
                print(๊ฒŒ์ž„๋ณด๋“œ[h][w],end=" ")
            print(" ")
        for h in range(n):
            for w in range(m):
                print(๋น„ํŠธ๋ณด๋“œ[h][w],end=" ")
            print(" ")
    ๊ฒŒ์ž„๋ณด๋“œ๊ทธ๋ฆฌ๊ธฐ()
    
    ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ, ์ง€์›Œ์ง„ํ”„๋ Œ์ฆˆ = 1, 0
    limit = 5
    while ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ > 0 and limit > 0:
        limit -= 1
        ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ = 0 # ์ง€์›Œ์งˆ ํ”„๋ Œ์ฆˆ ์ฒดํฌ
        for h in range(n-1):
            for w in range(m-1):
                ํ”„๋ Œ์ฆˆ = ๊ฒŒ์ž„๋ณด๋“œ[h][w]
                if ํ”„๋ Œ์ฆˆ != ' ' and ํ”„๋ Œ์ฆˆ == ๊ฒŒ์ž„๋ณด๋“œ[h+1][w] and ํ”„๋ Œ์ฆˆ == ๊ฒŒ์ž„๋ณด๋“œ[h+1][w+1] and ํ”„๋ Œ์ฆˆ == ๊ฒŒ์ž„๋ณด๋“œ[h][w+1]:
                    ๋น„ํŠธ๋ณด๋“œ[h][w], ๋น„ํŠธ๋ณด๋“œ[h+1][w], ๋น„ํŠธ๋ณด๋“œ[h+1][w+1], ๋น„ํŠธ๋ณด๋“œ[h][w+1] = 1, 1, 1, 1
                    ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ = 1
        if ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ == 0:
            break
        
        for h in range(n): # ์ง€์›Œ์งˆ ํ”„๋ Œ์ฆˆ ์นด์šดํŠธ
            ์ง€์›Œ์ง„ํ”„๋ Œ์ฆˆ += ๋น„ํŠธ๋ณด๋“œ[h].count(1)
        
        for h in range(n): # ๊ฒŒ์ž„๋ณด๋“œ/๋น„ํŠธ๋ณด๋“œ ๋‚™ํ•˜
            a = ๋น„ํŠธ๋ณด๋“œ[h].count(1)
            for i in range(a):
                ๊ฒŒ์ž„๋ณด๋“œ[h].pop(๋น„ํŠธ๋ณด๋“œ[h].index(1))
                ๋น„ํŠธ๋ณด๋“œ[h].pop(๋น„ํŠธ๋ณด๋“œ[h].index(1))
                ๊ฒŒ์ž„๋ณด๋“œ[h].insert(0, ' ')
                ๋น„ํŠธ๋ณด๋“œ[h].insert(0, 0)
            
    ๋น„ํŠธ๋ณด๋“œ์ดˆ๊ธฐํ™”()
    ๊ฒŒ์ž„๋ณด๋“œ๊ทธ๋ฆฌ๊ธฐ()
    
    return ์ง€์›Œ์ง„ํ”„๋ Œ์ฆˆ
  • ์ž˜ ๋˜๋‚˜ ํ™•์ธํ•˜๊ณ 
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	4, 5, ["CCBDE", "AAADE", "AAABF", "CCBBF"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	14
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	C A A C  
C A A C  
B A A B  
D D B B  
E E F F  
0 0 0 0  
0 0 0 0  
0 0 0 0  
0 0 0 0  
0 0 0 0  
         
         
         
    D D  
E E F F  
0 0 0 0  
0 0 0 0  
0 0 0 0  
0 0 0 0  
0 0 0 0  

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	6, 6, ["TTTANT", "RRFACC", "RRRFCC", "TRRRAA", "TTMMMF", "TMMTTJ"]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	15
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	T R R T T T  
T R R R T M  
T F R R M M  
A A F R M T  
N C C A M T  
T C C A F J  
0 0 0 0 0 0  
0 0 0 0 0 0  
0 0 0 0 0 0  
0 0 0 0 0 0  
0 0 0 0 0 0  
0 0 0 0 0 0  
        T T  
          M  
    T F M M  
A A F R M T  
    N A M T  
    T A F J  
0 0 0 0 0 0  
0 0 0 0 0 0  
0 0 0 0 0 0  
0 0 0 0 0 0  
0 0 0 0 0 0  
0 0 0 0 0 0
  • ์ฝ”๋“œ ์ˆ˜์ •ํ•ด์„œ ์ฑ„์ !
def solution(m, n, board):
    ๊ฒŒ์ž„๋ณด๋“œ = [[0]*m for _ in range(n)]    # ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ€๋Š” ๋ฐฉ์‹์ด๋‹ˆ๊นŒ ํ๋ฅผ ์ˆ˜์ง์œผ๋กœ ์„ธ์›Œ์„œ ๋งŒ๋“œ๋Š”๊ฒŒ ์ข‹์„ ๋“ฏ
    ๋น„ํŠธ๋ณด๋“œ = [[0]*m for _ in range(n)]

    for h in range(n):
        for w in range(m):
            ๊ฒŒ์ž„๋ณด๋“œ[h][w] = board[w][h]    # ๊ฒŒ์ž„๋ณด๋“œ ๋ฐ์ดํ„ฐ ์ฑ„์šฐ๊ธฐ
    
    ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ, ์ง€์›Œ์ง„ํ”„๋ Œ์ฆˆ = 1, 0
    while True:
        ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ = 0                      # ์ง€์›Œ์งˆ ํ”„๋ Œ์ฆˆ ์ฒดํฌ
        for h in range(n-1):
            for w in range(m-1):
                ํ”„๋ Œ์ฆˆ = ๊ฒŒ์ž„๋ณด๋“œ[h][w]
                if ํ”„๋ Œ์ฆˆ != ' ' and ํ”„๋ Œ์ฆˆ == ๊ฒŒ์ž„๋ณด๋“œ[h+1][w] and ํ”„๋ Œ์ฆˆ == ๊ฒŒ์ž„๋ณด๋“œ[h+1][w+1] and ํ”„๋ Œ์ฆˆ == ๊ฒŒ์ž„๋ณด๋“œ[h][w+1]:
                    ๋น„ํŠธ๋ณด๋“œ[h][w], ๋น„ํŠธ๋ณด๋“œ[h+1][w], ๋น„ํŠธ๋ณด๋“œ[h+1][w+1], ๋น„ํŠธ๋ณด๋“œ[h][w+1] = 1, 1, 1, 1
                    ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ = 1
        if ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ == 0:
            break
        for h in range(n): # ์ง€์›Œ์งˆ ํ”„๋ Œ์ฆˆ ์นด์šดํŠธ
            ์ง€์›Œ์ง„ํ”„๋ Œ์ฆˆ += ๋น„ํŠธ๋ณด๋“œ[h].count(1)
        for h in range(n): # ๊ฒŒ์ž„๋ณด๋“œ/๋น„ํŠธ๋ณด๋“œ ๋‚™ํ•˜
            a = ๋น„ํŠธ๋ณด๋“œ[h].count(1)
            for i in range(a):
                ๊ฒŒ์ž„๋ณด๋“œ[h].pop(๋น„ํŠธ๋ณด๋“œ[h].index(1))
                ๋น„ํŠธ๋ณด๋“œ[h].pop(๋น„ํŠธ๋ณด๋“œ[h].index(1))
                ๊ฒŒ์ž„๋ณด๋“œ[h].insert(0, ' ')
                ๋น„ํŠธ๋ณด๋“œ[h].insert(0, 0)
    
    return ์ง€์›Œ์ง„ํ”„๋ Œ์ฆˆ
  • ํ†ต๊ณผ ํ–ˆ๋Š”๋ฐ ์ ์ˆ˜๊ฐ€ 1์ ...
    • ์ ์ˆ˜๊ฐ€ ์งœ๋‹ค ใ…‹ใ…‹ใ…‹
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1.21ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (23.87ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (3.06ms, 10.6MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.64ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (1.19ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.6MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.44ms, 10.5MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (2.00ms, 10.6MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด... ํ ... ์†๋„๊ฐ€ ๋ณ„๋กœ ์•ˆ๋น ๋ฅธ๋ฐ? ๊ทธ ์ค‘ ์ข€ ๋น ๋ฅธ ๊ฑฐ ์ฐพ์•„๋ด„
def solution(m, n, board):
    board = [list(b) for b in board]
    board = [list(reversed([b[i] for b in board])) for i in range(n)]
    remove_target = get_remove_target(board)
    while not "".join([str.strip("".join(t)) for t in remove_target]) == "":
        for i in range(len(board)):
            for j in range(len(board[i]) - 1, -1, -1):
                if "*" == remove_target[i][j]:
                    del board[i][j]
        remove_target = get_remove_target(board)
    cnt = 0
    for b in board:
        cnt += len(b)
    return m * n - cnt


def get_remove_target(board):
    remove_target = [list(" " * len(board[i])) for i in range(len(board))]
    for i in range(len(board) - 1):
        for j in range(len(board[i]) - 1):
            try:
                if board[i][j] == board[i + 1][j] and board[i + 1][j] == board[i + 1][j + 1] and board[i + 1][j + 1] == \
                        board[i][j + 1]:
                    remove_target[i][j] = "*"
                    remove_target[i + 1][j] = "*"
                    remove_target[i][j + 1] = "*"
                    remove_target[i + 1][j + 1] = "*"
            except IndexError:
                continue
    return remove_target
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.91ms, 10MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (23.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (2.60ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.36ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.72ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.38ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.93ms, 10.2MB)
  • ์ด์ชฝ์ด ์ฝ”๋“œ๊ฐ€ ๋” ์งง์•„...
def solution(m, n, board):
    board.reverse()
    field = [list(cols) for cols in zip(*board)]
    answer = 0

    while True:
        bomb = set()
        for row in range(n - 1):
            for col in range(m - 1):
                try:
                    if field[row][col] == field[row + 1][col] == field[row][col + 1] == field[row + 1][col + 1]:
                        bomb.update({(row, col), (row + 1, col), (row, col + 1), (row + 1, col + 1)})
                except:
                    break

        if not len(bomb):
            break

        for r, c in bomb:
            field[r][c] = ''
            answer += 1

        for row in range(n):
            field[row] = list(''.join(field[row]))

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.82ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (17.61ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (1.80ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.38ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (1.35ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.48ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (1.03ms, 10.3MB)
  • ์ด๊ฑฐ ๋‘์ค„๋กœ ํ•ฉ์ณค์œผ๋ฉด ๋” ๋นจ๋ผ์กŒ์„ ๋“ฏ?

  • ์ตœ์ข…. ๋ณ„ ์ฐจ์ด ์—†๋„ค...
def solution(m, n, board):
    ๊ฒŒ์ž„๋ณด๋“œ = [[0]*m for _ in range(n)]    # ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ€๋Š” ๋ฐฉ์‹์ด๋‹ˆ๊นŒ ํ๋ฅผ ์ˆ˜์ง์œผ๋กœ ์„ธ์›Œ์„œ ๋งŒ๋“œ๋Š”๊ฒŒ ์ข‹์„ ๋“ฏ
    ๋น„ํŠธ๋ณด๋“œ = [[0]*m for _ in range(n)]

    for h in range(n):
        for w in range(m):
            ๊ฒŒ์ž„๋ณด๋“œ[h][w] = board[w][h]    # ๊ฒŒ์ž„๋ณด๋“œ ๋ฐ์ดํ„ฐ ์ฑ„์šฐ๊ธฐ
    
    ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ, ์ง€์›Œ์ง„ํ”„๋ Œ์ฆˆ = 1, 0
    while True:
        ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ = 0                      # ์ง€์›Œ์งˆ ํ”„๋ Œ์ฆˆ ์ฒดํฌ
        for h in range(n-1):
            for w in range(m-1):
                ํ”„๋ Œ์ฆˆ = ๊ฒŒ์ž„๋ณด๋“œ[h][w]
                if ํ”„๋ Œ์ฆˆ != ' ' and ํ”„๋ Œ์ฆˆ == ๊ฒŒ์ž„๋ณด๋“œ[h+1][w] and ํ”„๋ Œ์ฆˆ == ๊ฒŒ์ž„๋ณด๋“œ[h+1][w+1] and ํ”„๋ Œ์ฆˆ == ๊ฒŒ์ž„๋ณด๋“œ[h][w+1]:
                    ๋น„ํŠธ๋ณด๋“œ[h][w], ๋น„ํŠธ๋ณด๋“œ[h+1][w], ๋น„ํŠธ๋ณด๋“œ[h+1][w+1], ๋น„ํŠธ๋ณด๋“œ[h][w+1] = 1, 1, 1, 1
                    ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ = 1
        if ์ง€์›Œ์งˆํ”„๋ Œ์ฆˆ == 0:
            break
        for h in range(n): # ์ง€์›Œ์งˆ ํ”„๋ Œ์ฆˆ ์นด์šดํŠธ + ๊ฒŒ์ž„๋ณด๋“œ/๋น„ํŠธ๋ณด๋“œ ๋‚™ํ•˜
            a = ๋น„ํŠธ๋ณด๋“œ[h].count(1)
            ์ง€์›Œ์ง„ํ”„๋ Œ์ฆˆ += a
            for i in range(a):
                ๊ฒŒ์ž„๋ณด๋“œ[h].pop(๋น„ํŠธ๋ณด๋“œ[h].index(1))
                ๋น„ํŠธ๋ณด๋“œ[h].pop(๋น„ํŠธ๋ณด๋“œ[h].index(1))
                ๊ฒŒ์ž„๋ณด๋“œ[h].insert(0, ' ')
                ๋น„ํŠธ๋ณด๋“œ[h].insert(0, 0)
    
    return ์ง€์›Œ์ง„ํ”„๋ Œ์ฆˆ
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.5MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.6MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1.20ms, 10.5MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (21.22ms, 10.5MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (3.26ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.65ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (1.16ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.5MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.47ms, 10.6MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (1.46ms, 10.3MB)

 

728x90
๋ฐ˜์‘ํ˜•