In the Indian Ocean, there are several small islands. A war ship is stationed in the ocean and wants to find how many of these islands are within its striking power. For simplicity, the islands are all assumed to have square shapes and again, the curvature of the earth can be ignored. The coordinates of two opposite corners of the islands are given and the position of the ship is also given. You need to find the islands in the increasing sequence of their distances from the ship. The distance is the shortest distance – the distance of the nearest point on the island boundary from the ship. Use Manhattan Distance, i.e. distance between 2 points (x1,y1) and (x2,y2) is |x1-x2| + |y1-y2|.
Constraints
1 <= N <= 100000.
-10^9 <= x1i, y1i, x2i,y2i <= 10^9.
Input Format
First line contains single integer N denoting the number of islands.
Next N lines contain 4 integers separated by space denoting the coordinates of the opposite corners of the islands (x1i, y1i) & (x2i,y2i). (1 <= i <= N)
Islands are numbered 1, 2, …, N.
The final line contains 2 integer separated by space denoting the coordinates of the warship.
Output
N integers separated by space each integer denoting the index of island sorted by distance from warship in non-decreasing order.
If 2 islands are at the same distance from warship, rank them according to their index.
Answers
Answer:
i don't know sorry.........
Explanation:
Answer:
import java.util.*;
Class pro
{
public static void main(String args[])
{
int n,arr1[][],x,y,arr2[][],sh1,sh2,sh3,sh4,d;
arr1=new int[100][4];
arr2=new int[100][2];
int t,i,j;
System.out.println(“Enter input”)
n=sc.nextInt();
for(i=0;i<n;i++)
{
arr[i][0]= sc.nextInt();
arr1[i][1]= sc.nextInt();
arr1[i][2]= sc.nextInt();
arr1[i][3]= sc.nextInt();
arr2[i][0]=i+1;
}
System.out.println(“Enter input for x”)
x=sc.nextInt();
System.out.println(“Enter input for y”)
y=sc.nextInt();
for(i=0;i<n;i++)
{
sh1=abs(x-arr1[i][0])+abs(y-arr1[i][1]);
sh2=abs(x-arr1[i][2])+abs(y-arr1[i][3]);
sh3=abs(x-arr1[i][0])+abs(y-arr1[i][3]);
sh4=abs(x-arr1[i][2])+abs(y-arr1[i][1]);
d=sh1;
if(d>sh2)
d=sh2;
if(d>sh3)
d=sh3;
if(d>sh4)
d=sh4;
arr2[i][1]=d;
}
for(i=0;i<n;i++)
for(j=0;j<n-1;i++)
{
if(arr2[j][1]>arr2[j+1][1])
{
t=arr2[j][1];
arr2[j][1]=arr2[j+1][1];
arr2[j+1][1]=t;
t=arr2[j][0];
arr2[j][0]=arr2[j+1][0];
arr2[j+1][0]=t;
}
}
for(i=0;i<n;i++)
System.out.println(“\t”+arr2[i][0]);
}