fork(1) download
  1. int getValue(x, y, width, height) {
  2. int acc = width * height
  3. int outerLayers = [x, y, (width - 1 - x), (height - 1 - y)].min()
  4. width -= 2*outerLayers
  5. height -= 2*outerLayers
  6. x -= outerLayers
  7. y -= outerLayers
  8. for (acc -= width * height; y != 0; acc += height - 1)
  9. (x, y, height, width) = [y, width - 1 - x, width, height]
  10. return acc + x + 1
  11. }
  12.  
  13. def printArray(width, height) {
  14. height.times { y ->
  15. width.times { x ->
  16. print getValue(x, y, width, height).toString().padLeft(4)
  17. }
  18. println()
  19. }
  20. }
  21.  
  22. printArray(*System.in.newReader().readLine().split()*.toInteger())
  23.  
  24.  
Success #stdin #stdout 1.43s 332608KB
stdin
5 4
stdout
   1   2   3   4   5
  14  15  16  17   6
  13  20  19  18   7
  12  11  10   9   8