๋ฐฑ์ค (BOJ) 14500
https://www.acmicpc.net/problem/14500
์ฌ์ฉ์ธ์ด : PYTHON
1.๋ฌธ์
2.ํ์ด
๋ธ๋ฃจํธํฌ์ค ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ๋ ๋ฌธ์ ์ด๋ค. ( ๋ค๋ฅธ ํ์ด๋ฅผ ์ถ๊ฐํ์์ต๋๋ค)
์๊ฐ์ ํ์ 2์ด์ธ๋ฐ 1์ต=1์ด
์ ๋ฃฐ์ ๋์
ํ์๋ 2์ฒ8๋ฐฑ๋ง์ด ๋์ค๋ฏ๋ก ๋ธ๋ฃจํธํฌ์ค ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํด๋ ๋๋ค.
499*499*19*6 = 28,386,114
ํ์ ๋๋ ๋์นญ์ผ๋ก ๋ง๋ค ์ ์๋ ๋ชจ๋ ํ ํธ๋ก๋ฏธ๋ ธ๋ฅผ 1๊ณผ 0์ ๋ฐฐ์ด๋ก ๋ง๋ ๋ค.
19๊ฐ์ ํ ํธ๋ก๋ฏธ๋ ธ๋ค์ ๋์๊ฐ๋ฉฐ ์ข ์ด์ ๋ชจ๋ ๋ฐฐ์น ๊ฐ๋ฅํ ๊ตฌ์ญ์ ๋ฐฐ์นํ๋ค.
๊ทธ๋ฆฌ๊ณ ๋ชจ๋ ๋ฐฐ์น์ค์์ ์ต๋๊ฐ์ ์ฐพ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํตํด์ ์ฐ์ฐ์ด ๋ณต์กํด ์ง ๋๋ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ณต์กํ ์ฐ์ฐ๋ค์ ๋ถํ ํ๋ฉด ์ข ๋ ์ฝ๊ฒ ๊ตฌํํ ์ ์์์ ์๊ฒ๋์๋ค.
3.์ฝ๋
import sys
import math
input = sys.stdin.readline
N,M = map(int,input().split())
paper=[list(map(int,input().split())) for _ in range(N)]
tetros=[
[[1,1,1,1]],
[[1],[1],[1],[1]],
[[1,1],[1,1]],
[[1,0],[1,0],[1,1]],
[[1,1,1],[1,0,0]],
[[1,1],[0,1],[0,1]],
[[0,0,1],[1,1,1]],
[[0,1],[0,1],[1,1]],
[[1,1,1],[0,0,1]],
[[1,1],[1,0],[1,0]],
[[1,0,0],[1,1,1]],
[[1,0],[1,1],[0,1]],
[[0,1,1],[1,1,0]],
[[0,1],[1,1],[1,0]],
[[1,1,0],[0,1,1]],
[[1,1,1],[0,1,0]],
[[0,1],[1,1],[0,1]],
[[0,1,0],[1,1,1]],
[[1,0],[1,1],[1,0]]
]
def calc(t,i,j,paper): # ํ์ฌ tetro๊ฐ ๋ฐฐ์น๋ ์ข
์ด์ ๊ฐ์ ๊ณ์ฐํ๋ ํจ์
h,w = len(t),len(t[0])
result=0
for p in range(h):
for q in range(w):
result+=t[p][q]*paper[i+p][j+q]
return result
def place(t,n,m,paper): # ์ ๋ฌ๋ฐ์ tetro๋ฅผ ์ข
์ด์ ๋ชจ๋ ๊ฒฝ์ฐ์ ๋ฐฐ์นํ๊ณ ์ต๋๊ฐ์ ๋ฆฌํดํ๋ ํจ์
h,w = len(t),len(t[0])
result=0
for i in range(n-h+1):
for j in range(m-w+1):
tmp=calc(t,i,j,paper) # ํ์ฌ tetro๊ฐ ๋ฐฐ์น๋ ์ข
์ด์ ๊ฐ์ ๊ณ์ฐํ๋ ํจ์
if tmp>result:
result=tmp
return result
max_tetro=0
for tetro in tetros: # ๋ชจ๋ tetro๋ฅผ ์ํํ๋ฉด์ ์ข
์ด์ ๋ฐฐ์นํ๋ค.
tmp=place(tetro,N,M,paper)
if max_tetro < tmp:
max_tetro=tmp
print(max_tetro)
๋ค๋ฅธ ํ์ด
ใ ๋ชจ์์ ์ ์ธํ ๋๋จธ์ง ๋ชจ์๋ค์ ํ๋์ ์์์ ์์ ์ธ ์นธ์ ์ฐ์์ผ๋ก ์ด๋ํ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ์กฐํํ๋ฉด ๋๋ค.
- ใ ๋ชจ์์ ์ ์ธํ ๋๋จธ์ง ๋ชจ์ : solve() ํจ์์์ ๋ชจ๋ ์ ์ ๋ํด ์ฐ์์ผ๋ก 3 ์นธ ์ด๋ํ๋ dfs๋ฅผ ์ํํ๋ค
- ใ ๋ชจ์ : exSolve() ํจ์์์ ๋ชจ๋ ์ ์ ๋ํด (์,์ค๋ฅธ์ชฝ, ์๋) (์ค๋ฅธ์ชฝ,์๋,์ผ์ชฝ) (์๋,์ผ์ชฝ,์) (์ผ์ชฝ,์,์ค๋ฅธ์ชฝ) ์ ๋ฐฉ๋ฌธํ๋ dfs๋ฅผ ์ํํ๋ค
import sys
input = sys.stdin.readline
dr = [-1, 0, 1, 0]
dc = [0, 1, 0, -1]
def solve(r, c, n):
global answer
if n == 3:
result = 0
for i in range(4):
result += l[p[i][0]][p[i][1]]
answer = result if result > answer else answer
return
for d in range(4):
nr = r + dr[d]
nc = c + dc[d]
if nr<0 or nr>=N: continue
if nc<0 or nc>=M: continue
if not chk[nr][nc]:
chk[nr][nc]=True
p.append([nr,nc])
solve(nr,nc,n+1)
chk[nr][nc]=False
p.pop()
def exSolve(r,c,d,n):
global answer
if n==3:
result = 0
for a in p:
result += l[a[0]][a[1]]
answer = result if result > answer else answer
return
for di in range(d+1,4):
nr = r + dr[di]
nc = c + dc[di]
if nr<0 or nr>=N: continue
if nc<0 or nc>=M: continue
p.append([nr,nc])
exSolve(r,c,di,n+1)
p.pop()
N, M = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(N)]
chk = [[False] * M for _ in range(N)]
p = []
answer = 0
for i in range(0,N):
for j in range(0,M):
p.append([i,j])
chk[i][j]=True
solve(i,j,0)
p.pop()
chk[i][j]=False
p.clear()
for i in range(0,N):
for j in range(0,M):
p.append([i,j])
exSolve(i,j,-1,0)
p.pop()
print(answer)
'๐์ฝ๋ฉํ ์คํธ:CodingTest' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[swea 2814-ํ์ด์ฌ]์ต์ฅ ๊ฒฝ๋ก (0) | 2023.06.21 |
---|---|
[swea 1244-ํ์ด์ฌ][S/W ๋ฌธ์ ํด๊ฒฐ ์์ฉ] 2์ผ์ฐจ - ์ต๋ ์๊ธ (0) | 2023.06.21 |
[๋ฐฑ์ค 1107๋ฒ-ํ์ด์ฌ]๋ฆฌ๋ชจ์ปจ (0) | 2023.06.20 |
[๋ฐฑ์ค 11053๋ฒ-ํ์ด์ฌ]๊ฐ์ฅ ๊ธด ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด (0) | 2023.06.20 |
[๋ฐฑ์ค 11052๋ฒ-ํ์ด์ฌ]์นด๋ ๊ตฌ๋งคํ๊ธฐ (0) | 2023.06.20 |