Pagini recente » Diferente pentru problema/produse intre reviziile 6 si 5 | Cod sursa (job #1524879) | Statistici Dologa Razvan (DologaRazvan) | Cod sursa (job #2057268) | Cod sursa (job #2333879)
package tema_Divide_et_Impera_var1_probl_5;
import java.util.*;
import java.io.*;
class Punct{
int x, y;
double dist(Punct a, Punct b){
return Math.sqrt( (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
double Direct(ArrayList<Punct> pct, int dim){
double min = 9999;
for(int i = 0; i < dim; i++)
for(int j = i+1; j < dim; j++)
if(dist(pct.get(i), pct.get(j)) < min)
min = dist(pct.get(i), pct.get(j));
return min;
}
double distantaMin(ArrayList<Punct> pctx, ArrayList<Punct> pcty, int dim){
if(dim <= 3) return Direct(pctx, dim);
int mijloc = dim/2;
Punct punctMij = new Punct();
punctMij.x = pctx.get(mijloc).x;
punctMij.y = pcty.get(mijloc).y;
ArrayList<Punct> pctl = new ArrayList<>();
ArrayList<Punct> pctr = new ArrayList<>();
int i;
for(i = 0; i < pcty.size() ;i++){
if(pcty.get(i).x <= punctMij.x)
pctl.add(pcty.get(i));
else
pctr.add(pcty.get(i));
}
double dl = distantaMin(pctx, pctl, mijloc);
ArrayList<Punct> pctx_local = new ArrayList<>();
for(i=mijloc; i<dim; i++)
pctx_local.add(pctx.get(i));
double dr = distantaMin(pctx_local, pctr, dim - mijloc);
double d;
if(dl < dr) d = dl;
else d = dr;
ArrayList<Punct> b = new ArrayList<>();
for(i = 0; i < pcty.size() ; i++)
if(Math.abs(pcty.get(i).x - punctMij.x) < d)
b.add(pcty.get(i));
for(i = 0; i < b.size(); i++)
for(int j = i+1; j < b.size() && (b.get(j).y - b.get(i).y) < d; j++)
if(dist(b.get(i), b.get(j)) < d)
d = dist(b.get(i), b.get(j));
return d;
}
}
public class Main {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Punct> pctx = new ArrayList<>();
ArrayList<Punct> pcty = new ArrayList<>();
Scanner input = new Scanner(new File("date5.txt"));
int n = input.nextInt(), i;
for(i=0; i < n; i++){
Punct pctl = new Punct();
pctl.x = input.nextInt();
pctl.y = input.nextInt();
pctx.add(pctl);
pcty.add(pctl);
}
input.close();
Collections.sort(pctx, new Comparator<Punct>(){
public int compare(Punct a, Punct b){
return a.x - b.x;
}
});
Collections.sort(pcty, new Comparator<Punct>(){
public int compare(Punct a, Punct b){
return a.y - b.y;
}
});
Punct p = new Punct();
double min = p.distantaMin(pctx, pcty, pctx.size());
System.out.println("distanta minima : " + min);
/*for(Punct x:pctx)
System.out.println(x.x + " " + x.y);
for(Punct x:pcty)
System.out.println(x.x + " " + x.y);
*/
}
}