#include using namespace std; using ll = long long; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b void vprint(T &V){ for(auto v : V){ cout << v << " "; } cout << endl; } struct Point{ ll x; ll y; Point(ll x, ll y): x(x), y(y) {} }; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; ll d[100][100]; ll H, W; bool in_range(ll x, ll y){ if(0<=x && x> H >> W; ll sx, sy, gx, gy; // cin >> sx >> sy >> gx >> gy; cin >> sy >> sx >> gy >> gx; sx--; sy--; gx--; gy--; vector G(H); FOR(i, 0, H){ cin >> G.at(i); } FOR(i, 0, 100){ FOR(j, 0, 100){ d[i][j] = inf; } } queue que; que.push(Point(sx, sy)); d[sy][sx] = 0; while(!que.empty()){ auto p = que.front(); que.pop(); ll h = G[p.y][p.x] - '0'; FOR(i, 0, 4){ ll nx = p.x + dx[i]; ll ny = p.y + dy[i]; ll nh; if(in_range(nx, ny) && d[ny][nx]==inf){ nh = G[ny][nx] - '0'; ll diff = abs(nh - h); if(diff<=1){ // 行ける que.push(Point(nx, ny)); d[ny][nx] = d[p.y][p.x] + 1; } } // はしご nx += dx[i]; ny += dy[i]; if(in_range(nx, ny) && d[ny][nx]==inf){ ll height2 = G[ny][nx] - '0'; if(h==height2 && nh