ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 2615 ์˜ค๋ชฉ (Brute Force)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 2. 15:44

     

    ๐Ÿ“Œ ๋ฌธ์ œ

    ์˜ค๋ชฉ์€ ๋ฐ”๋‘‘ํŒ์— ๊ฒ€์€ ๋ฐ”๋‘‘์•Œ๊ณผ ํฐ ๋ฐ”๋‘‘์•Œ์„ ๊ต๋Œ€๋กœ ๋†“์•„์„œ ๊ฒจ๋ฃจ๋Š” ๊ฒŒ์ž„์ด๋‹ค. ๋ฐ”๋‘‘ํŒ์—๋Š” 19๊ฐœ์˜ ๊ฐ€๋กœ์ค„๊ณผ 19๊ฐœ์˜ ์„ธ๋กœ์ค„์ด ๊ทธ๋ ค์ ธ ์žˆ๋Š”๋ฐ ๊ฐ€๋กœ์ค„์€ ์œ„์—์„œ๋ถ€ํ„ฐ ์•„๋ž˜๋กœ 1๋ฒˆ, 2๋ฒˆ, ... ,19๋ฒˆ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™๊ณ  ์„ธ๋กœ์ค„์€ ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ 1๋ฒˆ, 2๋ฒˆ, ... 19๋ฒˆ์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™๋Š”๋‹ค.

     

    ์œ„์˜ ๊ทธ๋ฆผ์—์„œ์™€ ๊ฐ™์ด ๊ฐ™์€ ์ƒ‰์˜ ๋ฐ”๋‘‘์•Œ์ด ์—ฐ์†์ ์œผ๋กœ ๋‹ค์„ฏ ์•Œ์„ ๋†“์ด๋ฉด ๊ทธ ์ƒ‰์ด ์ด๊ธฐ๊ฒŒ ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ์—ฐ์†์ ์ด๋ž€ ๊ฐ€๋กœ, ์„ธ๋กœ ๋˜๋Š” ๋Œ€๊ฐ์„  ๋ฐฉํ–ฅ ๋ชจ๋‘๋ฅผ ๋œปํ•œ๋‹ค. ์ฆ‰, ์œ„์˜ ๊ทธ๋ฆผ์€ ๊ฒ€์€์ƒ‰์ด ์ด๊ธด ๊ฒฝ์šฐ์ด๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ์„ฏ ์•Œ ์ด์ƒ์ด ์—ฐ์†์ ์œผ๋กœ ๋†“์ธ ๊ฒฝ์šฐ์—๋Š” ์ด๊ธด ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

     

    ์ž…๋ ฅ์œผ๋กœ ๋ฐ”๋‘‘ํŒ์˜ ์–ด๋–ค ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฒ€์€์ƒ‰์ด ์ด๊ฒผ๋Š”์ง€, ํฐ์ƒ‰์ด ์ด๊ฒผ๋Š”์ง€ ๋˜๋Š” ์•„์ง ์Šน๋ถ€๊ฐ€ ๊ฒฐ์ •๋˜์ง€ ์•Š์•˜๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๊ฒ€์€์ƒ‰๊ณผ ํฐ์ƒ‰์ด ๋™์‹œ์— ์ด๊ธฐ๊ฑฐ๋‚˜ ๊ฒ€์€์ƒ‰ ๋˜๋Š” ํฐ์ƒ‰์ด ๋‘ ๊ตฐ๋ฐ ์ด์ƒ์—์„œ ๋™์‹œ์— ์ด๊ธฐ๋Š” ๊ฒฝ์šฐ๋Š” ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค์ง€ ์•Š๋Š”๋‹ค.

    ์ž…๋ ฅ

    19์ค„์— ๊ฐ ์ค„๋งˆ๋‹ค 19๊ฐœ์˜ ์ˆซ์ž๋กœ ํ‘œํ˜„๋˜๋Š”๋ฐ, ๊ฒ€์€ ๋ฐ”๋‘‘์•Œ์€ 1, ํฐ ๋ฐ”๋‘‘์•Œ์€ 2, ์•Œ์ด ๋†“์ด์ง€ ์•Š๋Š” ์ž๋ฆฌ๋Š” 0์œผ๋กœ ํ‘œ์‹œ๋˜๋ฉฐ, ์ˆซ์ž๋Š” ํ•œ ์นธ์”ฉ ๋„์–ด์„œ ํ‘œ์‹œ๋œ๋‹ค.

    ์ถœ๋ ฅ

    ์ฒซ์ค„์— ๊ฒ€์€์ƒ‰์ด ์ด๊ฒผ์„ ๊ฒฝ์šฐ์—๋Š” 1์„, ํฐ์ƒ‰์ด ์ด๊ฒผ์„ ๊ฒฝ์šฐ์—๋Š” 2๋ฅผ, ์•„์ง ์Šน๋ถ€๊ฐ€ ๊ฒฐ์ •๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์—๋Š” 0์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฒ€์€์ƒ‰ ๋˜๋Š” ํฐ์ƒ‰์ด ์ด๊ฒผ์„ ๊ฒฝ์šฐ์—๋Š” ๋‘˜์งธ ์ค„์— ์—ฐ์†๋œ ๋‹ค์„ฏ ๊ฐœ์˜ ๋ฐ”๋‘‘์•Œ ์ค‘์—์„œ ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ๋ฐ”๋‘‘์•Œ(์—ฐ์†๋œ ๋‹ค์„ฏ ๊ฐœ์˜ ๋ฐ”๋‘‘์•Œ์ด ์„ธ๋กœ๋กœ ๋†“์ธ ๊ฒฝ์šฐ, ๊ทธ ์ค‘ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ๊ฒƒ)์˜ ๊ฐ€๋กœ์ค„ ๋ฒˆํ˜ธ์™€, ์„ธ๋กœ์ค„ ๋ฒˆํ˜ธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    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 0 0
    0 1 2 0 0 2 2 2 1 0 0 0 0 0 0 0 0 0 0
    0 0 1 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
    0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 1 2 2 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 2 1 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 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 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 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
    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 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 0 0 0 0

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    1
    3 2

     


     

    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys
    input = sys.stdin.readline
    
    board = []
    for i in range(19):
        board.append(list(map(int, input().split())))
    
    # → ↓ โ†˜ โ†—
    dx = [0, 1, 1, -1]
    dy = [1, 0, 1, 1]
    
    for x in range(19):
        for y in range(19):
            if board[x][y] != 0:
                focus = board[x][y]
    
                for i in range(4):
                    cnt = 1
                    nx = x + dx[i]
                    ny = y + dy[i]
    
                    while 0 <= nx < 19 and 0 <= ny < 19 and board[nx][ny] == focus:
                        cnt += 1
    
                        if cnt == 5:
                            # ์œก๋ชฉ ์ฒดํฌ
                            if 0 <= x - dx[i] < 19 and 0 <= y - dy[i] < 19 and board[x - dx[i]][y - dy[i]] == focus:
                                break
                            if 0 <= nx + dx[i] < 19 and 0 <= ny + dy[i] < 19 and board[nx + dx[i]][ny + dy[i]] == focus:
                                break
                            # ์œก๋ชฉ์ด ์•„๋‹ˆ๋ฉด ์„ฑ๊ณตํ•œ๊ฑฐ๋‹ˆ๊นŒ ์ข…๋ฃŒ
                            print(focus)
                            print(x + 1, y + 1)
                            sys.exit(0)
    
                        nx += dx[i]
                        ny += dy[i]
    
    print(0)

    ๐Ÿ’ก Solution

    ์œก๋ชฉ ์ฒดํฌ๊นŒ์ง€ ํ•ด์•ผํ•ด์„œ ๊นŒ๋‹ค๋กœ์› ๋˜ ๋ฌธ์ œ..๐Ÿคฎ

     

    1. ๋ฌธ์ œ์—์„œ ์œ ์‹ฌํžˆ ๋ณด์•„์•ผ ํ•  ๋ถ€๋ถ„
      • ์—ฌ์„ฏ ์•Œ ์ด์ƒ์ด ์—ฐ์†์ ์œผ๋กœ ๋†“์ธ ๊ฒฝ์šฐ์—๋Š” ์ด๊ธด ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค.
      • ์•„์ง ์Šน๋ถ€๊ฐ€ ๊ฒฐ์ •๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์—๋Š” 0์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
      • ์Šน๋ถ€๊ฐ€ ๊ฒฐ์ •๋˜์—ˆ์„ ๊ฒฝ์šฐ์—๋Š” ์—ฐ์†๋œ ๋‹ค์„ฏ ๊ฐœ์˜ ๋ฐ”๋‘‘์•Œ ์ค‘ ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ๋ฐ”๋‘‘์•Œ์˜ ์ขŒํ‘œ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. → ๋ฐฉํ–ฅ์„ (→ ↓ โ†˜ โ†—)๋กœ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š” ์ด์œ !
    2. (0, 0)๋ถ€ํ„ฐ (18, 18)๊นŒ์ง€ ๋ชจ๋“  ์ขŒํ‘œ์—์„œ ๋ฐฉํ–ฅ ๊ฐฏ์ˆ˜๋งŒํผ for๋ฌธ์„ 4๋ฒˆ ๋Œ๋ ค → ↓ โ†˜ โ†— ๋ฐฉํ–ฅ์œผ๋กœ ์ญ‰์ญ‰ 5์นธ์”ฉ์„ ์ฒดํฌ (๋ฐ”๋‘‘๋Œ์ด ๋†“์—ฌ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ=์ฆ‰ ์ขŒํ‘œ์— ์ €์žฅ๋œ ๊ฐ’์ด 0์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ)
      • x, y๋Š” ํ˜„์žฌ ์ขŒํ‘œ
      • nx, ny๋Š” ํ˜„์žฌ ์ขŒํ‘œ์—์„œ ํ•ด๋‹น ๋ฐฉํ–ฅ์œผ๋กœ 1๋งŒํผ ์ด๋™์‹œํ‚จ ์ขŒํ‘œ
    3. 5์นธ ์—ฐ์†์œผ๋กœ 1 or 2๊ฐ€ ๋†“์—ฌ์žˆ์—ˆ์„ ๊ฒฝ์šฐ ์œก๋ชฉ์ธ ์ผ€์ด์Šค๋ฅผ ๊ฑธ๋Ÿฌ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
      • ์ฒซ๋ฒˆ์งธ ๋ฐ”๋‘‘๋Œ๋ณด๋‹ค ํ•œ ์นธ ์ด์ „์˜ ๋ฐ”๋‘‘๋Œ ์ฒดํฌ
      • ๋งˆ์ง€๋ง‰ ๋ฐ”๋‘‘๋Œ๋ณด๋‹ค ํ•œ ์นธ ์ดํ›„์˜ ๋ฐ”๋‘‘๋Œ ์ฒดํฌ

    sys.exit(0)

    4์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ์—ฌ์„œ, ์Šน๋ฆฌํŒ์ •์‹œ ๋”์ด์ƒ์˜ ์ขŒํ‘œ๋ฅผ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ์ด ๋ชจ๋“  ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜์˜ฌ ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค.

             if isEnd:
                 break
         if isEnd:
            break
    if isEnd:
       break

    ์ฒ˜์Œ์—” isEnd๋ผ๋Š” ๋ถˆ๋ฆฐ๊ฐ’์„ ๋งŒ๋“ค์–ด์„œ ์ด๋Ÿฐ ์‹์œผ๋กœ ์งฐ๋Š”๋ฐ.. ๋„ˆ๋ฌด ๊ฐ€๋…์„ฑ์ด ๋ณ„๋กœ์ธ ๊ฒƒ ๊ฐ™์•„์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๋ฌผ์ƒ‰ํ•ด๋ณด๋‹ค๊ฐ€ ๋ฐœ๊ฒฌํ•œ ๊ฒƒ์ด sys.exit(0)์ž…๋‹ˆ๋‹ค. ์–˜๋Š” ๋ญํ•˜๋Š” ์•ค์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค.

    a = 10
    b = 11
    print(a+b)

    ์ด๋Ÿฌํ•œ ์ฝ”๋“œ๋ฅผ ์งœ๊ณ  ์‹คํ–‰์„ ํ–ˆ์„ ๋•Œ ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ์ฐฝ์„ ๋ณด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์„ฑ๊ณต์ ์œผ๋กœ ์ˆ˜ํ–‰๋œ ํ”„๋กœ๊ทธ๋žจ์€ ๋ณด๋‹ค์‹œํ”ผ exit code๊ฐ€ 0์œผ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

    a = 10
    b = "f"
    print(a+b)

    ๋งŒ์•ฝ ์ด๋Ÿฌํ•œ ํ„ฐ๋ฌด๋‹ˆ์—†๋Š” ์˜ค๋ฅ˜ ์ฝ”๋“œ๋ฅผ ์งœ๊ณ  ์‹คํ–‰์„ ํ•˜๋ฉด ์˜ค๋ฅ˜ ๋ฌธ์žฅ์„ ์ถœ๋ ฅํ•˜๋ฉด exit code๊ฐ€ 1๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ๊ทธ๋ ‡๊ธฐ์— ํ”„๋กœ๊ทธ๋žจ์ด ์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๊ฐ€ ๋๋‹ค๋Š” ๊ฒƒ์„ ํ‘œ์‹œํ•  ๋•Œ๋Š” ์ธ์ž๊ฐ’์„ 0์œผ๋กœ ์ฃผ๊ณ  ๋น„์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๊ฐ€ ๋๋‹ค๋Š” ๊ฒƒ์„ ํ‘œ์‹œํ•  ๋•Œ๋Š” ์ธ์ž๊ฐ’์„ 1์„ ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    sys.exit(0)์„ ์‹คํ–‰ํ•˜๋ฉด ๊ตณ์ด ๋ถˆ๋ฆฐ๊ฐ’์œผ๋กœ ์ข…๋ฃŒ์ฒดํฌ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋ฐ”๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ์ •์ƒ์ข…๋ฃŒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!๐Ÿ’ก

     

     

    ๋Œ“๊ธ€

Designed by Tistory.