Cod sursa(job #1273896)

Utilizator thereauFMI Sandu Robert Stelian thereau Data 23 noiembrie 2014 00:10:30
Problema Cele mai apropiate puncte din plan Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 5.09 kb
package Laborator5;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.io.IOException;

 class cmap {
	class punct {
		int x;
		int y;

		punct() {
			x = y = 0;
		}

		punct(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public void setX(int x) {
			this.x = x;
		}

		public void setY(int y) {
			this.y = y;
		}

		public void setXY(int x, int y) {
			this.x = x;
			this.y = y;
		}

	}

	static class punctMinim {
		punct punct1, punct2;
		double distantaPuncte;

		punctMinim(punct punct1, punct punct2) {
			this.punct1 = punct1;
			this.punct2 = punct2;
			distantaPuncte = this.calculareDistanta(punct1, punct2);
		}

		public void updatePunctMinim(punct punct1, punct punct2,
				double distantaPuncte) {
			this.punct1 = punct1;
			this.punct2 = punct2;
			this.distantaPuncte = distantaPuncte;
		}

		public double calculareDistanta(punct punct1, punct punct2) {
			return Math.sqrt(Math.pow(punct1.x - punct2.x, 2)
					+ Math.pow(punct1.y - punct2.y, 2));
		}
	}

	ArrayList<punct> punctePlan;

	cmap() {
		Scanner citire = null;
		System.out.println("constructor");
		try {
			citire = new Scanner(new java.io.File(
					"C:\\Users\\Robert-Stelian\\Desktop\\TAP\\cmap.in"));
			int dimensiune = citire.nextInt();
			System.out.println("dimensiune " + dimensiune);
			punctePlan = new ArrayList<punct>(dimensiune);
			for (int i = 0; i < dimensiune; i++)
				punctePlan.add(new punct(citire.nextInt(), citire.nextInt()));
			citire.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void sortareX(ArrayList<punct> puncte) {
		Collections.sort(puncte, new Comparator<punct>() {
			public int compare(punct a, punct b) {
				if (a.x < b.x)
					return -1;
				else
					return 1;
			}
		});
	}

	public static void sortareY(ArrayList<punct> puncte) {
		Collections.sort(puncte, new Comparator<punct>() {
			public int compare(punct a, punct b) {
				if (a.y < b.y)
					return -1;
				else
					return 1;
			}
		});
	}

	public double calculareDistanta(punct punct1, punct punct2) {
		return Math.sqrt(Math.pow(punct1.x - punct2.x, 2)
				+ Math.pow(punct1.y - punct2.y, 2));
	}

	public punctMinim distantaMinima(ArrayList<punct> sortat) {
		if (sortat.size() < 2)
			return null;
		punctMinim curent = new punctMinim(sortat.get(0), sortat.get(1));
		if (sortat.size() == 2)
			return curent;
		for (int i = 0; i < sortat.size() - 1; i++) {
			punct punctI = sortat.get(i);
			for (int j = i + 1; j < sortat.size(); j++) {
				punct punctJ = sortat.get(j);
				if (punctJ.y - punctI.y < curent.distantaPuncte) {
					double distantaPiPj = calculareDistanta(punctI, punctJ);
					if (distantaPiPj < curent.distantaPuncte)
						curent.updatePunctMinim(punctI, punctJ, distantaPiPj);
				}
			}
		}

		return curent;
	}

	public punctMinim distantaMinima(ArrayList<punct> sortatX,
			ArrayList<punct> sortatY) {
		if (sortatX.size() <= 3) {
			return distantaMinima(sortatX);
		}
		int mijloc = sortatX.size() / 2;
		ArrayList<punct> stanga = new ArrayList<punct>(sortatX.subList(0,
				mijloc));
		ArrayList<punct> dreapta = new ArrayList<punct>(sortatX.subList(mijloc,
				sortatX.size()));
		ArrayList<punct> copieCalcul = new ArrayList<punct>(stanga);
		sortareY(copieCalcul);
		punctMinim st = distantaMinima(stanga, copieCalcul);

		copieCalcul.clear();
		copieCalcul.addAll(dreapta);

		sortareY(copieCalcul);
		punctMinim dr = distantaMinima(dreapta, copieCalcul);

		copieCalcul.clear();
		punct punctMijloc = sortatX.get(sortatX.size() / 2);
		punctMinim minim = (st.distantaPuncte < dr.distantaPuncte) ? st : dr;
		for (punct p : sortatY)
			if (Math.abs(punctMijloc.x - p.x) < minim.distantaPuncte)
				copieCalcul.add(p);
		for (int i = 0; i < copieCalcul.size() - 1; i++) {
			punct punctI = copieCalcul.get(i);
			for (int j = i + 1; j < copieCalcul.size(); j++) {
				punct punctJ = copieCalcul.get(j);
				if (punctJ.y - punctI.y < minim.distantaPuncte) {
					double distantaPiPj = calculareDistanta(punctI, punctJ);
					if (distantaPiPj < minim.distantaPuncte)
						minim.updatePunctMinim(punctI, punctJ, distantaPiPj);
				}
			}
		}
		return minim;

	}

	void distantaMinima() throws IOException {
		ArrayList<punct> sortatX = new ArrayList<punct>(punctePlan);
		sortareX(sortatX);
		ArrayList<punct> sortatY = new ArrayList<punct>(punctePlan);
		sortareY(sortatY);

		punctMinim p = distantaMinima(sortatX, sortatY);
		File file = new File("cmap.in");

		// if file doesnt exists, then create it
		if (!file.exists()) {
			file.createNewFile();
		}

		FileWriter fw = new FileWriter(file.getAbsoluteFile());
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write(String.format("%.6f", p.distantaPuncte));
		bw.close();

	}
}
class Main{
	public static void main(String[] args) throws IOException {
		cmap test = new cmap();
		test.distantaMinima();
	}
	
}