fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. char[] moves = {'v','>','v','>','^','<','^','<'};
  13.  
  14. int x=0;
  15. int y=0;
  16. Point init = new Point(x,y);
  17.  
  18. List<Point> visitedHouses = new ArrayList<Point>();
  19. visitedHouses.add(init);
  20.  
  21. for (int i=0; i<moves.length; i++) {
  22. char c = moves[i];
  23. switch(c) {
  24. case '^': y--; break;
  25. case 'v': y++; break;
  26. case '>': x++; break;
  27. case '<': x--; break;
  28. default : break; // quit
  29. }
  30.  
  31. boolean found = false;
  32. for (Point p : visitedHouses) {
  33. if ((p.getX() == x) && (p.getY() == y)) {
  34. found = true;
  35. break;
  36. }
  37. }
  38.  
  39. if (!found) {
  40. visitedHouses.add(new Point(x,y));
  41. }
  42. }
  43. System.out.println("Number of houses visited at least once : " + visitedHouses.size());
  44. }
  45.  
  46. private static class Point {
  47. private final int x, y;
  48.  
  49. public Point(int x, int y) {
  50. this.x = x;
  51. this.y = y;
  52. }
  53.  
  54. public int getX() {
  55. return x;
  56. }
  57.  
  58. public int getY() {
  59. return y;
  60. }
  61.  
  62. @Override
  63. public boolean equals(Object other) {
  64. if (this == other) return true;
  65.  
  66. if (!(other instanceof Point)) return false;
  67.  
  68. Point otherPoint = (Point) other;
  69. return otherPoint.x == x && otherPoint.y == y;
  70. }
  71.  
  72. @Override
  73. public int hashCode() {
  74. return 71 * this.x + 37 * this.y + 17;
  75. }
  76. }
  77. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
Number of houses visited at least once : 7