Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var MAP = [
- "############",
- "#A #",
- "# #",
- "# #",
- "# #",
- "# #",
- "# B#",
- "############"
- ], MAP_H = MAP.length, MAP_W = MAP[0].length, pointA, pointB, received = [], truePaths = [];
- (function(){
- function findPath(actpos, path){
- //console.log('-----------------------');
- //console.log('comparing ' + actpos.join(', ') + ' to ' + pointB.join(', ') + '...');
- //console.log('-----------------------');
- if(actpos.join('') == pointB.join('')){
- console.log("-----------------------");
- console.log("---- FOUND PATH!!! ----");
- console.log(path.join('; '));
- console.log('-----------------------');
- truePaths.push(path);
- return 1;
- }
- var enabled = [
- [actpos[0]-1, actpos[1]],
- [actpos[0]+1, actpos[1]],
- [actpos[0], actpos[1]-1],
- [actpos[0], actpos[1]+1]
- ];
- for(var j in enabled){
- if(enabled[j][0] < 0 || enabled[j][0] >= MAP_W || enabled[j][1] < 0 || enabled[j][1] >= MAP_H){
- continue;
- }
- else if(received[enabled[j][1] * MAP_W + enabled[j][0]] == "1"){
- continue;
- }
- var _x = path;
- _x.push(actpos);
- received[enabled[j][1] * MAP_W + enabled[j][0]] = "1";
- findPath(enabled[j], _x);
- }
- }
- //find a, b
- for(var y=0;y<MAP_H;y++){
- for(var x=0;x<MAP_W;x++){
- //prepare received(completed cords)
- if(MAP[y][x] == '#')
- received.push("1");
- else
- received.push("0");
- //check point
- if(MAP[y][x] == 'A'){
- pointA = [x, y];
- }
- else if(MAP[y][x] == 'B'){
- pointB = [x, y];
- }
- }
- }
- if(!pointA || !pointB){
- console.warn("Could not found A or B point!");
- return -1;
- }
- findPath(pointA, []);
- return truePaths.length > 0;
- })();
- console.log(truePaths);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement