โ
์ฌํด ์๋ฐ๊ธฐ ์ผ์ฑ ์ฝํ
๊ธฐ์ถ๋ฌธ์ ์ด๋ค
๋ฌธ์ ๊ฐ ์ ๋ง ์ผ์ฑ์ค๋ฝ๋ค.. ์๋ฎฌ์๋ฎฌ
โ
์ด์ฐจ์ ๋ฆฌ์คํธ ํ์ ํ๋ ์ฝ๋๋ฅผ ๋ชป์ง์ ํ์ด๋ฅผ ์ฐธ๊ณ ํด์ ํ์๋ค๐ฅฒ
1. ์ ๋ ฅ ๋ฐ๊ธฐ
n, m, k = map(int, input().split())
board = []
for _ in range(n):
board.append(list(map(int, input().split())))
entry = [] # ์ฐธ๊ฐ์์ ์์น
for _ in range(m):
y, x = map(int, input().split())
entry.append([y-1, x-1])
exit = list(map(int, input().split())) # ์ถ๊ตฌ์ ์ขํ
exit[0] -= 1
exit[1] -= 1
answer = 0 # ์ฐธ๊ฐ์๋ค์ ์ด๋๊ฑฐ๋ฆฌ ํฉ
์ด๋ ์ขํ๊ฐ 1๋ถํฐ ์์ํ๊ธฐ ๋๋ฌธ์ ์
๋ ฅ๊ฐ์์ 1์ฉ ๋นผ์ค์ผ ํ๋ค
์ฒ์์ ์ถ๊ตฌ์ ์ขํ์์ 1 ๋นผ๋ ๊ฑธ ๋์ณค๋ค
2. ์ฐธ๊ฐ์ ์ด๋
dy = [-1, 1, 0, 0] # ์ํ์ข์ฐ
dx = [0, 0, -1, 1]
visit = set() # ์ ์ฌ๊ฐํ ์ก์ ๋ ์ฌ์ฉ
moved = [] # ์ด๋ํ ์ฐธ๊ฐ์์ ์ขํ
for y, x in entry:
dist1 = abs(y-exit[0]) + abs(x-exit[1]) # ํ์ฌ ๋จธ๋ฌผ๋ฌ ์๋ ์นธ์ ์ถ๊ตฌ๊น์ง ๊ฑฐ๋ฆฌ
flag = False # ์์ง์ ์ฌ๋ถ ํ์
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
# ์ฐธ๊ฐ์๋ ๋ฒฝ์ด ์๋ ๊ณณ(๋น์นธ or ์ถ๊ตฌ)์ผ๋ก ์ด๋ํ ์ ์๋ค
if 0 <= ny < n and 0 <= nx < n and board[ny][nx] == 0:
dist2 = abs(ny-exit[0]) + abs(nx-exit[1]) # ์์ง์ธ ์นธ์ ์ถ๊ตฌ๊น์ง์ ๊ฑฐ๋ฆฌ
if dist2 < dist1:
# ์ด๋ํ ์นธ์ด ์ถ๊ตฌ๊ฐ ์๋๋ผ๋ฉด ๋ฆฌ์คํธ์ ์ถ๊ฐํด์ค
if not (ny == exit[0] and nx == exit[1]):
moved.append([ny, nx])
visit.add((ny, nx))
flag = True
break
# ์์ง์ผ ์ ์์ผ๋ฉด ์์ง์ด์ง ์์
if not flag:
visit.add((y, x))
moved.append([y, x])
# ์์ง์๋ค๋ฉด ์ด๋๊ฑฐ๋ฆฌ๋ฅผ ๋ํด์ค
else:
answer += 1
์ํ๋ก ์์ง์ด๋ ๊ฒ ์ฐ์ ์๋๊ธฐ ๋๋ฌธ์, ์ํ๋ก ๋จผ์ ์ด๋์ ๊ณ ๋ คํด์ฃผ์๋ค
3. ๋ฏธ๋ก ํ์
โ
์ถ๊ตฌ์ ์ขํ์ ์ฐธ๊ฐ์์ ์์น๋ฅผ ์ ๋ฌ๋ฐ์, ์ ์ฌ๊ฐํ์ ์ขํ๋ฅผ ๋ฆฌํดํ๋ ํจ์๋ฅผ ๋ง๋ค์๋ค
def find(exit, visit):
start, end = [0, 0], [0, 0]
flag = False
# ์ฐธ๊ฐ์์ ์ถ๊ตฌ๋ฅผ ๋ชจ๋ ํฌํจํ๋ ค๋ฉด ์ ์ฌ๊ฐํ ๊ธธ์ด๋ 2 ์ด์์ผ ๊ฒ
for size in range(2, n):
for sy in range(0, n-size+1):
for sx in range(0, n-size+1):
ey = sy + size - 1
ex = sx + size - 1
# ์ถ๊ตฌ๊ฐ ์ ์ฌ๊ฐํ ๋ด๋ถ์ ํฌํจ๋๋์ง ํ์ธ
if sy <= exit[0] <= ey and sx <= exit[1] <= ex:
# ์ฐธ๊ฐ์๊ฐ ์ ์ฌ๊ฐํ ๋ด๋ถ์ ํฌํจ๋๋์ง ํ์ธ
for y, x in visit:
# ํ ๋ช
์ด๋ผ๋ ํฌํจ๋๋ฉด ์ข
๋ฃ
if sy <= y <= ey and sx <= x <= ex:
start = [sy, sx]
end = [ey, ex]
flag = True
break
if flag:
break
if flag:
break
if flag:
break
return start[0], start[1], end[0], end[1]
์ขํ๊ฐ ์์ ๊ฒ์ด ์ฐ์ ๋๋๋ฐ, 0๋ถํฐ ํ์ธํ๋ฏ๋ก ์กฐ๊ฑด์ ๋ง์กฑํ๋ค
โ
๋ด๊ฐ ๋ชป ํผ ๋ถ๋ถ์ด๋ค..
์ด์ฐจ์ ๋ฆฌ์คํธ 90๋ ํ์ ์ฝ๋๋ ์๊ณ ์์์ง๋ง, ๋ด๊ฐ ์๊ณ ์๋๋๋ก ์ฐ๋ฉด ๊ฒฐ๊ณผ๊ฐ ์ด์ํ๋คโน๏ธ
์์ ์ขํ๋ฅผ (0, 0)์ผ๋ก ์ด๋์์ผ์ค ๋ค ํ์ ํด์ผ ํ๋ ๊ฒ!!!!
sy, sx, ey, ex = find(exit, visit) # ๊ฐ์ฅ ์์ ์ ์ฌ๊ฐํ์ ์ขํ
rotate = copy.deepcopy(board)
m = ey - sy + 1 # ์ ์ฌ๊ฐํ์ ๊ธธ์ด
# ์ ์ฌ๊ฐํ ํ์
for i in range(sy, ey+1):
for j in range(sx, ex+1):
oy, ox = i-sy, j-sx # โญ๏ธ(sy, sx) -> (0, 0)
ry, rx = ox, m-oy-1
if board[i][j] > 0:
rotate[ry+sy][rx+sx] = board[i][j] - 1
else:
rotate[ry+sy][rx+sx] = board[i][j]
# ์ ์ฌ๊ฐํ ๋ด๋ถ์ ์๋ ์ฐธ๊ฐ์ ์ขํ ํ์
for i in range(len(moved)):
if sy <= moved[i][0] <= ey and sx <= moved[i][1] <= ex:
oy, ox = moved[i][0]-sy, moved[i][1]-sx
ry, rx = ox, m-oy-1
moved[i] = [ry+sy, rx+sx]
# ์ถ๊ตฌ ํ์
oy, ox = exit[0]-sy, exit[1]-sx
ry, rx = ox, m-oy-1
exit = [ry+sy, rx+sx]
โ
๊ทธ๋ฆฌ๊ณ ๋ ์ค์ํ๋ ๋ถ๋ถ์
์ฐธ๊ฐ์ ๋ชจ๋ ์ถ๊ตฌ๋ก ์ด๋ํด ํ์ถํ๋ฉด ๋ฐ๋ก ๋ฐ๋ณต๋ฌธ์ ์ข
๋ฃ์์ผ์ผ ํ๋ค๋ ๊ฒ์ด๋ค
๋๋ ์ฐธ๊ฐ์๋ค์ด ํ์ถํ ์ดํ์๋ ๋ฏธ๋ก๋ฅผ ํ์ ์์ผ์ ์ถ๊ตฌ ์ขํ๊ฐ ๋ง์ด๋์ค๊ฐ ๋์์๋ค
๋ฐ๋ณต๋ฌธ ์ข
๋ฃ ์์ ์ ์๊ฐ์์ด ๋ฐ๋ณต๋ฌธ ๋ง์ง๋ง์ ๋ฃ์ง ์ข ๋ง์..
import copy
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
n, m, k = map(int, input().split())
board = []
for _ in range(n):
board.append(list(map(int, input().split())))
entry = []
for _ in range(m):
y, x = map(int, input().split())
entry.append([y-1, x-1])
exit = list(map(int, input().split())) # ์ถ๊ตฌ์ ์ขํ
exit[0] -= 1
exit[1] -= 1
answer = 0 # ์ฐธ๊ฐ์๋ค์ ์ด๋๊ฑฐ๋ฆฌ ํฉ
def find(exit, visit):
start, end = [0, 0], [0, 0]
flag = False
for size in range(2, n):
for sy in range(0, n-size+1):
for sx in range(0, n-size+1):
ey = sy + size - 1
ex = sx + size - 1
if sy <= exit[0] <= ey and sx <= exit[1] <= ex:
for y, x in visit:
if sy <= y <= ey and sx <= x <= ex:
start = [sy, sx]
end = [ey, ex]
flag = True
break
if flag:
break
if flag:
break
if flag:
break
return start[0], start[1], end[0], end[1]
answer = 0
for t in range(k):
# ์ฐธ๊ฐ์ ์ด๋
visit = set()
moved = []
for y, x in entry:
dist1 = abs(y-exit[0]) + abs(x-exit[1]) # ํ์ฌ ๋จธ๋ฌผ๋ฌ ์๋ ์นธ์ ์ถ๊ตฌ๊น์ง ๊ฑฐ๋ฆฌ
flag = False # ์์ง์ ์ฌ๋ถ ํ์
for i in range(4):
ny = y + dy[i]
nx = x + dx[i]
# ์ฐธ๊ฐ์๋ ๋ฒฝ์ด ์๋ ๊ณณ(๋น์นธ or ์ถ๊ตฌ)์ผ๋ก ์ด๋ํ ์ ์๋ค
if 0 <= ny < n and 0 <= nx < n and board[ny][nx] == 0:
dist2 = abs(ny-exit[0]) + abs(nx-exit[1]) # ์์ง์ธ ์นธ์ ์ถ๊ตฌ๊น์ง์ ๊ฑฐ๋ฆฌ
if dist2 < dist1:
# ์ด๋ํ ์นธ์ด ์ถ๊ตฌ๊ฐ ์๋๋ผ๋ฉด ๋ฆฌ์คํธ์ ์ถ๊ฐํด์ค
if not (ny == exit[0] and nx == exit[1]):
moved.append([ny, nx])
visit.add((ny, nx))
flag = True
break
# ์์ง์ผ ์ ์์ผ๋ฉด ์์ง์ด์ง ์์
if not flag:
visit.add((y, x))
moved.append([y, x])
# ์์ง์๋ค๋ฉด ์ด๋๊ฑฐ๋ฆฌ๋ฅผ ๋ํด์ค
else:
answer += 1
# ๋ฏธ๋ก๋ฅผ ํ์ถํ์ ๊ฒฝ์ฐ
if len(moved) == 0:
break
# ๋ฏธ๋ก ํ์
sy, sx, ey, ex = find(exit, visit)
rotate = copy.deepcopy(board)
m = ey - sy + 1
for i in range(sy, ey+1):
for j in range(sx, ex+1):
oy, ox = i-sy, j-sx
ry, rx = ox, m-oy-1
if board[i][j] > 0:
rotate[ry+sy][rx+sx] = board[i][j] - 1
else:
rotate[ry+sy][rx+sx] = board[i][j]
for i in range(len(moved)):
if sy <= moved[i][0] <= ey and sx <= moved[i][1] <= ex:
oy, ox = moved[i][0]-sy, moved[i][1]-sx
ry, rx = ox, m-oy-1
moved[i] = [ry+sy, rx+sx]
oy, ox = exit[0]-sy, exit[1]-sx
ry, rx = ox, m-oy-1
exit = [ry+sy, rx+sx]
board = copy.deepcopy(rotate)
entry = copy.deepcopy(moved)
print(answer)
print(exit[0]+1, exit[1]+1)