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

The only one you can truly trust is yourself.

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ (2021 ์นด์นด์˜ค ์ฑ„์šฉ์—ฐ๊ณ„ํ˜• ์ธํ„ด์‹ญ)

๐ŸŽฎinspirer9 2023. 3. 3. 17:41
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

๊ฐœ๋ฐœ์ž๋ฅผ ํฌ๋งํ•˜๋Š” ์ฃ ๋ฅด๋””๊ฐ€ ์นด์นด์˜ค์— ๋ฉด์ ‘์„ ๋ณด๋Ÿฌ ์™”์Šต๋‹ˆ๋‹ค.

์ฝ”๋กœ๋‚˜ ๋ฐ”์ด๋Ÿฌ์Šค ๊ฐ์—ผ ์˜ˆ๋ฐฉ์„ ์œ„ํ•ด ์‘์‹œ์ž๋“ค์€ ๊ฑฐ๋ฆฌ๋ฅผ ๋‘ฌ์„œ ๋Œ€๊ธฐ๋ฅผ ํ•ด์•ผํ•˜๋Š”๋ฐ ๊ฐœ๋ฐœ ์ง๊ตฐ ๋ฉด์ ‘์ธ ๋งŒํผ
์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์œผ๋กœ ๋Œ€๊ธฐ์‹ค์— ๊ฑฐ๋ฆฌ๋ฅผ ๋‘๊ณ  ์•‰๋„๋ก ์•ˆ๋‚ดํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๋Œ€๊ธฐ์‹ค์€ 5๊ฐœ์ด๋ฉฐ, ๊ฐ ๋Œ€๊ธฐ์‹ค์€ 5x5 ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค.
  2. ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์œ„ํ•˜์—ฌ ์‘์‹œ์ž๋“ค ๋ผ๋ฆฌ๋Š” ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ1๊ฐ€ 2 ์ดํ•˜๋กœ ์•‰์ง€ ๋ง์•„ ์ฃผ์„ธ์š”.
  3. ๋‹จ ์‘์‹œ์ž๊ฐ€ ์•‰์•„์žˆ๋Š” ์ž๋ฆฌ ์‚ฌ์ด๊ฐ€ ํŒŒํ‹ฐ์…˜์œผ๋กœ ๋ง‰ํ˜€ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด,

์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ž๋ฆฌ ์‚ฌ์ด์— ํŒŒํ‹ฐ์…˜์ด ์กด์žฌํ•œ๋‹ค๋ฉด ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ๊ฐ€ 2์—ฌ๋„ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚จ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ํŒŒํ‹ฐ์…˜์„ ์‚ฌ์ด์— ๋‘๊ณ  ์•‰์€ ๊ฒฝ์šฐ๋„ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚จ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ž๋ฆฌ ์‚ฌ์ด๊ฐ€ ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ 2์ด๊ณ  ์‚ฌ์ด์— ๋นˆ ํ…Œ์ด๋ธ”์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค์ง€ ์•Š์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์‘์‹œ์ž๊ฐ€ ์•‰์•„์žˆ๋Š” ์ž๋ฆฌ(P)๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋นˆ ํ…Œ์ด๋ธ”(O)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ํŒŒํ‹ฐ์…˜(X)์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

5๊ฐœ์˜ ๋Œ€๊ธฐ์‹ค์„ ๋ณธ ์ฃ ๋ฅด๋””๋Š” ๊ฐ ๋Œ€๊ธฐ์‹ค์—์„œ ์‘์‹œ์ž๋“ค์ด ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ž˜ ๊ธฐํ‚ค๊ณ  ์žˆ๋Š”์ง€ ์•Œ๊ณ  ์‹ถ์–ด์กŒ์Šต๋‹ˆ๋‹ค. ์ž๋ฆฌ์— ์•‰์•„์žˆ๋Š” ์‘์‹œ์ž๋“ค์˜ ์ •๋ณด์™€ ๋Œ€๊ธฐ์‹ค ๊ตฌ์กฐ๋ฅผ ๋Œ€๊ธฐ์‹ค๋ณ„๋กœ ๋‹ด์€ 2์ฐจ์› ๋ฌธ์ž์—ด ๋ฐฐ์—ด places๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐ ๋Œ€๊ธฐ์‹ค๋ณ„๋กœ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์œผ๋ฉด 1์„, ํ•œ ๋ช…์ด๋ผ๋„ ์ง€ํ‚ค์ง€ ์•Š๊ณ  ์žˆ์œผ๋ฉด 0์„ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • places์˜ ํ–‰ ๊ธธ์ด(๋Œ€๊ธฐ์‹ค ๊ฐœ์ˆ˜) = 5
    • places์˜ ๊ฐ ํ–‰์€ ํ•˜๋‚˜์˜ ๋Œ€๊ธฐ์‹ค ๊ตฌ์กฐ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • places์˜ ์—ด ๊ธธ์ด(๋Œ€๊ธฐ์‹ค ์„ธ๋กœ ๊ธธ์ด) = 5
  • places์˜ ์›์†Œ๋Š” P,O,X๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
    • places ์›์†Œ์˜ ๊ธธ์ด(๋Œ€๊ธฐ์‹ค ๊ฐ€๋กœ ๊ธธ์ด) = 5
    • P๋Š” ์‘์‹œ์ž๊ฐ€ ์•‰์•„์žˆ๋Š” ์ž๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • O๋Š” ๋นˆ ํ…Œ์ด๋ธ”์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • X๋Š” ํŒŒํ‹ฐ์…˜์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” 5๊ฐœ ๋Œ€๊ธฐ์‹ค์˜ ํฌ๊ธฐ๋Š” ๋ชจ๋‘ 5x5 ์ž…๋‹ˆ๋‹ค.
  • return ๊ฐ’ ํ˜•์‹
    • 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด์— 5๊ฐœ์˜ ์›์†Œ๋ฅผ ๋‹ด์•„์„œ return ํ•ฉ๋‹ˆ๋‹ค.
    • places์— ๋‹ด๊ฒจ ์žˆ๋Š” 5๊ฐœ ๋Œ€๊ธฐ์‹ค์˜ ์ˆœ์„œ๋Œ€๋กœ, ๊ฑฐ๋ฆฌ๋‘๊ธฐ ์ค€์ˆ˜ ์—ฌ๋ถ€๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์Šต๋‹ˆ๋‹ค.
    • ๊ฐ ๋Œ€๊ธฐ์‹ค ๋ณ„๋กœ ๋ชจ๋“  ์‘์‹œ์ž๊ฐ€ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์œผ๋ฉด 1์„, ํ•œ ๋ช…์ด๋ผ๋„ ์ง€ํ‚ค์ง€ ์•Š๊ณ  ์žˆ์œผ๋ฉด 0์„ ๋‹ด์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

places result
[["POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"], ["POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"], ["PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"], ["OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"], ["PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"]] [1, 0, 1, 1, 1]

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

์ž…์ถœ๋ ฅ ์˜ˆ #1

์ฒซ ๋ฒˆ์งธ ๋Œ€๊ธฐ์‹ค

No. 0 1 2 3 4
0 P O O O P
1 O X X O X
2 O P X P X
3 O O X O X
4 P O X X P
  • ๋ชจ๋“  ์‘์‹œ์ž๊ฐ€ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ๋Œ€๊ธฐ์‹ค

No. 0 1 2 3 4
0 P O O P X
1 O X P X P
2 P X X X O
3 O X X X O
4 O O O P P
  • (0, 0) ์ž๋ฆฌ์˜ ์‘์‹œ์ž์™€ (2, 0) ์ž๋ฆฌ์˜ ์‘์‹œ์ž๊ฐ€ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • (1, 2) ์ž๋ฆฌ์˜ ์‘์‹œ์ž์™€ (0, 3) ์ž๋ฆฌ์˜ ์‘์‹œ์ž๊ฐ€ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • (4, 3) ์ž๋ฆฌ์˜ ์‘์‹œ์ž์™€ (4, 4) ์ž๋ฆฌ์˜ ์‘์‹œ์ž๊ฐ€ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์„ธ ๋ฒˆ์งธ ๋Œ€๊ธฐ์‹ค

No. 0 1 2 3 4
0 P X O P X
1 O X O X P
2 O X P O X
3 O X X O P
4 P X P O X
  • ๋ชจ๋“  ์‘์‹œ์ž๊ฐ€ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋„ค ๋ฒˆ์งธ ๋Œ€๊ธฐ์‹ค

No. 0 1 2 3 4
0 O O O X X
1 X O O O X
2 O O O X X
3 O X O O X
4 O O O O O
  • ๋Œ€๊ธฐ์‹ค์— ์‘์‹œ์ž๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์„ฏ ๋ฒˆ์งธ ๋Œ€๊ธฐ์‹ค

No. 0 1 2 3 4
0 P X P X P
1 X P X P X
2 P X P X P
3 X P X P X
4 P X P X P
  • ๋ชจ๋“  ์‘์‹œ์ž๊ฐ€ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๋ฅผ ์ง€ํ‚ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ๋Œ€๊ธฐ์‹ค์„ ์ œ์™ธํ•œ ๋ชจ๋“  ๋Œ€๊ธฐ์‹ค์—์„œ ๊ฑฐ๋ฆฌ๋‘๊ธฐ๊ฐ€ ์ง€์ผœ์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ๋ฐฐ์—ด [1, 0, 1, 1, 1]์„ return ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‹œ๊ฐ„ ์•ˆ๋‚ด

  • ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ : 10์ดˆ

โ€ป ๊ณต์ง€ - 2022๋…„ 4์›” 25์ผ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋‘ ํ…Œ์ด๋ธ” T1, T2๊ฐ€ ํ–‰๋ ฌ (r1, c1), (r2, c2)์— ๊ฐ๊ฐ ์œ„์น˜ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, T1, T2 ์‚ฌ์ด์˜ ๋งจํ•ดํŠผ ๊ฑฐ๋ฆฌ๋Š” |r1 - r2| + |c1 - c2| ์ž…๋‹ˆ๋‹ค.

ํ’€์ด

  • ๊ฑฐ๋ฆฌ๋‘๊ธฐ ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ๋ดค์„ ๋•Œ๋Š”
    • ์˜์—ญ ๊ตฌ๋ถ„ํ•ด์„œ ๊ฒ€์‚ฌํ•  ์• ๋“ค ๊ณจ๋ผ๋‚ด๊ณ ,
    • ์• ๋“ค๋ผ๋ฆฌ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐํ•˜๊ณ ,
    • ์ธ์ ‘์ด๋ฉด ๊ธธ์ฐพ๊ธฐํ•˜๊ณ 
    • ๋ง‰ ๊ทธ๋Ÿฐ... ๋ณต์žกํ•œ ์ƒ๊ฐ์„ ํ–ˆ๋Š”๋ฐ,
  • ํ’€๋‹ค๋ณด๋‹ˆ๊นŒ ๊ทธ๋ƒฅ ๋ฌธ์ž์—ด๋กœ ๋น„๊ตํ•˜๋ฉด ๋˜์ž–์•„?
    • ์‹ถ์–ด์„œ 2์ค‘ for๋ฌธ 3๋ฒˆ์œผ๋กœ ํ†ต๊ณผ
    • ๋ฒ„ํ‹ฐ์ปฌ์€ ๋ฐฐ์—ด์„ 90๋„ ํšŒ์ „์‹œ์ผœ์„œ ํ•ด๊ฒฐ...
    • ๋ฐ•์Šค๋Š” ๊ทธ๋ƒฅ ๋…ธ๊ฐ€๋‹ค๋กœ... ใ…ก.ใ…ก;
def solution(places):
    answer = [1] * 5
    h_check_patterns = ["PP", "POP"]
    v_checks_patterns = ["PP", "POP"]
    box_checks_patterns = [("PO","OP"),("PX","OP"),("PO","XP"),("OP","PO"),("XP","PO"),("OP","PX")]
    places_90d_r = []
    
    for i in range(5):
        for j in range(5):
            if answer[i] == 0:
                break
            for h_chk in h_check_patterns:
                if h_chk in places[i][j]:
                    answer[i] = 0
                    break
                                 
    for i in range(5):
        _tmp = []
        for j in range(5):
            _t = ""
            for k in range(5):
                _t += places[i][k][j]
            _tmp.append(_t)
        places_90d_r.append(_tmp)
        
    for i in range(5):
        for j in range(5):
            if answer[i] == 0:
                break
            for h_chk in h_check_patterns:
                if h_chk in places_90d_r[i][j]:
                    answer[i] = 0
                    break
                    
    for i in range(5):
        for j in range(4):
            if answer[i] == 0:
                break
            for k in range(4):
                for b_chk in box_checks_patterns:
                    if places[i][j][k]+places[i][j][k+1] == b_chk[0] and places[i][j+1][k]+places[i][j+1][k+1] == b_chk[1]:
                        answer[i] = 0
                        break
        
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.63ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.4MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.3MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.3MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.2MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.29ms, 10.4MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.27ms, 10.2MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.2MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.3MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.4MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด.
    • ๋น„์Šทํ•œ ๋ฐฉ์‹
def check(place):
    for irow, row in enumerate(place):
        for icol, cell in enumerate(row):
            if cell != 'P':
                continue
            if irow != 4 and place[irow + 1][icol] == 'P':
                return 0
            if icol != 4 and place[irow][icol + 1] == 'P':
                return 0
            if irow < 3 and place[irow + 2][icol] == 'P' and place[irow + 1][icol] != 'X':
                return 0
            if icol < 3 and place[irow][icol + 2] == 'P' and place[irow][icol + 1] != 'X':
                return 0
            if irow != 4 and icol != 4 and place[irow + 1][icol + 1] == 'P' and (place[irow + 1][icol] != 'X' or place[irow][icol + 1] != 'X'):
                return 0
            if irow != 4 and icol != 0 and place[irow + 1][icol - 1] == 'P' and (place[irow + 1][icol] != 'X' or place[irow][icol - 1] != 'X'):
                return 0
    return 1

def solution(places):
    return [check(place) for place in places]
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
  • ๋˜ ๋‹ค๋ฅธ ๊ณ ์ˆ˜์˜ ํ’€์ด
    • ์žฌ๊ท€ํ˜ธ์ถœ ๊ธธ์ฐพ๊ธฐ
def solution(places):
    result = []
    dx = [-1, 1, 0, 0]
    dy = [0, 0, -1, 1]

    def f(i, j, cnt):
        nonlocal good
        if cnt >2 : return
        if -1<i<5 and -1<j<5:
            if graph[i][j] == 'X':
                return

            if cnt != 0 and graph[i][j] == 'P':
                good = 0
                return

            graph[i][j] = 'X'

            for w in range(4):
                ni = i+dx[w]
                nj = j+dy[w]
                f(ni, nj, cnt+1)

    for case in places:
        graph = [list(r) for r in case]
        good = 1
        for i in range(5):
            for j in range(5):
                if graph[i][j]=='P':
                    f(i,j,0)

        result.append(good)
    return result
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.17ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.17ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.25ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.1MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.1MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.13ms, 10.4MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.23ms, 10.2MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.1MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.13ms, 10.3MB)

 

 

  • ์‚ฝ์งˆํ•˜๋˜ ๊ธฐ๋ก...
    • ํ•ญ์ƒ 2D๋กœ ๊ทธ๋ฆฌ๋Š” ๋ฒ„๋ฆ‡์ด ์žˆ๋Š”๋ฐ ๊ณ ์ณ์•ผ ํ•  ๋“ฏ
