Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. new Algorithm();
  10. }
  11. }
  12.  
  13. class Algorithm{
  14.  
  15. LinkedList<Point> points;
  16. LinkedList<Point> pointsPassed;
  17. int numPoints;
  18. Scanner sc;
  19.  
  20. public Algorithm(){
  21. points = new LinkedList<Point>();
  22. pointsPassed = new LinkedList<Point>();
  23. sc = new Scanner(System.in);
  24. System.out.print("Prosze podac ilosc punktow: ");
  25. numPoints = sc.nextInt();
  26. enterPoints();
  27. showPoints(points);
  28. createPath();
  29. showPoints(pointsPassed);
  30. }
  31.  
  32. public void enterPoints(){
  33.  
  34. for(int i=0;i<numPoints;i++){
  35. System.out.print("Prosze podac koordynaty punktu["+i+"] ");
  36. int x = sc.nextInt();
  37. int y = sc.nextInt();
  38. points.add(new Point(x, y, i));
  39. }
  40. for(Point p: points){
  41. p.fillingNeight(points);
  42. }
  43. }
  44.  
  45. public void showPoints(LinkedList<Point> points){
  46.  
  47. for(int i=0;i<points.size();i++){
  48. System.out.println(points.get(i));
  49. }
  50. }
  51.  
  52. public void createPath(){
  53.  
  54. System.out.print("Prosze podac numer punktu poczatkowego: ");
  55. int number = sc.nextInt();
  56. Point currentPoint = points.get(number);
  57. pointsPassed.add(currentPoint);
  58. deleteCurrentPoint(currentPoint);
  59. for(int i=1;i<numPoints;i++){
  60. currentPoint = currentPoint.getMin(10000);
  61. pointsPassed.add(currentPoint);
  62. deleteCurrentPoint(currentPoint);
  63. }
  64. getDist(points.get(number));
  65. }
  66.  
  67. public void deleteCurrentPoint(Point currentPoint){
  68.  
  69. for(Point p: points){
  70. p.removePoint(currentPoint);
  71. }
  72. }
  73.  
  74. public void getDist(Point startPoint){
  75. double dist = 0;
  76. for(Point p: pointsPassed)
  77. dist += p.passedDist;
  78. dist += pointsPassed.getLast().findDistance(startPoint);
  79. System.out.println("Distans ogolny jest: " + dist);
  80. }
  81.  
  82. }
  83.  
  84. class Point{
  85.  
  86. int x, y;
  87. int numPoint;
  88. double passedDist;
  89. HashMap<Point, Double> neight;
  90.  
  91. public Point(){}
  92.  
  93. public Point(int x, int y, int numPoint){
  94. this.x = x;
  95. this.y = y;
  96. this.passedDist = 0;
  97. this.numPoint = numPoint;
  98. neight = new HashMap<Point, Double>();
  99. }
  100.  
  101. public void fillingNeight(LinkedList<Point> points){
  102.  
  103. for(Point p: points){
  104. if(!p.equals(this)){
  105. neight.put(p, findDistance(p));
  106. }
  107. }
  108. }
  109.  
  110. public Point getMin(double min){
  111.  
  112. Point point = new Point();
  113. for(Point p: neight.keySet()){
  114. if(neight.get(p) < min) {
  115. min = neight.get(p);
  116. point = p;
  117. }
  118. }
  119. this.passedDist = min;
  120. return point;
  121. }
  122.  
  123. @Override
  124. public String toString() {
  125.  
  126. return "Punkt [" + numPoint + "] " + Integer.toString(x) + " " + Integer.toString(y);
  127. }
  128.  
  129. public double findDistance(Point p){
  130.  
  131. return Math.sqrt(Math.pow((p.x - this.x), 2) + Math.pow((p.y - this.y), 2));
  132. }
  133.  
  134. public void removePoint(Point p){
  135.  
  136. if(neight.containsKey(p)){
  137. neight.remove(p);
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement