我正在完成一项家庭作业,我需要找到连接到顶部的所有0,将它们更改为2,然后返回更改的金额。
到目前为止,我有:
int[][] cavern = {{1,1,1,1,1,0,1},
{1,0,0,1,1,0,1},
{1,1,1,0,0,0,1},
{1,1,0,0,1,1,1},
{1,0,1,0,1,0,1},
{1,0,1,0,0,0,1},
{0,0,0,1,1,1,0},
{1,1,1,0,0,0,1}};
//reads the size of the array
...code
//prints array as a grid (original)
...code
//find zero value in first row
...code, sets value to 2
//change connecting zeros to 2's
for(int r=0; r<cavern.length; r++)
for(int c = 0; c < cavern[0].length; c++){
if(cavern[r][c] == 2){
if((c+1) < cavern[r].length && cavern[r][c+1] == 0){cavern[r][c+1] = 2;}
else if((c-1) < cavern[r].length && cavern[r][c-1] == 0){cavern[r][c-1] = 2;}
else if((r+1) < cavern.length && cavern[r+1][c] == 0){cavern[r+1][c] = 2;}
else if((r-1) < cavern.length && cavern[r-1][c] == 0){cavern[r-1][c] = 2;}
}
}
//Print the size of the array
...code
//prints array as a grid with colored values
...code
结果:
1 1 1 1 1 2 1
1 0 0 1 1 2 1
1 1 1 0 2 2 1
1 1 0 0 1 1 1
1 0 1 0 1 0 1
1 0 1 0 0 0 1
0 0 0 1 1 1 0
1 1 1 0 0 0 1
正如您所看到的,2s仅在几个之后停止,并且不会继续循环到零的末尾。我觉得我错过了一些简单的东西,或者犯了一个愚蠢的错误。我多年没有完成任何编程,而且一些基本的东西正在让我不知所措。
我也无法计算出现在阵列中的所有2个,无论我尝试什么,我得到的值为1,2,否则会返回错误。
任何帮助都将不胜感激。
编辑:根据第一个答案的建议,我现在得到了这个: 1 1 1 1 1 2 1
1 0 0 1 1 2 1
1 1 1 2 0 2 1
1 1 0 2 1 1 1
1 2 1 2 1 2 1
1 2 1 2 0 2 1
0 0 0 1 1 1 0
1 1 1 2 2 2 1
答案 0 :(得分:0)
int numberOfTwos = 0;
for(int r = 0; r < cavern.length; r++){
for(int c = 0; c < cavern[r].length; c++){
if(cavern[r][c] == 0){
// We check that we're not on the last row, and also if the next
// row is also equals to 0
if(r < cavern.length-1 && cavern[r+1][c]==0){
numberOfTwos++;
// We change both 0's to 2's
cavern[r][c]=2
cavern[r+1][c]=2;
}
// We also change if we're on a 2 and not on the last column
else if(c < cavern[0].length-1 && cavern[r][c]==2){
// if the next one is a 0, we change it for a 2
if(cavern[r][c+1] ==0){
//numberOfTwos++;
cavern[r][c+1] =2;
}
// if the one under is also a 0, we change it for a 2
else if(r < cavern.length-1 && cavern[r+1][c] ==0){
numberOfTwos++;
cavern[r+1][c] =2;
}
}
// We only gonna change the current cavern[r][c] if there's no
//more rows to check
else if(r == cavern.length-1){
numberOfTwos++;
cavern[r][c]=2;
}
}
}
}
我认为这应该有效。它将检查每个cavern[r][c]
的值是否等于零,然后它将检查下面的行是否也是零(只有在该行之下有一行是r < column.length-1
),并且如果是的话,它会将它们的值更改为2。否则它只会更改cavern[r][c]
的值,因为下面没有行。
编辑:你不能从上到下连接0,它在最后一个之前停止2行。我不知道这是不是一个错字。
答案 1 :(得分:0)
在我看来,这是一种递归解决方案更优雅的东西。
有一个名为changeZeroToTwoPlusPartners()
的函数可以完成单个单元格的工作, plus 所有有效的伙伴单元格。在伪代码中,这将是:
def changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos, ypos):
# No action if we have moved outside the array.
if xpos < 0 or xpos >= xwidth or ypos < 0 or ypos >= yheight:
return
# No action if cell is not a zero.
if array[xpos,ypos] != 0:
return
# Change it then do all partners of that cell (right, left, down, up).
array[xpos,ypos] = 2
changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos+1, ypos)
changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos-1, ypos)
changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos, ypos+1)
changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos, ypos-1)
前面的检查可以防止您移出阵列并处理任何非零单元格。
将单元格立即设置为2,并结合上面的过滤掉非零单元格的检查,将阻止无限回归,因为移回已经处理过的单元格将被过滤为不为零更多(现在将是两个)。
然后,要使用此功能,您只需要为第一行中的每个单元格调用它:
for xpos in 0 to xwidth-1 inclusive:
changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos, 0)
就概念验证而言,这里有一些Python代码可以满足您的需求:
def changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos, ypos):
if xpos < 0 or xpos >= xwidth or ypos < 0 or ypos >= yheight:
return
if array[ypos][xpos] != 0:
return
array[ypos][xpos] = 2
changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos+1, ypos)
changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos-1, ypos)
changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos, ypos+1)
changeZeroToTwoPlusPartners(array, xwidth, yheight, xpos, ypos-1)
cavern = [[1,1,1,1,1,0,1],
[1,0,0,1,1,0,1],
[1,1,1,0,0,0,1],
[1,1,0,0,1,1,1],
[1,0,1,0,1,0,1],
[1,0,1,0,0,0,1],
[0,0,0,1,1,1,0],
[1,1,1,0,0,0,1]]
for line in cavern:
print(line)
for xpos in range(len(cavern[0])):
changeZeroToTwoPlusPartners(cavern, len(cavern[0]), len(cavern), xpos, 0)
print("-----")
for line in cavern:
print(line)
输出是(如果您手动执行此操作,您会看到转换是正确的,将顶行的每个0
更改为2
,然后递归更改任何0
相邻到2
到另一个2
):
[1, 1, 1, 1, 1, 0, 1]
[1, 0, 0, 1, 1, 0, 1]
[1, 1, 1, 0, 0, 0, 1]
[1, 1, 0, 0, 1, 1, 1]
[1, 0, 1, 0, 1, 0, 1]
[1, 0, 1, 0, 0, 0, 1]
[0, 0, 0, 1, 1, 1, 0]
[1, 1, 1, 0, 0, 0, 1]
-----
[1, 1, 1, 1, 1, 2, 1]
[1, 0, 0, 1, 1, 2, 1]
[1, 1, 1, 2, 2, 2, 1]
[1, 1, 2, 2, 1, 1, 1]
[1, 0, 1, 2, 1, 2, 1]
[1, 0, 1, 2, 2, 2, 1]
[0, 0, 0, 1, 1, 1, 0]
[1, 1, 1, 0, 0, 0, 1]