Guest User

Untitled

a guest
Jul 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.ArrayList;
  3.  
  4. /**
  5. * Created by IntelliJ IDEA.
  6. * User: Alowan
  7. * Date: 16-dec-2009
  8. * Time: 18:15:30
  9. * This code is for educational purposes only!
  10. */
  11. public class Main {
  12. public static void main(String[] args) {
  13. ArrayList<Point> points = new ArrayList<Point>();
  14. points.add(new Point(1,1));
  15. points.add(new Point (5,5));
  16. points.add(new Point(1,2));
  17. points.add(new Point(5,4));
  18. points.add(new Point(2,2));
  19. points.add(new Point(4,4));
  20. ArrayList<ArrayList<Point>> connectedPointsList = new ArrayList<ArrayList<Point>>();
  21. while(points.size() > 0) {
  22. connectedPointsList.add(getConnected(points.get(0), points, new ArrayList<Point>()));
  23. }
  24. for(ArrayList<Point> ps : connectedPointsList) {
  25. for(Point p : ps) {
  26. System.out.println(p);
  27. }
  28. System.out.println("-----------------------");
  29. }
  30. }
  31.  
  32.  
  33. private static ArrayList<Point> getConnected(Point p, ArrayList<Point> allPoints, ArrayList<Point> connectedPoints) {
  34. connectedPoints.add(p);
  35. allPoints.remove(p);
  36. Point p1 = new Point(p.x - 1, p.y);
  37. Point p2 = new Point(p.x + 1, p.y);
  38. Point p3 = new Point(p.x, p.y - 1);
  39. Point p4 = new Point(p.x, p.y + 1);
  40. for(Point pX : new Point[] {p1, p2, p3, p4}) {
  41. if(allPoints.contains(pX) && !connectedPoints.contains(pX)) {
  42. getConnected(pX, allPoints, connectedPoints);
  43. }
  44. }
  45. return connectedPoints;
  46. }
  47. }
Add Comment
Please, Sign In to add comment