#include #include #include #include using namespace std; using i64 = long long; constexpr int inf = 987654321; // N E S W constexpr array dr = {-1, 0, 1, 0}, dc = { 0, 1, 0,-1}; constexpr int dr_size = dr.size(); int main(void) { int N, V, sr, sc, gr, gc; scanf("%d%d%d%d%d%d", &N, &V, &sc, &sr, &gc, &gr); --sr, --sc, --gr, --gc; vector> G(N, vector(N, 0)); // G[N][N] for(int r=0; r> que; // 座標, 残りHP que.emplace(sr, sc, V); // cost[座標][残りHP] := 時間 vector>> cost(N, vector>(N, vector(V+1, inf))); // cost[N][N][V+1] cost[sr][sc][V] = 0; vector>> inque(N, vector>(N, vector(V+1, false))); // inque[N][N][V+1] inque[sr][sc][V] = true; while(!que.empty()) { int r, c, v; tie(r, c, v) = que.front(); que.pop(); inque[r][c][v] = false; if(r == gr && c == gc) { break; } for(int i=0; i cost[r][c][v] + 1) { cost[nr][nc][nv] = cost[r][c][v] + 1; if(!inque[nr][nc][nv]) { inque[nr][nc][nv] = true; que.emplace(nr, nc, nv); } } } } int res = inf; for(int v=1; v<=V; ++v) { res = min(res, cost[gr][gc][v]); } if(res == inf) { res = -1; } printf("%d\n", res); return 0; }