def solution(places):
    answer = [0] * 5
    people = []
    rooms = [[[0 for _ in range(5)] for _ in range(5)] for _ in range(5)]

    checks = (((-1,0),(-1,0)),((-1,0),(0,1)),((0,1),(-1,0)),((0,1),(0,1)),((0,1),(1,0)),((1,0),(0,1)),((1,0),(1,0)),((1,0),(0,-1)),((0,-1),(1,0)),((0,-1),(0,-1)),((0,-1),(1,0)),((1,0),(0,-1)))
    
    def check_around(ri, yi, xi):
        for chk in checks:
            for y,x in chk:
                if yi+y < 0:
                    continue
                if xi+x < 0:
                    continue
                if yi+y > 4:
                    continue
                if yi+y > 4:
                    continue
                if places[ri][yi+y][xi+x] == "P":
                    print(ri, yi+y,xi+x,"์‚ฌ๋žŒ์žˆ๋‹ค")
                    return False
        return True
    
    next_room = 0
    for ri, room in enumerate(places):
        print(ri, next_room)
        if ri == next_room:
            for yi in range(5):
                for xi in range(5):
                    if "P" == places[ri][yi][xi]:
                        if check_around(ri, yi, xi) ==  False:
                            answer[ri] = 0
                            next_room += 1
                            break
                if ri != next_room:
                    break
    print(rooms)
    return answer
def solution(places):
    answer = [0] * 5
    people = []
    ๋Œ€๊ธฐ์‹ค = [[[0 for _ in range(5)] for _ in range(5)] for _ in range(5)]
    
    for ri, room in enumerate(places):
        for yi in range(5):
            for xi in range(5):
                # ๋Œ€๊ธฐ์‹ค[ri][yi][xi] = places[ri][yi][xi]
                if "P" == places[ri][yi][xi]:
                    people.append((ri, yi, xi))
    
    def dfs(ri, yi, xi, cnt):
        cnt -= 1
        if cnt == 0:
            return True
        if places[ri][yi][xi] == "P":
            return False
        elif places[ri][yi][xi] == "O":
            ๋Œ€๊ธฐ์‹ค[ri][yi][xi] = 1
            if cnt == 0:
                return True
            else:
                if yi + 1 < 5:
                    if dfs(ri, yi+1, xi, cnt) == False:
                        return False
                if xi + 1 < 5:
                    if dfs(ri, yi, xi+1, cnt) == False:
                        return False
                if yi > 0:
                    if dfs(ri, yi-1, xi, cnt) == False:
                        return False
                if xi > 0:
                    if dfs(ri, yi, xi-1, cnt) == False:
                        return False
                return True

    def draw_room(ri):
        print("๋ฃธ:",ri)
        for y in range(5):
            for x in range(5):
                print(๋Œ€๊ธฐ์‹ค[ri][y][x],end=" ")
            print("")
        print("="*10)

    next_room = 0
    for ri, yi, xi in people:
        answer[ri] = 1
        if next_room == ri:
            if dfs(ri, yi, xi, 2) == False:
                answer[ri] = 0
                next_room += 1
        if next_room == 5:
            break
        draw_room(ri)

    #print(๋Œ€๊ธฐ์‹ค)
    return answer
