检查2D数组元素的相邻元素,而无需多次检查元素

时间:2019-06-13 15:43:43

标签: c

说我有一个2D数组,大小为5X5。对于每个元素,我想使用为检查“地雷”而制作的特定功能来检查相邻元素。我如何只检查一次相邻元素(意味着当我移到下一个元素中某些“最后一个邻居”仍然是新元素的邻居时,不再检查它们)(作为背景,我还将提供“ isValid函数和isMine)。(当然,这只是我所知道的所有初始化等过程的一部分代码。

// A Utility Function to check whether given cell (row, col) 
// has a mine or not. 
bool isMine (int row, int col, char board[][MAXSIDE]) 
{ 
    if (board[row][col] == '*') 
        return (true); 
    else
        return (false); 
} 

// A Utility Function to check whether given cell (row, col) 
// is a valid cell or not 
bool isValid(int row, int col) 
{ 
    // Returns true if row number and column number 
    // is in range 
    return (row >= 0) && (row < SIDE) && 
           (col >= 0) && (col < SIDE); 
} 



 if (isValid (row-1, col) == true) 
            { 
                   if (isMine (row-1, col, realBoard) == false) 
                   playMinesweeperUtil(myBoard, realBoard, mines, row-1, col, movesLeft); 
            } 

            //----------- 2nd Neighbour (South) ------------ 

            // Only process this cell if this is a valid one 
            if (isValid (row+1, col) == true) 
            { 
                   if (isMine (row+1, col, realBoard) == false) 
                    playMinesweeperUtil(myBoard, realBoard, mines, row+1, col, movesLeft); 
            } 

            //----------- 3rd Neighbour (East) ------------ 

            // Only process this cell if this is a valid one 
            if (isValid (row, col+1) == true) 
            { 
                if (isMine (row, col+1, realBoard) == false) 
                    playMinesweeperUtil(myBoard, realBoard, mines, row, col+1, movesLeft); 
            } 

            //----------- 4th Neighbour (West) ------------ 

            // Only process this cell if this is a valid one 
            if (isValid (row, col-1) == true) 
            { 
                   if (isMine (row, col-1, realBoard) == false) 
                    playMinesweeperUtil(myBoard, realBoard, mines, row, col-1, movesLeft); 
            } 

            //----------- 5th Neighbour (North-East) ------------ 

            // Only process this cell if this is a valid one 
            if (isValid (row-1, col+1) == true) 
            { 
                if (isMine (row-1, col+1, realBoard) == false) 
                    playMinesweeperUtil(myBoard, realBoard, mines, row-1, col+1, movesLeft); 
            } 

             //----------- 6th Neighbour (North-West) ------------ 

            // Only process this cell if this is a valid one 
            if (isValid (row-1, col-1) == true) 
            { 
                 if (isMine (row-1, col-1, realBoard) == false) 
                    playMinesweeperUtil(myBoard, realBoard, mines, row-1, col-1, movesLeft); 
            } 

             //----------- 7th Neighbour (South-East) ------------ 

            // Only process this cell if this is a valid one 
            if (isValid (row+1, col+1) == true) 
            { 
                   if (isMine (row+1, col+1, realBoard) == false) 
                    playMinesweeperUtil(myBoard, realBoard, mines, row+1, col+1, movesLeft); 
            } 

            //----------- 8th Neighbour (South-West) ------------ 

            // Only process this cell if this is a valid one 
            if (isValid (row+1, col-1) == true) 
            {

1 个答案:

答案 0 :(得分:1)

  

如何仅检查一次相邻元素(意味着   当我移至下一个元素中的某些元素时,请再次检查它们   “最后一个邻居”仍然是新元素的邻居)

要按字面意思进行操作,您将需要某种数据结构来记录板中每个单元的状态。但是您已经以董事会本身的形式拥有了。添加另一个数据结构仅意味着您检查该数据结构而不是检查电路板,此外,您还需要实现逻辑以选择要测试的数据源。如果测试细胞的防雷系统很昂贵,那么做出这样的努力可能很有意义,但事实并非如此。

例如,如果您正在执行某种操作,例如计算与每个单元相邻的地雷数量,那么您可以考虑将问题彻底解决:首先将每个单元的地雷邻居计数设置为零,然后扫描电路板对于地雷,每当您发现一个地雷时,就增加每个地雷的数量。如果两个或多个地雷有一个共同的邻居,您仍然会多次触摸该邻居,但是如果地雷的数量少于未开采的单元的数量,那么您可能会看到一些改进。