简体   繁体   English

如何在多维数组中找到彼此相邻的相同值?

[英]How to find the same values next to each other in multidimensional array?

I tried to write a function that would find in a multidimensional array (with values from 3 to 7) repeating values for at least 3 times next to each other (vertical and horizontal). 我试图编写一个可以在多维数组(值从3到7)中找到的函数,它们相互之间至少重复3次(垂直和水平)。 And if it finds that, change it for a different value. 如果找到了,请将其更改为其他值。 Let's say 1. 比方说1。

I tried to do this by loops but it doesn't seem to be a good way to solve that or I messed it up. 我试图通过循环执行此操作,但这似乎不是解决该问题的好方法,否则我将其弄乱了。 Because for some array it works, for some it does not. 因为它对某些数组有效,但对某些数组却无效。

Here's my code: 这是我的代码:

 function searching(array) { for (i = 0; i < array.length; i++) { let horizontal = array[i][0]; let howMany = 1; for (j = 1; j < array[i].length; j++) { if (horizontal === array[i][j]) { howMany += 1; horizontal = array[i][j]; if (howMany >= 3) { for (d = j; d > j - howMany; d--) { array[i][d] = 0; } } } else { horizontal = array[i][j]; howMany = 1; } } } for (v = 0; v < array.length; v++) { let vertical = array[0][v]; let howMany = 1; for (x = 1; x < array.length; x++) { if (vertical === array[x][v]) { howMany++; vertical = array[x][v]; if (howMany >= 3) { for (d = x; d > x - howMany; d--) { array[d][v] = 0; } } } else { vertical = array[x][v]; howMany = 1; } } } } 

The idea is to for example give array: 这个想法是例如给数组:

 let array = [ [3, 4, 5, 6, 7], [3, 4, 5, 6, 7], [3, 4, 5, 5, 5], [3, 5, 6, 7, 4] ] 

And the result should be: 结果应该是:

 let result = [ [1, 1, 1, 6, 7], [1, 1, 1, 6, 7], [1, 1, 1, 1, 1], [1, 5, 6, 7, 4] ] 

Thanks in advance for any ideas how to solve it :) Greetings! 在此先感谢您提出解决问题的任何想法:)问候!

I didn't understand the question at first... So this is my code: 我一开始不理解这个问题,所以这是我的代码:

 let array = [ [3, 4, 5, 6, 7], [3, 4, 5, 6, 7], [3, 4, 5, 5, 5], [3, 5, 6, 7, 4] ]; function replace(arr, target = 1) { let needToChange = []; // save the index to change const numbers = [3, 4, 5, 6, 7]; const m = arr.length; // m rows const n = arr[0].length; // n columns let mi = 0; let ni = 0; // search in row for (mi = 0; mi < m; mi++) { for (let x = 0; x < numbers.length; x++) { const num = numbers[x]; // number to search let counter = 0; // counter for this number in row mi let tempArr = []; for (ni = 0; ni < n; ni++) { const currentNum = arr[mi][ni]; if (currentNum === num) { counter++; tempArr.push([mi, ni]); } } if (counter >= 3) { needToChange = needToChange.concat(tempArr); } } } // search in column for (ni = 0; ni < n; ni++) { for (let x = 0; x < numbers.length; x++) { const num = numbers[x]; // number to search let counter = 0; // counter for this number in row mi let tempArr = []; for (mi = 0; mi < m; mi++) { const currentNum = arr[mi][ni]; if (currentNum === num) { counter++; tempArr.push([mi, ni]); } } if (counter >= 3) { needToChange = needToChange.concat(tempArr); } } } // replace needToChange.forEach(([i, j]) => { array[i][j] = target; }); } replace(array); array.forEach(row => { console.log(row.join(', ')); }) 

The problems with your current code are 您当前代码的问题是

(1) You're only checking individual rows and columns, when you need to be checking them both (eg, with [[2, 2], [2, 5]] , when at the starting position [0][0] , you need to look at both [0][1] (and its neighbors, if matching) as well as [1][0] (and its neighbors, if matching). (1)仅在需要检查单个行和列时(例如,使用[[2, 2], [2, 5]] ,在起始位置[0][0]时才检查它们[0][0] ,您需要同时查看[0][1] (及其匹配项,如果匹配)以及[1][0] (及其匹配项,如果匹配)。

(2) You're not actually checking for adjacency at the moment, you're just counting up the total number of matching elements in a particular row or column. (2)目前,您实际上并没有检查邻接关系,只是在计算特定行或列中匹配元素的总数。

Iterate over all indicies of the array. 遍历数组的所有索引。 If an index has already been checked, return early. 如果已经检查了索引,请尽早返回。 Recursively search for neighbors to that index, and if at least 3 matching in total are found, set them all to 1. Put all matching neighbors in the checked set to avoid checking them again (even if there were less than 2 adjacent matches found total). 递归搜索该索引的邻居,如果总共找到至少3个匹配项,则将它们全部设置为1。将所有匹配的邻居都放入checked集合中,以避免再次检查它们(即使发现总数少于2个的相邻匹配项也是如此) )。

 setAllAdjacentToOne([ [3, 4, 5, 6, 7], [3, 4, 5, 6, 7], [3, 4, 5, 5, 5], [3, 5, 6, 7, 4] ]); // all 9s stay, the rest get set to 1: setAllAdjacentToOne([ [2, 2, 9, 7, 7], [2, 9, 9, 9, 7], [3, 4, 4, 5, 5], [9, 4, 5, 5, 9] ]); function setAllAdjacentToOne(input) { const output = input.map(subarr => subarr.slice()); const checked = new Set(); const getKey = (x, y) => `${x}_${y}`; const width = input[0].length; const height = input.length; const getAllAdjacent = (x, y, numToFind, matches = []) => { if (x >= width || x < 0 || y >= height || y < 0) { return matches; } const key = getKey(x, y); if (!checked.has(key) && input[y][x] === numToFind) { checked.add(key); matches.push({ x, y }); getAllAdjacent(x + 1, y, numToFind, matches); getAllAdjacent(x - 1, y, numToFind, matches); getAllAdjacent(x, y + 1, numToFind, matches); getAllAdjacent(x, y - 1, numToFind, matches); } return matches; }; output.forEach((innerRowArr, y) => { innerRowArr.forEach((num, x) => { const allAdjacent = getAllAdjacent(x, y, num); if (allAdjacent.length <= 2) { return; } allAdjacent.forEach(({ x, y }) => { output[y][x] = 1; }); }); }); console.log(JSON.stringify(output)); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM