Search code examples
javascriptarrayspath-finding

pathfinding in a 2d matrix using js


hey I have been trying this problem from the past few hours. i just can't get 10 random test cases right out of 100

it would be great if anyone would help me out problem - https://www.codewars.com/kata/59604925a68b04598e00001e/train/javascript

ps there might be better way to solve the problem I just went with whatever came up in my mind.

code-

function conquerIsland(map) {

  let path=[];
  let mar=[];
  let len=0,len1=0;


for(let i=1;i<8;i++)
{


 for( let j=0;j<i;j++)
 {


   if(map[j][i]=='u')
  { 

   if(len1===0 || len1==i+j)
   {
   path.push([j,i]);
    len1=i+j;
    }

  }
   if(map[i][j]=='u')
  {  

   if(len1===0 || len1==i+j)
   {
   path.push([i,j]);
   len1=i+j;
    }

  }


   if(map[j][i]=='m')
  { 
   if(len==0 || len==i+j)
   {
   mar.push([j,i]);
    len=i+i;
    }

  }


if(map[i][j]=='m')
  {  
    if(len==0 || len==i+j){
   mar.push([i,j]);
   len=i+j; 
  }
  }


  }
 if(map[i][i]=='m')
  {  
 if(len==0 || len==i+i)
 {
  mar.push([i,i]);
  len=i+i;
 }
 }
 if(map[i][i]=='u')
  {  
  if(len1==0 || len1==i+i)
  {
   path.push([i,i]);
   len1=i*2;
  }
  }
}
if(path.length>0)
{
if(path.length==1)
{ let path1;


path1 = [].concat.apply([], path);
return path1;

}

else
{
  path.sort(sortFunction);

function sortFunction(a, b) {
    if (a[0] === b[0]) {
        return 0;
    }
    else {
        return (a[0] < b[0]) ? -1 : 1;
    }
}
 return path;
}
}
else
if(mar!=[])
{
if(mar.length==1)
{ let mar1;

mar1 = [].concat.apply([], mar);
return mar1;

}
else
{
  mar.sort(sortFunction);

function sortFunction(a, b) {
    if (a[0] === b[0]) {
        return 0;
    }
    else {
        return (a[0] < b[0]) ? -1 : 1;
    }
}
   return mar;

}
}
else {
return [];
}
}

Solution

  • Please refer to the following code (hopefully the comments are enough, if you're still confused feel free to ask):

    function conquerIsland(map) {
        // a list of all the us
        var us = [];
        // a list of all the ms
        var ms = [];
        // our position
        var pos;
        for (var i = 0; i < map.length; i ++) {
            for (var j = 0; j < map.length; j ++) {
                if (map[i][j] == "u") {
                    // found a u: push to us
                    us.push([i, j]);
                } else if (map[i][j] == "m") {
                    // found an m: push to ms
                    ms.push([i, j]);
                } else if (map[i][j] == "p") {
                    // found ourselves: update our position
                    pos = [i, j];
                }
            }
        }
        // figure out which array to search from
        var search;
        if (us.length > 0) {
            search = us;
        } else if (ms.length > 0) {
            search = ms;
        } else {
            // no us or ms, return empty array
            return [];
        }
        var mindist = Infinity;
        var coords = [];
        for (var i = 0; i < search.length; i ++) {
            // manhattan distance since no diagonals
            var dist = search[i][0] - pos[0] + search[i][1] - pos[1];
            if (dist == mindist) {
                // multiple things tied for shortest distance
                coords.push(search[i]);
            } else if (dist < mindist) {
                // new shortest distance, reset array
                mindist = dist;
                coords = [search[i]];
            }
        }
        if (coords.length == 1) {
            return coords[0];
        }
        return coords.sort(function(a, b) {
            // this basically accomplishes the sort they want
            for (var i = 0; i <= 1; i ++) {
                if (a[i] > b[i]) {
                    return -1;
                }
                if (b[i] > a[i]) {
                    return 1;
                }
            }
            return 0;
        });
    }