Thursday, March 21, 2013

Common Meeting point in the grid

Question: There is a town which is a square grid.. every point can be reached from any other point.. there are 10 people in the grid.. find a common meeting ground for them such that the total distance traveled by those 10 people are the least..

Solution: We can find the answer by simplify the question: How to solve it if all we are dealing with a one dimensional array?? How to solve the problem if there is only two people, three people, four people and so on and find the rules!! The answer is to choose the median.

 

import java.util.Arrays;
import java.util.Comparator;


public class GridShortestDistance {
public static void main(String[] args) {
POS poses[] = { new POS(1,2),new POS(4,2),new POS(6,8),new POS(10,32),
new POS(45,3),new POS(22,56),new POS(34,43),new POS(22,12),
new POS(13,33),new POS(54,1),new POS(64,77)};
POS ps = FindMeetingPlace(poses, poses.length);

System.out.println("Meeting place ("+ps.x+","+ps.y+")");

}
static POS FindMeetingPlace(POS a[], int n)
{
POS pos = new POS(0,0);
Arrays.sort(a,new CompX());
pos.x = a[a.length/2].x;
Arrays.sort(a,new CompY());
pos.y = a[a.length/2].y;

return pos;
}
}

class CompX implements Comparator<POS> {
public int compare(POS a, POS b)
{
return ((Integer)a.x).compareTo((Integer)b.x);
}
}
class CompY implements Comparator<POS> {
public int compare(POS a, POS b)
{
return ((Integer)a.y).compareTo((Integer)b.y);
}
}

class POS
{
int x;
int y;
public POS(int a,int b)
{
x = a;
y = b;
}
}

No comments: