检查索引是否超出范围以满足条件 [英] check if index is out of bounds to satisfy if conditions
问题描述
你好,我正在练习在c ++中使用2d数组,例如,我的问题是是否要检查4是否以0或11作为邻居的北,东,南,西,它应该返回false.这是我的如果
Hello I am practicing working with 2d arrays in c++ and my question is for example if I want to check if 4 has either 0 or 11 to the north , east, south, west as neighbour it should return false. this is my if
if((grid[0-1][0] == 0 || grid[0-1][0] == 11 ) &&
(grid[0+1][0] == 0 || grid[0+1][0] == 11 ) &&
(grid[0][0+1] == 0 || grid[0][0+1] ==11) &&
(grid[0][0-1] == 0 || grid[0][0-1] ==11 ))
{
return false;
}
现在我的问题是,由于4的西边和4的北边超出范围,它将永远不会返回false.如何优化if条件以使其返回false?这是我的二维数组
Now my problem is since west of 4 and north of four is out of bounds it will never return false. How could I optimise my if condition to make it return false? This is my 2d array
int grid[ROW][COL] = {{ 4, 11, 1, 1 },
{ 0, 0, 1, 0 },
{ 0, 1, 5, 0},
{ 0, 5, 0,0 } };
推荐答案
TL; DR您缺少边界条件
TL;DR you are missing boundary conditions
// Boundary Conditions
if( i == ROW || j == COL || i < 0 || j < 0 )
return false;
根据问题将矩阵定义为
Based on the question the matrix is defined as
#define ROW 4
#define COL 4
int grid[ROW][COL] = {{ 4, 11, 1, 1 },
{ 0, 0, 1, 0 },
{ 0, 1, 5, 0},
{ 0, 5, 0,0 } };
在 row i
和 col j
处指定了一个单元格,该单元格由协调的 i,j
表示为二维数组中的可视化对象看起来像这样
given a cell at row i
and col j
denoted by cordinated i, j
the visualization in a 2-D array for that will look like this
i-1, j-1 i-1, j i-1,j+1
i, j-1 i, j i,j+1
i+1, j-1 i+1, j i+1,j+1
从上面我们现在可以推断出给定 i,j
from above we can now deduce the corresponding cordinates/points reference to given i,j
i,j ---> North( i-1, j )
i,j ---> South( i+1, j )
i,j ---> East( i , j+1)
i,j ---> West( i , j-1)
现在我们可以编写一个小函数来检查由i和j表示的任何单元格上的给定值是否为真,该下面的函数执行类似的操作..检查提供的坐标是否在边界内以及 grid [j] j]
匹配我们需要匹配的内容
now we can write a small function to check if a given value at any cell denoted by i and j is true or not, this below function does similar .. checks if supplied coordinates are within boundary and if the value at grid[j]j]
matches what we need to match
bool Check( int grid[ROW][COL], int expected, int i, int j )
{
// Boundary Conditions
if( i == ROW || j == COL || i < 0 || j < 0 )
return false;
return ( grid[i][j] == expected );
}
现在是时候将北,南,西,东计算结果进行编码并将其公开为漂亮的函数了,
Now time to put the North, South, West, East calculation to code and expose them as nice functions,
bool northHas( int grid[ROW][COL], int expected, int i, int j )
{
return check(grid, expected, i-1, j );
}
bool southHas( int grid[ROW][COL], int expected, int i, int j )
{
return check(grid, expected, i+1, j );
}
bool eastHas( int grid[ROW][COL], int expected, int i, int j )
{
return check(grid, expected, i, j+1 );
}
bool westHas( int grid[ROW][COL], int expected, int i, int j )
{
return check(grid, expected, i, j-1 );
}
上面的每个函数都公开了一个更好的接口来处理逻辑程序想要做的事情
each of these functions above exposes a nicer interface to deal with what logic program wants to do
if( (northHas( grid, 0, i, j ) || northHas( grid, 11, i, j)) &&
( eastHas( grid, 0, i, j ) || eastHas( grid, 11, i, j)) &&
(southHas( grid, 0, i, j ) || southHas( grid, 11, i, j)) &&
( westHas( grid, 0, i, j ) || westHas( grid, 11, i, j)) )
{
return false
}
这篇关于检查索引是否超出范围以满足条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!