Below given c++ code is for CRAN01 spoj
or An experiment by penny.
Here to find
the logic you can test it for some small test by pen & paper. For myself I have
checked for approx. 20 test cases then I got the logic .
Logic is
maximum corner distance from given point is the minimum time to fill the entire
box.
For example I am taking a grid of 7 x 7 and starting position as (4,5).
Here in the
above table there are four corners coloured as yellow green black & blue
and the starting position is red coloured box. Now the corners coloured with yellow
and black are at maximum distances = 7 So here answer will be 7 .I get this
logic from observation so if you want to clear your logic please use pen &
paper and draw some small test cases or use brute-force.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,x,y;
scanf("%d%d%d%d",&n,&m,&x,&y);
int r1,r2,r3,r4,result;
r1 = abs(x-1) + abs(y-1);
r2 = abs(x-1) + abs(y-m);
r3 = abs(x-n) + abs(y-1);
r4 = abs(x-n) + abs(y-m);
result = max(r1,max(r2,max(r3,r4)));
printf("%d\n",result);
}
return 0;
}
here also python 2.7 code is for CRAN01 spoj
or An experiment by penny.
import sys,math
t=int(sys.stdin.readline())
while t:
n,m = map(int,sys.stdin.readline().split())
x,y = map(int,sys.stdin.readline().split())
r1 = abs(x-1) + abs(y-1)
r2 = abs(x-1) + abs(y-m)
r3 = abs(x-n) + abs(y-1)
r4 = abs(x-n) + abs(y-m)
result = max(r1,max(r2,max(r3,r4)))
sys.stdout.write(str(result))
sys.stdout.write("\n")
t=t-1