def solution(places):
    answer = [0] * 5
    area_of_places = []
    man_of_places = []
    cnt_of_area = 0
    area_set = {0:0}
    
    for ri,r in enumerate(places):
        _tmp = []
        for yi,y in enumerate(r):
            _t = list(y.replace("P","O"))
            for xi in range(5):
                if places[ri][yi][xi] == "P":
                    man_of_places.append([ri, yi, xi, 0]) # Room, Y์ขŒํ‘œ, X์ขŒํ‘œ, ๊ฐ์—ผ์—ฌ๋ถ€
            _tmp.append(_t)
        area_of_places.append(_tmp)
    
    def dfs(r,y,x):
        area_of_places[r][y][x] = cnt_of_area
        if 0<x and area_of_places[r][y][x-1] == "O":
            dfs(r,y,x-1)
        if 0<y and area_of_places[r][y-1][x] == "O":
            dfs(r,y-1,x)
        if x<4 and area_of_places[r][y][x+1] == "O":
            dfs(r,y,x+1)
        if y<4 and area_of_places[r][y+1][x] == "O":
            dfs(r,y+1,x)
        return
    
    for ri,r in enumerate(places):
        for yi in range(5):
            for xi in range(5):
                if area_of_places[ri][yi][xi] == "O":
                    dfs(ri,yi,xi)
                    cnt_of_area += 1
    
    for i,(r,y,x,m) in enumerate(man_of_places):
        man_of_places[i][3] = area_of_places[r][y][x]
        try:
            area_set[man_of_places[i][3]] += 1
        except:
            area_set[man_of_places[i][3]] = 1
    
    for k, v in list(area_set.items()):
        if v <= 1:
            del area_set[k]
    
    for i in reversed(range(len(man_of_places))):
        if man_of_places[i][3] not in area_set:
            man_of_places.pop(i)
        
    man_of_places.sort(key=lambda x:x[3])
    print(man_of_places)
    print(area_set)
    
    # for ri,r in enumerate(area_of_places):
    #     for yi,y in enumerate(r):
    #         for xi,x in enumerate(y):
    #             print(area_of_places[ri][yi][xi],end=" ")
    #         print("")
    #     print("="*10)
    # answer[0] = 1
    return answer
  • ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ํ’€์ด ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ๊ฐ€์ง€๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ณ€๋ณ„๋ ฅ์ด ๋” ๋†’์„ ๋“ฏ ํ•˜๋‹ค.
    • ๊ทธ๋Ÿฌ๋ฉด ๋‚˜์ฒ˜๋Ÿผ ๋…ธ๊ฐ€๋‹ค๋กœ ํ‘ธ๋Š” ๋ฌด์ง€์„ฑ ์ฝ”๋”๋“ค์€ ๋‹ค... ใ… .ใ… 
    • 90๋„ ํšŒ์ „์„ ๋งˆ์ง€๋ง‰์— ๋„ฃ๋Š” ๊ฒƒ๋„ ๊ดœ์ฐฎ์ง€ ์•Š๋‚˜ ์‹ถ์—ˆ๋Š”๋ฐ 5x5ํฌ๊ธฐ๋ผ ๋ณ„ ์ฐจ์ด ์—†๋‹ค.
def solution(places):
    answer = [1] * 5
    h_check_patterns = ["PP", "POP"]
    v_checks_patterns = ["PP", "POP"]
    box_checks_patterns = [("PO","OP"),("PX","OP"),("PO","XP"),("OP","PO"),("XP","PO"),("OP","PX")]
    places_90d_r = []
    
    for i in range(5):
        for j in range(5):
            if answer[i] == 0:
                break
            for h_chk in h_check_patterns:
                if h_chk in places[i][j]:
                    answer[i] = 0
                    break
                    
    for i in range(5):
        for j in range(4):
            if answer[i] == 0:
                break
            for k in range(4):
                for b_chk in box_checks_patterns:
                    if places[i][j][k]+places[i][j][k+1] == b_chk[0] and places[i][j+1][k]+places[i][j+1][k+1] == b_chk[1]:
                        answer[i] = 0
                        break
    
    for i in range(5):
        _tmp = []
        for j in range(5):
            if answer[i] == 0:
                break
            _t = ""
            for k in range(5):
                _t += places[i][k][j]
            _tmp.append(_t)
        places_90d_r.append(_tmp)
        
    for i in range(5):
        for j in range(5):
            if answer[i] == 0:
                break
            for h_chk in h_check_patterns:
                if h_chk in places_90d_r[i][j]:
                    answer[i] = 0
                    break
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.13ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.4MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.4MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.13ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.2MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.4MB)

 

728x90
๋ฐ˜์‘ํ˜•