ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] ๊ธฐ๋‘ฅ๊ณผ ๋ณด ์„ค์น˜ with Python
    PS 2021. 11. 28. 02:32
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ programmers - [๊ธฐ๋‘ฅ๊ณผ ๋ณด ์„ค์น˜]

    ๐Ÿ’ก ์กฐ๊ฑด

    1. 5 <= n <= 100
      1 <= ์ž…๋ ฅ๋ฐ›์„ ๊ธฐ๋‘ฅ๊ณผ ๋ณด์˜ ๊ฐœ์ˆ˜ <= 1000
      `์ž…๋ ฅ๋ฐ›์„ ๊ธฐ๋‘ฅ ํ˜น์€ ๋ณด์˜ ์ •๋ณด์˜ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜ == 4'
    1. ๋ฐ”๋‹ฅ์— ๋ณด๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.
      ๋ฒฝ๋ฉด์„ ๋ฒ—์–ด๋‚˜๊ฒŒ ์„ค์น˜ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.
      ๊ตฌ์กฐ๋ฌผ์€ ๊ต์ฐจ์  ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ณด๋Š” ์˜ค๋ฅธ์ชฝ, ๊ธฐ๋‘ฅ์€ ์œ„์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ์„ค์น˜ ๋˜๋Š” ์‚ญ์ œํ•œ๋‹ค.
      ๊ตฌ์กฐ๋ฌผ์ด ๊ฒน์น˜๊ฑฐ๋‚˜, ์—†๋Š” ๊ฒƒ์„ ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
    1. ๊ตฌํ˜„&์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ
    1. ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” x, y, ๊ธฐ๋‘ฅ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

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

    def check(answer):
        for x, y, a in answer:
            if a:
                # ๋ณด
                """๋ณด๋Š” ํ•œ์ชฝ ๋ ๋ถ€๋ถ„์ด ๊ธฐ๋‘ฅ ์œ„์— ์žˆ๊ฑฐ๋‚˜, ๋˜๋Š” ์–‘์ชฝ ๋ ๋ถ€๋ถ„์ด ๋‹ค๋ฅธ ๋ณด์™€ ๋™์‹œ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."""
                if y != 0 and([x + 1, y - 1, 0] in answer or [x, y - 1, 0] in answer or ([x - 1, y, 1] in answer and [x + 1, y, 1] in answer)):
                    continue
                else:
                    return False
            else:
                # ๊ธฐ๋‘ฅ
                """๊ธฐ๋‘ฅ์€ ๋ฐ”๋‹ฅ ์œ„์— ์žˆ๊ฑฐ๋‚˜ ๋ณด์˜ ํ•œ์ชฝ ๋ ๋ถ€๋ถ„ ์œ„์— ์žˆ๊ฑฐ๋‚˜, ๋˜๋Š” ๋‹ค๋ฅธ ๊ธฐ๋‘ฅ ์œ„์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."""
                if y == 0 or [x - 1, y, 1] in answer or [x, y, 1] in answer or [x, y - 1, 0] in answer:
                    continue
                else:
                    return False
        return True
    
    
    def solution(n, build_frame):
        answer = []
    
        for frame in build_frame:
            x, y, a, b = frame
    
            if b:
                # install
                answer.append([x, y, a])
                if not check(answer):
                    answer.remove([x, y, a])
            else:
                # remove
                answer.remove([x, y, a])
                if not check(answer):
                    answer.append([x, y, a])
    
        return sorted(answer)

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

    ์˜ˆ์ œ

    n = 5
    build_frame = [[1,0,0,1],[1,1,1,1],[2,1,0,1],[2,2,1,1],[5,0,0,1],[5,1,0,1],[4,2,1,1],[3,2,1,1]]

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

    [[1,0,0],[1,1,1],[2,1,0],[2,2,1],[3,2,1],[4,2,1],[5,0,0],[5,1,0]]

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

    1. build_frame์„ ์ˆœํšŒํ•˜๋ฉด์„œ, ๊ฐ frame์—์„œ x, y, a, b ๋ฅผ ๊ตฌ๋ณ„ํ•˜์—ฌ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ํ•ฉ๋‹ˆ๋‹ค.
      a ๋Š” ๊ธฐ๋‘ฅ๊ณผ ๋ณด์˜ ๊ตฌ๋ณ„, b๋Š” ์‚ฝ์ž…, ์‚ญ์ œ์˜ ๊ตฌ๋ณ„์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    2. ๋งŒ์•ฝ b๊ฐ€ ์‚ฝ์ž…์— ํ•ด๋‹นํ•  ๊ฒฝ์šฐ, (x, y, a) ๋ฅผ answer์— ์ €์žฅํ•˜๊ณ  check()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ
      ๋ฐฉ๊ธˆ ์„ธ์šด ๊ตฌ์กฐ๋ฌผ์ด ์ „์ฒด์ ์œผ๋กœ ๊ฐ€๋Šฅํ•œ ๊ตฌ์กฐ๋ฌผ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

      check ํ•จ์ˆ˜์—์„œ False๋ฅผ ๋ฐ˜ํ™˜ํ•œ ๊ฒฝ์šฐ, answer์—์„œ (x, y, a)๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
      check ํ•จ์ˆ˜์—์„œ True๋ฅผ ๋ฐ˜ํ™˜ํ•œ ๊ฒฝ์šฐ, ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.

    3. ๋งŒ์•ฝ b๊ฐ€ ์‚ญ์ œ์— ํ•ด๋‹นํ•  ๊ฒฝ์šฐ, (x, y, a) ๋ฅผ answer์—์„œ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
      check ํ•จ์ˆ˜์—์„œ False๋ฅผ ๋ฐ˜ํ™˜ํ•œ ๊ฒฝ์šฐ, answer์—์„œ (x, y, a)๋ฅผ ๋‹ค์‹œ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
      check ํ•จ์ˆ˜์—์„œ True๋ฅผ ๋ฐ˜ํ™˜ํ•œ ๊ฒฝ์šฐ, ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.

    4. check ํ•จ์ˆ˜์—์„œ๋Š” ํ˜„์žฌ ๊ตฌ์กฐ๋ฌผ์ด ์ •์ƒ์ธ์ง€์— ๋Œ€ํ•ด ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.

    5. ํ˜„์žฌ ๊ฒ€์‚ฌํ•˜๋Š” ๊ตฌ์กฐ๋ฌผ์˜ ์ข…๋ฅ˜๊ฐ€ ๋ณด๋ผ๋ฉด ์•„๋ž˜์˜ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

      if y != 0 and([x + 1, y - 1, 0] in answer or [x, y - 1, 0] in answer or ([x - 1, y, 1] in answer and [x + 1, y, 1] in answer)):
    6. ํ˜„์žฌ ๊ฒ€์‚ฌํ•˜๋Š” ๊ตฌ์กฐ๋ฌผ์˜ ์ข…๋ฅ˜๊ฐ€ ๊ธฐ๋‘ฅ์ด๋ผ๋ฉด ์•„๋ž˜์˜ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, True๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

      if y == 0 or [x - 1, y, 1] in answer or [x, y, 1] in answer or [x, y - 1, 0] in answer:

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. check ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ํž˜์ด ๋“ค์—ˆ๋‹ค.
    2. ๊ตฌํ˜„ ์—ฐ์Šต์€ ๋ฐ˜๋ณต๋งŒ์ด ๋‹ต์ด ์•„๋‹Œ๊ฐ€ ์‹ถ๋‹ค. ๋” ์—ด์‹ฌํžˆ ํ’€์–ด์•ผ๊ฒ ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.