javascript - 使用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;
});
}
推荐阅读
- javascript - 如何将从 http 获取的数据从 Mat-Dialog 组件传递到 Angular 6 中的父组件?
- javascript - 是否有任何 DOMstring 元素没有存储在新变量中以进行进一步处理的原因?
- vb.net - 在运行时使用 vb.net 更改 Cystal 报告中详细信息部分中的公式字段的颜色
- laravel - Laravel 5.3 中 RouteCollection.php 第 161 行中的 NotFoundHttpException
- c++ - 我可以扩展 std::map::lower_bound 以搜索非 key_type 参数吗?
- android - 如何使用其 REST API 在 Android 上访问 Google Drive 上的应用程序数据
- javascript - 使用 javascript 向元素添加事件侦听器
- c# - Virustotal Trapmine 可疑.low.ml.score
- c++ - 是否可以将 X 的容器声明为 X 的成员
- android - 使用 ndk arm-linux-androideabi-clang++ 编译器为 android 构建 grpc C++