![](/img/trans.png)
[英]How to create a two-dimensional array in PHP and iterate through it with Javascript
[英]How can I iterate through a two-dimensional array in a snail mode, with a single cycle?
給定一個二維數組,我想以蝸牛模式迭代它並使用一個循環打印出元素。
例如,如果給定的數組是:
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
25 26 27 28 29
30 31 32 33 34
該程序應打印出:
10 15 20 25 30 31 32 33 34 29 24 19 14 13 12 11 16 21 26 27 28 23 18 17 22
所以從左上角開始,到達數組的中心。
這是一個for循環的解決方案:
它僅在矩陣為: n> = m時有效
#include <iostream>
using namespace std;
int main()
{
// int arr[4][3] = {{0, 9, 8} , {1, 10 , 7} , {2, 11, 6} , {3, 4, 5}};
// int n = 4, m = 3;
int arr[4][4] = {{0, 11, 10, 9} , {1, 12, 15, 8} , {2, 13, 14, 7} , {3, 4, 5, 6}};
int n = 4, m = 4;
int row = 0, col = 0, turn = 0;
bool isTop = true;
for(int nrElem = 0; nrElem <= (n*m); nrElem++)
{
//This part make the left, bottom, right part ( U form )
if((row < n-1-turn) && (col != m-1) && (isTop == true))
{
cout << " " << arr[row][col];
row++;
} else {
if((row == n-1-turn) && (col < m-1-turn))
{
cout << " " << arr[row][col];
col++;
} else {
if((col == m-1-turn) && (row > 0))
{
cout << " " << arr[row][col];
row--;
} else {
isTop = false;
}
}
}
//
//And this do the top backward parsing
if((col > 0) && (isTop == false))
{
cout << " " << arr[row][col];
col--;
} else {
if (isTop == false)
{
isTop = true;
turn++;
row += turn;
col += turn;
}
}
}
cout << endl;
return 0;
}
我們可以在一個周期內完成,而無需存儲額外的矩陣。 下面的代碼假設您可以使用來自C++11
std::vector
,並且基於geeks的geeks示例。 當然,該算法也可以在沒有std::vector
情況下工作。 此外,這只蝸牛順時針方向運動,你應該改變它逆時針方向:)。 [我沒編譯代碼]
#include <iostream>
#include <vector>
using namespace std;
void printSnail(vector<vector<int>> const &matrix)
{
size_t nRow = matrix.size(); // Number of rows that are not printed yet
size_t nCol = matrix[0].size(); // Number of columns that are not printed yet
size_t k = 0;
size_t l = 0;
// Print all elements in the matrix
while (k < nRow and l < nCol)
{
// Print first row of remaining rows
for (size_t idx = l; idx < nCol; ++idx)
cout << matrix[k][idx] << ' ';
++k;
// Print last column of remaining columns
for (size_t idx = k; idx < nRow; ++idx)
cout << matrix[idx][nCol - 1] << ' ';
--nCol;
// Print last row of remaining rows
if (k < nRow)
{
for (size_t idx = nCol - 1; idx >= l; --idx)
cout << matrix[nRow - 1][idx] << ' ';
--nRow;
}
// Print the first column of the remaining columns
if (l < nCol)
{
for (size_t idx = nRow - 1; idx >= k; --idx)
cout << matrix[idx][l] << ' ';
++l;
}
}
}
以下是您的問題的簡單解決方案:
保持與陣列相同大小的二維數組( checkIfVisited
)(所有單元格初始化為0
),以便跟蹤已訪問過的單元格。 如果(i,j)
為1
則表示原始單元格已被訪問過。
我們在dir
變量的幫助下螺旋地迭代整個數組,該變量跟蹤我們當前正在移動的方向。
dir
= 0
表示向下移動, 1
表示向右移動, 2
表示向上移動, 3
表示向左移動。
當i
和j
超出限制時,或者通過從checkIfVisited
數組執行查找之前已經遍歷了下一個要遍歷的單元格時,我們更改方向。
我有一個簡單的上述算法的C ++實現:
#include <iostream>
using namespace std;
int main()
{
int arr[5][5] = {10, 11, 12, 13, 14,
15, 16, 17, 18, 19,
20, 21, 22, 23, 24,
25, 26, 27, 28, 29,
30, 31, 32, 33, 34};
int checkIfVisited[5][5] = {0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0};
int i,j,dir,countVisited;
dir = 0;
countVisited = 0;
i = 0;
j = 0;
while(countVisited<5*5)
{
cout<<arr[i][j]<<" ";
checkIfVisited[i][j]=1;
if(dir==0)
{
countVisited++;
if(i+1>4 || checkIfVisited[i+1][j]==1){
dir=1;
j++;
}
else
i++;
}
else if(dir==1)
{
countVisited++;
if(j+1>4 || checkIfVisited[i][j+1]==1){
dir=2;
i--;
}
else
j++;
}
else if(dir==2)
{
countVisited++;
if(i-1<0 || checkIfVisited[i-1][j]==1){
dir=3;
j--;
}
else
i--;
}
else
{
countVisited++;
if(j-1<0 || checkIfVisited[i][j-1]==1){
dir=0;
i++;
}
else
j--;
}
}
return 0;
}
產出: 10 15 20 25 30 31 32 33 34 29 24 19 14 13 12 11 16 21 26 27 28 23 18 17 22
下面是如何在 Javascript 中實現它
snail = function(arr) {
let [y, x] = [0, 0];
let [rs, ls, us, ds] = [0, 0, 0, 0]
let [xLimit, yLimit] = [arr.length, arr.length];
let dir = 'right'
const res = []
const len = arr[0].length * arr[0].length
const rowLen = arr[0].length
while (res.length < len) {
res.push(arr[y][x])
switch (dir) {
case 'right':
if (x + 1 < xLimit) {
x++
} else {
dir = 'down'
yLimit = rowLen - ds
rs++
y++
}
break;
case 'down':
if (y + 1 < yLimit) {
y++
} else {
dir = 'left'
xLimit = ls
ds++
x--
}
break;
case 'left':
if (x > xLimit) {
x--
} else {
dir = 'up'
yLimit = ds
ls++
y--
}
break;
case 'up':
if (y > yLimit) {
y--
} else {
dir = 'right'
xLimit = rowLen - rs
us++
x++
}
break;
default:
break;
}
}
return res
}
它沒有使用任何內置 Javascript function 所以它可以翻譯成任何語言
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.