#include #include #include #include #include #include using namespace std; int main(){ int w,h,sx,sy,ex,ey; cin>>w>>h>>sx>>sy>>ex>>ey; vector> t(h,vector(w)); for(int i=0;i>t[i][j]; } } double d = 0.00000000001; double cx=sx, cy=sy; double l=0; while (true){ if(sx==ex && sy==ey)break; // find next edge double nx; if (ex > sx) { nx = floor(cx/2+1)*2; // ???? } else { nx = ceil(cx/2-1)*2; } double ny; if (ey > sy) { ny = floor(cy/2+1)*2; } else { ny = ceil(cy/2-1)*2; } // end loop if ((nx-sx)/(ex-sx) > 1){ nx = ex; } if ((ny-sy)/(ey-sy) > 1){ ny = ey; } bool end = false; bool xsame = (sx==ex); bool ysame = (sy==ey); if (!xsame && !ysame && (ey-sy)*(nx-sx) == (ex-sx)*(ny-sy)){ if (nx == ex || ny == ey) end = true; if(!end){ int a = ((int)ny)/2-1; int b = ((int)nx)/2-1; int h[4]; h[0] = t[a][b]; h[1] = t[a][b+1]; h[2] = t[a+1][b]; h[3] = t[a+1][b+1]; sort(h,h+4); int sechig = h[2]; int sh, eh; if ((ex-sx)/(ey-sy) > 0){ sh=t[a][b]; eh=t[a+1][b+1]; }else{ sh=t[a][b+1]; eh=t[a+1][b]; } l+=abs(sechig-sh)+abs(sechig-eh); } }else if(ysame || (!xsame && (nx-sx)/(ex-sx) < (ny-sy)/(ey-sy))){ // we move to nx ny = sy + (nx-sx)/(ex-sx)*(ey-sy); if (!ysame && !xsame && (nx == ex || ny == ey)) end = true; if (ysame && nx == ex) end = true; if (xsame && ny == ey) end = true; if(!end){ int a = floor(ny/2); l+= abs(t[a][((int)nx)/2]-t[a][((int)nx)/2-1]); } } else { // we move to ny nx = sx + (ny-sy)/(ey-sy)*(ex-sx); if (!ysame && !xsame && (nx == ex || ny == ey)) end = true; if (ysame && nx == ex) end = true; if (xsame && ny == ey) end = true; if(!end){ int a = floor(nx/2); l+= abs(t[((int)ny)/2][a]-t[((int)ny)/2-1][a]); } } l += sqrt((nx-cx)*(nx-cx) + (ny-cy)*(ny-cy)); cx = nx; cy = ny; if (end){ break; } } printf("%f\n",l); }