[๋ฐฑ์ค€/python] 14500๋ฒˆ : ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ

2021. 8. 19. 20:44

๋ฌธ์ œ

ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋ž€ ํฌ๊ธฐ๊ฐ€ 1×1์ธ ์ •์‚ฌ๊ฐํ˜•์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ด์–ด์„œ ๋ถ™์ธ ๋„ํ˜•์ด๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค.

  • ์ •์‚ฌ๊ฐํ˜•์€ ์„œ๋กœ ๊ฒน์น˜๋ฉด ์•ˆ ๋œ๋‹ค.
  • ๋„ํ˜•์€ ๋ชจ๋‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • ์ •์‚ฌ๊ฐํ˜•์˜ ๋ณ€๋ผ๋ฆฌ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ฆ‰, ๊ผญ์ง“์ ๊ณผ ๊ผญ์ง“์ ๋งŒ ๋งž๋‹ฟ์•„ ์žˆ์œผ๋ฉด ์•ˆ ๋œ๋‹ค.

์ •์‚ฌ๊ฐํ˜• 4๊ฐœ๋ฅผ ์ด์–ด ๋ถ™์ธ ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋Š” ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๋ผ๊ณ  ํ•˜๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ 5๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

์•„๋ฆ„์ด๋Š” ํฌ๊ธฐ๊ฐ€ N×M์ธ ์ข…์ด ์œ„์— ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ํ•˜๋‚˜๋ฅผ ๋†“์œผ๋ ค๊ณ  ํ•œ๋‹ค. ์ข…์ด๋Š” 1×1 ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ๊ฐ์˜ ์นธ์—๋Š” ์ •์ˆ˜๊ฐ€ ํ•˜๋‚˜ ์“ฐ์—ฌ ์žˆ๋‹ค.

ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ํ•˜๋‚˜๋ฅผ ์ ์ ˆํžˆ ๋†“์•„์„œ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๊ฐ€ ๋†“์ธ ์นธ์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๋“ค์˜ ํ•ฉ์„ ์ตœ๋Œ€๋กœ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๋Š” ๋ฐ˜๋“œ์‹œ ํ•œ ์ •์‚ฌ๊ฐํ˜•์ด ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ์นธ์„ ํฌํ•จํ•˜๋„๋ก ๋†“์•„์•ผ ํ•˜๋ฉฐ, ํšŒ์ „์ด๋‚˜ ๋Œ€์นญ์„ ์‹œ์ผœ๋„ ๋œ๋‹ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ข…์ด์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (4 ≤ N, M ≤ 500)

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ข…์ด์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ์ˆ˜๋Š” ์œ„์—์„œ๋ถ€ํ„ฐ i๋ฒˆ์งธ ์นธ, ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ j๋ฒˆ์งธ ์นธ์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜์ด๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์ˆ˜๋Š” 1,000์„ ๋„˜์ง€ ์•Š๋Š” ์ž์—ฐ์ˆ˜์ด๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๊ฐ€ ๋†“์ธ ์นธ์— ์“ฐ์ธ ์ˆ˜๋“ค์˜ ํ•ฉ์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ •๋‹ต

import sys
input = sys.stdin.readline

N, M = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(N)]
answer = []
tetrominos = [[(0,0), (0,1), (0,2), (0,3)],
              [(0,0), (1,0), (2,0), (3,0)],
              [(0,0), (0,1), (1,0), (1,1)],
              [(0,0), (1,0), (1,1), (2,1)],
              [(1,0), (1,1), (0,1), (0,2)],
              [(0,0), (0,1), (1,1), (1,2)],
              [(0,1), (1,0), (1,1), (2,0)],
              [(0,0), (0,1), (0,2), (1,1)],
              [(0,1), (1,0), (1,1), (2,1)],
              [(0,1), (1,0), (1,1), (1,2)],
              [(0,0), (1,0), (2,0), (1,1)],
              [(0,0), (1,0), (2,0), (2,1)],
              [(0,1), (1,1), (2,1), (2,0)],
              [(0,0), (0,1), (0,2), (1,0)],
              [(0,0), (0,1), (0,2), (1,2)],
              [(0,0), (0,1), (1,1), (2,1)],
              [(0,0), (0,1), (1,0), (2,0)],
              [(0,2), (1,0), (1,1), (1,2)],
              [(0,0), (1,0), (1,1), (1,2)]]

for x in range(N):
    for y in range(M):
        for tetromino in tetrominos:
            total = 0
            for _x, _y in tetromino:
                try:
                    total += board[x+_x][y+_y]
                except IndexError:
                    break
            answer.append(total)

print(max(answer))

 

๋ถ„๋ฅ˜๊ฐ€ ๋ธŒ๋ฃจํŠธํฌ์Šค๊ธธ๋ž˜ ์œ„์™€๊ฐ™์ด ํ’€์—ˆ๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ๋…ธ๊ฐ€๋‹ค๋กœ ํ‘ธ๋Š”๊ฒŒ ๋งž๋Š”๊ฑด์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค.

 

์ •์‚ฌ๊ฐํ˜• ๋„ค๊ฐœ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ธ”๋ก์€ ์œ„์™€ ๊ฐ™์€ 19๊ฐœ ๋ธ”๋ก๋ฟ์ด๋‹ค.

์ด๋ฅผ 0์„ ๊ธฐ์ค€์œผ๋กœ ์ขŒํ‘œํ™”ํ•˜์—ฌ tetrominos์— ์ €์žฅํ•œ๋‹ค.

 

์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ชจ๋“  ์ขŒํ‘œ์— ๋Œ€์ž…ํ•˜์—ฌ ์นธ์— ์“ฐ์—ฌ์žˆ๋Š” ์ˆ˜์˜ ํ•ฉ์„ answer์— ์ €์žฅํ•œ๋‹ค.

indexError๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์„ค์ •ํ•  ํ•„์š”์—†์ด try except๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฒดํฌํ•  ์ˆ˜ ์žˆ๋‹ค.

728x90

BELATED ARTICLES

more