统计二维数组中的数字总数。

huangapple go评论108阅读模式
英文:

Count all amount of numbers in 2d array

问题

我正在尝试编写控制台扫雷游戏,但无法弄清如何迭代遍历二维数组,并计算每个元素周围的"-1"的数量。我的数组如下所示:

[0 0 0 0 0]
[0 0 -1 0 0]
[0 0 0 0 0]
[0 0 0 -1 -1]
[0 0 0 -1 -1]

其中"-1"表示地雷。我尝试使用以下代码简单地进行迭代:

for i := 0; i < row; i++ {
    numb := 0
    for j := 0; j < col; j++ {
        if ary[i-1][j-1] == -1 {
            numb++
        }
    }
    if ary[i-1][j] == 1 {
        numb++
    }
    //省略其余代码
}

但这导致了大量的代码,很难阅读和理解。有没有更灵活的方法,在"for-iterator"中检查current周围的所有元素,以便结果看起来像这样:

[0 1 1 1 0]
[0 1 -1 1 0]
[0 1 2 2 1]
[0 0 1 -1 -1]
[0 0 1 -1 -1]
英文:

I'm trying to write console minesweeper, but cant figure out how to iterate over 2d array, and count amount of "-1" around each of element. My array looks like

[0 0 0 0 0]
[0 0 -1 0 0]
[0 0 0 0 0]
[0 0 0 -1 -1]
[0 0 0 -1 -1]

Where "-1" that is mine. I've tried to simply iterate over it with code

for i := 0; i &lt; row; i++ {
numb := 0
	for j := 0; j &lt; col; j++ {
         if ary[i-1][j-1] == -1 {
             numb ++ } 
		 }
         if ary[i-1][j] == 1 {
             numb ++ }
         //rest of code omitted
       
	}

But that resulted in huge amout of code, which is hard to read and understand. Is there more flexible way, to check all elements around current in for-iterator, so in result it would look like

[0 1  1   1 0]
[0 1 -1   1 0]
[0 1  2   2 1]
[0 0  1  -1 -1]
[0 0  1  -1 -1]

答案1

得分: 2

for i := 0; i < 行数; i++ {
    for j := 0; j < 列数; j++ {
        if ary[i][j] == -1 {
            for k := max(0, i-1); k <= min(行数-1, i+1); k++ {
                for l := max(0, j-1); l <= min(列数-1, j+1); l++ {
                    if ary[k][l] != -1 {
                        ary[k][l]++
                    }
                }
            }
        }
    }
}

前两个外部循环遍历数组。如果观察到的数字是 -1,则增加周围的所有单元格,除非该单元格上有地雷。为了避免检查 ary[k][l] 是否超出边界,使用 min/max 的技巧在之前计算边界,以确保 kl 不超出边界。例如,max(0, i-1) 将始终返回 i-1,除非 i-1 小于 0,此时返回 0

英文:
for i := 0; i &lt; row; i++ {
    for j := 0; j &lt; col; j++ {
        if ary[i][j] == -1 {
            for k := max(0, i - 1); k &lt;= min(row - 1, i + 1); k++ {
                for l := max(0, j - 1); l &lt;= min(col - 1, j + 1); l++ {
                    if ary[k][l] != -1 {
                        ary[k][l] ++
                    }
                }
            } 
        }
    }
}

The first two outer loops go through the array. If the observed number is -1 then it increases all surrounding cells unless there is a mine on that field. To avoid the checks if ary[k][l] is not outside of bounds the bounds are calculated before by using the trick with min / max to ensure that k and l are not outside of bounds. E.g. max(0, i-1) will always return i-1 unless i-1 is smaller than 0, then it returns 0.

huangapple
  • 本文由 发表于 2015年5月23日 06:25:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/30406728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定