Advertisement
Guest User

Untitled

a guest
Jul 20th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var MAP = [
  2.         "############",
  3.         "#A         #",
  4.         "#          #",
  5.         "#          #",
  6.         "#          #",
  7.         "#          #",
  8.         "#         B#",
  9.         "############"
  10.     ], MAP_H = MAP.length, MAP_W = MAP[0].length, pointA, pointB, received = [], truePaths = [];
  11.  
  12. (function(){
  13.  
  14.     function findPath(actpos, path){
  15.         //console.log('-----------------------');
  16.         //console.log('comparing ' + actpos.join(', ') + ' to ' + pointB.join(', ') + '...');
  17.         //console.log('-----------------------');
  18.  
  19.         if(actpos.join('') == pointB.join('')){
  20.             console.log("-----------------------");
  21.             console.log("---- FOUND PATH!!! ----");
  22.             console.log(path.join('; '));
  23.             console.log('-----------------------');
  24.  
  25.             truePaths.push(path);
  26.  
  27.             return 1;
  28.         }
  29.  
  30.         var enabled = [
  31.             [actpos[0]-1, actpos[1]],
  32.             [actpos[0]+1, actpos[1]],
  33.             [actpos[0], actpos[1]-1],
  34.             [actpos[0], actpos[1]+1]
  35.         ];
  36.  
  37.         for(var j in enabled){
  38.             if(enabled[j][0] < 0 || enabled[j][0] >= MAP_W || enabled[j][1] < 0 || enabled[j][1] >= MAP_H){
  39.                 continue;
  40.             }
  41.  
  42.             else if(received[enabled[j][1] * MAP_W + enabled[j][0]] == "1"){
  43.                 continue;
  44.             }
  45.  
  46.             var _x = path;
  47.             _x.push(actpos);
  48.  
  49.             received[enabled[j][1] * MAP_W + enabled[j][0]] = "1";
  50.             findPath(enabled[j], _x);
  51.         }
  52.     }
  53.  
  54.     //find a, b
  55.     for(var y=0;y<MAP_H;y++){
  56.         for(var x=0;x<MAP_W;x++){
  57.  
  58.             //prepare received(completed cords)
  59.             if(MAP[y][x] == '#')
  60.                 received.push("1");
  61.             else
  62.                 received.push("0");
  63.  
  64.             //check point
  65.             if(MAP[y][x] == 'A'){
  66.                 pointA = [x, y];
  67.             }
  68.             else if(MAP[y][x] == 'B'){
  69.                 pointB = [x, y];
  70.             }
  71.         }
  72.     }
  73.  
  74.     if(!pointA || !pointB){
  75.         console.warn("Could not found A or B point!");
  76.         return -1;
  77.     }
  78.  
  79.  
  80.     findPath(pointA, []);
  81.  
  82.     return truePaths.length > 0;
  83. })();
  84.  
  85. console.log(truePaths);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement