Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. //NB: map = grid
  2. // Map dimension
  3. var N = 5;
  4.  
  5. // Map init
  6. var map = [];
  7. for(var i = 0; i<N; i++) {
  8. map.push([])
  9. for(var j=0;j<N; j++) {
  10. map[i].push(
  11. { // Cells in my map are actually objects because
  12. // objects are cool and I can store multiple things
  13. t:false, // wether or not I've walked on it
  14. i: i, // row (please call it row and not i)
  15. j: j // cell in row (please call it cell and not j)
  16. })
  17. }
  18. }
  19.  
  20. // Getting the start point cell
  21. var start = map[0][0];
  22. start.t = true;
  23.  
  24.  
  25. /**
  26. This is a dirty (bad, bad, filthy code) couple of functions used to find the next possible options
  27. for this game.
  28. I find this to be a clean option because if the rules of the game change
  29. (the rules on what cell I can visit and in what order) I just have to change
  30. this, and not the recursive traversal later
  31. */
  32. function safeGet(i,j, ns) {
  33. try{ // This will try to get a cell, if it fails, it means it doesn't exist
  34. if(!map[i][j].t){ // Test if the cell has been walked on already
  35. ns.push(map[i][j]); // put it in the possible options array
  36. }
  37. }catch(e){}
  38. }
  39. function findNeighbours(i,j) {
  40. var neigh = [];
  41. //safeGet(i-1,j-1, neigh); // Uncomment if you want euclidian distance
  42. safeGet(i-1,j, neigh);
  43. //safeGet(i-1,j+1, neigh);
  44.  
  45. safeGet(i,j-1, neigh);
  46. safeGet(i,j+1, neigh);
  47.  
  48. //safeGet(i+1,j-1, neigh);
  49. safeGet(i+1,j, neigh);
  50. //safeGet(i+1,j+1, neigh);
  51.  
  52. return neigh;
  53. }
  54.  
  55. /**
  56. A function to make an array of cells (a path) into a string for debugging purpose
  57. */
  58. function printStack(stack) {
  59. return stack.map(s=>s.i+','+s.j).join`|`
  60. }
  61.  
  62. var number = 0; // The number of possible paths
  63. /**
  64. That's the actual recursion thing, where the magic happens.
  65. It takes the current node that is being walked on,
  66. and a stack (path) of previously traversed nodes, for debugging only
  67. */
  68. function go(node, stack) {
  69. // First flag the current node as traversed
  70. node.t = true;
  71.  
  72.  
  73. // Check if the node is a leaf (aka the goal node)
  74. if(node.i === N-1 && node.j === N-1) { // (aka test if its coordinates are those of the end node)
  75. console.log(printStack(stack)); // Print the path
  76. number++; // Increment the number of valid paths you found
  77. }
  78.  
  79. // Find all "children"/"neighbours" of the cell :
  80. // All the possible options to go for the next game turn
  81. var neigh = findNeighbours(node.i, node.j);
  82. /*
  83. If this was a search, we would find the best option that we want, and go for it
  84. but since we want to back-track, aka go through *all* possible options, we're just going to
  85. iterate through all of 'em
  86. */
  87. for (n of neigh) { // n is a cell in the neigh array
  88. stack.push(n); // Push it on the current path
  89. go(n, stack); // Call the recursive function with this new node
  90. stack.pop(n); // Pop (unpush) it from the current path to clean up after yourself
  91. }
  92.  
  93. // Clean up after yourself so you can start fresh with the next option of the previous recursion level
  94. // (Take some time to make sure you understand that sentence)
  95. node.t = false;
  96. }
  97.  
  98. // Actually call the thing, get the CPU to work
  99. go(start, [start])
  100. // It's working...
  101. // It's working...
  102. // It's working...
  103. // It's working...
  104.  
  105.  
  106. // Now `number` should have been increment for each of our paths
  107. console.log('Total',number)
  108.  
  109. /* Results
  110. 5*5 grid, Manhattan distance, all 4 possible directions : 8512
  111. 5*5 grid, Manhattan distance, only right and down : 70
  112. 5*5 grid, euclidian distance, all 8 directions : Timeout (exponential, too long)
  113. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement