๐Ÿ—ณ ๊ฒŒ๋ฆฌ ๋งจ๋”๋ง 2

๋ฐฑ์ค€ Gold4

๐Ÿ“– ์•Œ๊ณ ๋ฆฌ์ฆ˜

๐Ÿ‘‰๐Ÿป ๋ธŒ๋ฃจํŠธ ํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ [๋ฌธ์ œ ์ œํ•œ ์กฐ๊ฑด์ด ์ž‘์„ ๋•Œ] ์จ๋ณผ๊นŒ? ํ•  ์ˆ˜ ์žˆ๋‹ค.

์‚ฌ์‹ค โ€œ์ž‘๋‹คโ€๋ผ๋Š” ๊ฐœ๋…์€ ์ƒ๋Œ€์ ์ด๊ธฐ์— ๊ฐ€๋Š ํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ด ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋ด๋ณด์ž.

  • 5 โ‰ค N โ‰ค 20
  • 1 โ‰ค A[r][c] โ‰ค 100

๋”ฑ ๋ด๋„ ์ž‘๋‹ค๋Š” Feel์ด ์˜จ๋‹ค โ€ฆ ๋•Œ๋กœ๋Š” ๋‹จ์ˆœ๋ฌด์‹ํ•œ ๋ฐฉ๋ฒ•์ด ํ†ตํ•œ๋‹ค.

๐Ÿ“ ์„ค๊ณ„

แ„‹แ…งแ†ซแ„‰แ…ณแ†ธแ„Œแ…กแ†ผ-62

๋ชจ๋“  x,y,d1,d2์— ๋Œ€ํ•˜์—ฌ ์œ„ ํ”Œ๋กœ์šฐ ์ฐจํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

์ž˜๋ชป๋œ ์„ค๊ณ„

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-07-21 แ„‹แ…ฉแ„’แ…ฎ 5 18 04

Brute Force๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ x,y,d1,d2์˜ ์กฐ๊ฑด์„ ๊ฑธ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์กฐ๊ฑด์„ ์ž์„ธํžˆ ๋ณด๋ฉด ์ตœ์ดˆ ์ถœ๋ฐœ์ง€๊ฐ€ (1,1)์ด๋‹ค.

Python์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ธ๋ฑ์Šค๊ฐ€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปดํ“จํ„ฐ๋Š” (0,0)์ด ์‹œ์ž‘์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‚˜๋Š” ์œ„์˜ ์กฐ๊ฑด ์ „๋ถ€์— -1 ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ range ๋ฒ”์œ„๋ฅผ ์ž˜๋ชป ์„ค์ •ํ•ด์ค€ ๊ฒƒ์ธ์ง€ ๊ณ„์† ์˜ค๋‹ต์ด ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฌธ์ œ ์กฐ๊ฑด์€ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ค๋˜

๊ธฐ์กด city 2์ฐจ์› ๋ฐฐ์—ด์—์„œ ์ธ๊ตฌ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ (x,y) โžก๏ธ (x-1,y-1) ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์—ฌ ๊ฐ€์ ธ์™”๋‹ค.

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป CODE

import sys
input = sys.stdin.readline
INF = int(1e9)
# Initialize
N = int(input())
city = []
for i in range(N):
    city.append(list(map(int,input().split())))

total = 0 # ์ด์›
for i in range(N):
    for j in range(N):
        total += city[i][j]

def sol(x,y,d1,d2):
    temp = [[0]*N for _ in range(N)]
    # ์„ ๊ฑฐ๊ตฌ 5 ์„ธํŒ…
    for i in range(d1+1):
        temp[x+i-1][y-i-1] = 5
        temp[x+d2+i-1][y+d2-i-1] = 5
    for i in range(d2+1):
        temp[x+i-1][y+i-1] = 5
        temp[x+d1+i-1][y-d1+i-1] = 5


    max_people = -1*INF
    min_people = INF
    
    sum1 = 0
    # ์„ ๊ฑฐ๊ตฌ 1 ์„ธํŒ…
    for i in range(1,x+d1):
        for j in range(1,y+1):
            if temp[i-1][j-1] == 5:
                break
            # ์„ ๊ฑฐ๊ตฌ 1 ์ธ์›
            sum1 += city[i-1][j-1]
    
    # Max Min Update
    if max_people < sum1:
        max_people = sum1

    if min_people > sum1:
        min_people = sum1
    
    sum2 = 0
    # ์„ ๊ฑฐ๊ตฌ 2 ์„ธํŒ… (์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ ์  ์ž‘์•„์ง€๋ฏ€๋กœ ์—ญํƒ์ƒ‰ ํ•ด์•ผํ•จ)
    for i in range(1,x+d2+1):
        for j in range(N,y,-1):
            if temp[i-1][j-1] == 5:
                break

            # ์„ ๊ฑฐ๊ตฌ 2 ์ธ์›
            sum2 += city[i-1][j-1]
    
    # Max Min Update
    if max_people < sum2:
        max_people = sum2

    if min_people > sum2:
        min_people = sum2
    
    # ์„ ๊ฑฐ๊ตฌ 3 ์„ธํŒ…
    sum3 = 0
    for i in range(x+d1,N+1):
        for j in range(1,y-d1+d2):
            if temp[i-1][j-1] == 5:
                break
            # ์„ ๊ฑฐ๊ตฌ 3 ์ธ์›
            sum3 += city[i-1][j-1]
    
    # Max Min Update
    if max_people < sum3:
        max_people = sum3

    if min_people > sum3:
        min_people = sum3
    
    sum4 = 0
    # ์„ ๊ฑฐ๊ตฌ 4 ์„ธํŒ… (์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ ์  ์ž‘์•„์ง€๋ฏ€๋กœ ์—ญํƒ์ƒ‰ ํ•ด์•ผํ•จ)
    for i in range(x+d2+1,N+1):
        for j in range(N,y-d1+d2-1,-1):
            if temp[i-1][j-1] == 5:
                break

            # ์„ ๊ฑฐ๊ตฌ 4 ์ธ์›
            sum4 += city[i-1][j-1]
    
    # Max Min Update
    if max_people < sum4:
        max_people = sum4

    if min_people > sum4:
        min_people = sum4
    
    # ์„ ๊ฑฐ๊ตฌ 5 ์ธ์›
    sum5 = total - sum1 - sum2 - sum3 - sum4

    # Max Min Update
    if max_people < sum5:
        max_people = sum5

    if min_people > sum5:
        min_people = sum5
    
    return max_people-min_people

answer = INF

for d1 in range(1,N):
    for d2 in range(1,N):
        for x in range(1,N-d1-d2+1):
            for y in range(d1+1,N-d2+1):
                answer = min(answer,sol(x,y,d1,d2))

                

print(answer)

ยฉ 2022. All rights reserved.

Powered by Hydejack v9.1.6