#include #include #include #include using namespace std; using ll = long long; struct pont { int x, y; }; pont operator -(pont A, pont B) { return { A.x - B.x, A.y - B.y }; } pont operator +(pont A, pont B) { return { A.x + B.x, A.y + B.y }; } int sgn(int x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } int irany(pont A, pont B) { return sgn((ll)A.x * B.y - (ll)A.y * B.x); } int irany(pont A, pont B, pont C) { return irany(A - C, B - C); } double hossz(pont A) { return sqrt((double)A.x * A.x + (double)A.y * A.y); } int main() { ios::sync_with_stdio(false); int W, H; pont S, E; cin >> W >> H >> S.x >> S.y >> E.x >> E.y; vector> T(W / 2, vector(H / 2)); for (int i = 0; i < H / 2; i++) { for (int j = 0; j < W / 2; j++) { cin >> T[j][i]; } } if (S.y > E.y) swap(S, E); if (S.x > E.x) { reverse(T.begin(), T.end()); S.x = W - S.x; E.x = W - E.x; } pont poz = S; int koltseg = 0; while (poz.x != E.x || poz.y != E.y) { int ir = irany(S, E, poz + pont{ 1, 1 }); int i = poz.x / 2; int j = poz.y / 2; if (ir == 1) { koltseg += abs(T[i][j] - T[i + 1][j]); poz.x += 2; } else if (ir == -1) { koltseg += abs(T[i][j] - T[i][j + 1]); poz.y += 2; } else { int h = min(T[i + 1][j], T[i][j + 1]); if (h > max(T[i][j], T[i + 1][j + 1])) koltseg += 2 * h - T[i][j] - T[i + 1][j + 1]; else koltseg += abs(T[i][j] - T[i + 1][j + 1]); poz = poz + pont {2, 2}; } } cerr << "koltseg = " << koltseg << "\n"; cout.precision(20); cout << koltseg + hossz(E - S) << "\n"; }