fork download
  1. f(3, 2) //9
  2.  
  3.  
  4. function f(w, h) {
  5. 'use strict'
  6.  
  7. var len = w*h, result = 0
  8.  
  9. if (!(1 < len)) return -1
  10.  
  11. function dry() {
  12. for (var j = 0; j < len; ++j) nc[no+j] = (0 < tc[to+j] && tc[to+j] < 6) ? tc[to+j]+1 : tc[to+j]
  13. for (; j < len+3; ++j) nc[no+j] = tc[to+j]
  14. }
  15.  
  16. function paint(x, y) {
  17. dry()
  18. nc[no+x+w*y] = 1
  19. if(++nc[no+len+2] == len) result = turn
  20. nl++, no+=len+3
  21. }
  22.  
  23. function move(x, y) {
  24. dry()
  25. nc[no+len] = x, nc[no+len+1] = y
  26. nl++, no+=len+3
  27. }
  28.  
  29. function move_paint(x, y) {
  30. v++
  31. if (tc[to+x+y*w] == 0) {move(x, y); paint(x, y)}
  32. else if (tc[to+x+y*w] == 6) move(x, y)
  33. else v--
  34. }
  35.  
  36. var tl = 0, nl = 1, tc = [], nc = new Uint8Array(len+3)
  37.  
  38. for (var turn = 1; ; turn++) {
  39. tl = nl, nl = 0, tc = nc, nc = new Uint8Array(tl*(len+3)*4)
  40. for (var no = 0, i = 0; i < tl ;i++) {
  41. var to = i*(len+3), v = 0, x = tc[to+len], y = tc[to+len+1]
  42. if (0 < x) move_paint(x-1, y)
  43. if (x < w-1) move_paint(x+1, y)
  44. if (0 < y) move_paint(x, y-1)
  45. if (y < h-1) move_paint(x, y+1)
  46. if (v == 0) move(x, y)
  47. if (result > 0) return result
  48. }
  49. }
  50.  
  51. return -1
  52.  
  53. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty