35.42m
import sys
from collections import deque
input = sys.stdin.readline
n, m = list(map(int, input().rstrip().split()))
graph = [list(map(int, input().rstrip().split())) for _ in range(n)]
visited = [[0] * m for _ in range(n)]
h, w, sr, sc, fr, fc = list(map(int, input().rstrip().split()))
h -= 1
w -= 1
sr -= 1
sc -= 1
fr -= 1
fc -= 1
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
def bfs(node):
q = deque([node])
while q:
p_x, p_y = q.popleft()
if p_x == fr and p_y == fc:
return
for i in range(4):
n_x = p_x + dx[i]
n_y = p_y + dy[i]
if (0 <= n_x < n) and (0 <= n_y < m) and (0 <= n_x + h < n) and (0 <= n_y + w < m):
if not visited[n_x][n_y]:
if (1 not in graph[n_x][n_y:n_y+w+1]) and (1 not in graph[n_x + h][n_y:n_y+w+1]) and (1 not in [graph[i][n_y] for i in range(n_x, n_x + h + 1)]) and (1 not in [graph[i][n_y + w] for i in range(n_x, n_x + h + 1)]):
visited[n_x][n_y] = visited[p_x][p_y] + 1
q.append((n_x, n_y))
visited[sr][sc] = 1
bfs((sr, sc))
print(visited[fr][fc] - 1)
구현 + BFS의 문제였다!
문제 자체의 난이도는 어렵지 않았으나, 저 말도안되는 길이의 if문이 조금 아찔한 문제였다.