使用js在二维矩阵中寻路

pathfinding in a 2d matrix using js

嘿,过去几个小时我一直在尝试这个问题。我无法从 100

中随机抽取 10 个测试用例

如果有人能帮助我,那就太好了 问题 - https://www.codewars.com/kata/59604925a68b04598e00001e/train/javascript

ps 可能有更好的方法来解决这个问题我只是随心所欲。

代码-

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 [];
}
}

请参考以下代码(希望评论够多,如果还有不明白的可以追问):

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;
    });
}