多维数组,我怎么能检查所有相邻瓷砖了在边界某些细胞? [英] Multi-dimensional array, how can I check all adjacent tiles that are IN bounds to certain cells?

查看:137
本文介绍了多维数组,我怎么能检查所有相邻瓷砖了在边界某些细胞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个12×12格,这网格内我创建'传染'的瓷砖,当一个受感染的瓷砖是由其他被感染的瓷砖包围,包围细胞变成患病瓦。我在想,如果有检查相邻的一个很好的方式在边界细胞的价值?

公共静态无效diseaseTiles(){
    INT I,J;
    对于(i = 0; I< 12;我++){
        为(J = 0; J< 12; J ++){
            如果(myGrid [I] [J] =='我'){
                INT左,右,上,下​​;
                如果(我== 0){
                    左= 1;
                }
                如果(我== 11){
                    右= 1;
                }
                如果(J == 0){
                    向上= 1;
                }
                如果(J == 11){
                    向下= 1;
                }
                //这是我已经得到了被困
                //我要使用上述廉政局的决定
                //是否在该方向上相邻的瓦片
                //应检查(他们网格的边界)
            }
        }
    }
}


解决方案

您可以检查单元格被采取短路评价的优势感染:

 布尔leftInfected =(I = 0!)及和放大器; myGrid [I-1] [J] =='我';
布尔rightInfected =&AMP(I = 11!);&安培; myGrid [I + 1] [J] =='我';
布尔topInfected =(J = 0!)及&放大器; myGrid [I] [J-1] =='我';
布尔bottomInfected =(J = 11!)及&放大器; myGrid [I] [J + 1] =='我';

在这之后,你可以检查所有四个被感染:

 如果(leftInfected&安培;&安培; rightInfected和放大器;&安培; topInfected和放大器;&安培; bottomInfected){
    myGrid [I] [J] ='D';
}

I've created a 12x12 grid, and within this grid I have created 'infected' tiles, when one infected tile is surrounded by other infected tiles, the surrounded cell becomes a diseased tile. I was wondering if there was a nice way of checking adjacent, in bounds cells for their value?

public static void diseaseTiles() {
    int i, j;
    for(i = 0; i < 12; i++) {
        for(j = 0; j < 12; j++) {
            if(myGrid[i][j] == 'I'){
                int left, right, up, down;
                if(i == 0) {
                    left = 1;
                }
                if(i == 11) {
                    right = 1;
                }
                if(j == 0) {
                    up = 1;
                }
                if(j == 11) {
                    down = 1;
                }
                //this is where I've gotten stuck
                //I was going to use the above int's to determine
                //whether or not the adjacent tile in that direction
                //should be checked (they're the border of the grid)
            }
        }
    }
}

解决方案

You can check if a cell is infected by taking advantage of short-circuited evaluation:

boolean leftInfected = (i!=0) && myGrid[i-1][j]=='I';
boolean rightInfected = (i!=11) && myGrid[i+1][j]=='I';
boolean topInfected = (j!=0) && myGrid[i][j-1]=='I';
boolean bottomInfected = (j!=11) && myGrid[i][j+1]=='I';

After that, you could check if all four are infected:

if (leftInfected && rightInfected && topInfected && bottomInfected) {
    myGrid[i][j] = 'D';
}

这篇关于多维数组,我怎么能检查所有相邻瓷砖了在边界某些细胞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