fork download
  1. import java.util.*;
  2.  
  3. import java.lang.*;
  4. class Main
  5.  
  6. {
  7.  
  8. private static Point current = new Point(0, 0);
  9.  
  10. private static Direction lastMove = Direction.RIGHT;
  11.  
  12. private static int width;
  13.  
  14. private static int height;
  15.  
  16. private static Set<Point> visited;
  17. public static void main(String[] args) throws java.lang.Exception {
  18.  
  19. int [][] test = {
  20.  
  21. { 1, 2, 3 },
  22.  
  23. { 4, 5, 6 } ,
  24.  
  25. { 7, 8, 9 } };
  26.  
  27. spiralPrint(test);
  28.  
  29. }
  30.  
  31. public static synchronized void spiralPrint(int[][] data)
  32.  
  33. {
  34.  
  35. width = data[0].length;
  36.  
  37. height = data.length;
  38.  
  39. int numPoints = width * height;
  40.  
  41. visited = new HashSet<Point>();
  42.  
  43. while (visited.size() < numPoints)
  44.  
  45. {
  46.  
  47. System.out.print(data[current.y][current.x]);
  48.  
  49. System.out.print(" ");
  50.  
  51. visited.add(current);
  52.  
  53. current = getNextMove();
  54.  
  55. }
  56.  
  57. }
  58.  
  59. private static Point getNextMove()
  60.  
  61. {
  62.  
  63. int x = current.x;
  64.  
  65. int y = current.y;
  66.  
  67. Point nextPoint = lastMove.nextPoint(x, y);
  68.  
  69. if (! visited.contains(nextPoint)
  70.  
  71. && nextPoint.x < width && nextPoint.x >= 0
  72.  
  73. && nextPoint.y < height && nextPoint.y >= 0)
  74.  
  75. {
  76.  
  77. return nextPoint;
  78.  
  79. }
  80.  
  81. lastMove = lastMove.turnRight();
  82.  
  83. return lastMove.nextPoint(x, y);
  84.  
  85. }
  86.  
  87. private static class Point
  88.  
  89. {
  90.  
  91. private Integer x;
  92.  
  93. private Integer y;
  94. public Point(Integer x, Integer y)
  95.  
  96. {
  97.  
  98. this.x = x;
  99.  
  100. this.y = y;
  101.  
  102. }
  103.  
  104. @Override
  105.  
  106. public boolean equals(Object anotherObj)
  107.  
  108. {
  109.  
  110. Point test = (Point) anotherObj;
  111.  
  112. return test.x == this.x && test.y == this.y;
  113.  
  114. }
  115.  
  116. @Override
  117.  
  118. public int hashCode()
  119.  
  120. {
  121.  
  122. return this.x.hashCode() ^ this.y.hashCode();
  123.  
  124. }
  125.  
  126. }
  127.  
  128. private static enum Direction
  129.  
  130. {
  131.  
  132. UP, DOWN, LEFT, RIGHT;
  133. public Point nextPoint(int x, int y)
  134.  
  135. {
  136.  
  137. if (this == Direction.RIGHT)
  138.  
  139. {
  140.  
  141. return new Point(x + 1, y);
  142.  
  143. }
  144.  
  145. if (this == Direction.LEFT)
  146.  
  147. {
  148.  
  149. return new Point(x - 1, y);
  150.  
  151. }
  152.  
  153. if (this == Direction.DOWN)
  154.  
  155. {
  156.  
  157. return new Point(x, y + 1);
  158.  
  159. }
  160.  
  161. return new Point(x, y - 1);
  162.  
  163. }
  164.  
  165. public Direction turnRight()
  166.  
  167. {
  168.  
  169. if (this == Direction.RIGHT)
  170.  
  171. {
  172.  
  173. return Direction.DOWN;
  174.  
  175. }
  176.  
  177. if (this == Direction.LEFT)
  178.  
  179. {
  180.  
  181. return Direction.UP;
  182.  
  183. }
  184.  
  185. if (this == Direction.DOWN)
  186.  
  187. {
  188.  
  189. return Direction.LEFT;
  190.  
  191. }
  192.  
  193. return Direction.RIGHT;
  194.  
  195. }
  196.  
  197. }
  198.  
  199. }
Runtime error #stdin #stdout 0.01s 4980KB
stdin
Standard input is empty
stdout
Standard output is empty