Reputation: 3
say I have a 2D array, size 5X5. for each element, I want to check the neighboring elements using a specific function I made to check "mines". how can I check the neighboring elements only once (meaning not to check them again when I move to the next element in which some of the "last neighbors" are still the neighbors of the new element) (as a background I will also provide "isValid function and isMine).(of course, this is only part of the code I am aware of all the initiation etc.. process)
// 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)
{
Upvotes: 0
Views: 61
Reputation: 180058
how can I check the neighboring elements only once (meaning not to check them again when I move to the next element in which some of the "last neighbors" are still the neighbors of the new element)
To do literally what you describe, you would need some kind of data structure in which to record the minedness of each cell in the board. But you already have that in the form of the board itself. Adding another data structure would just mean that you check that data structure instead of checking the board, plus you would need to implement logic to choose which data source to test. If it were expensive to test the minedness of your cells then it might make sense to make such efforts, but that's not your case.
If, say, you're doing something like computing how many mines neighbor each cell, then you could consider turning the problem inside out: start out by setting the mine-neighbor count of each cell to zero, then scan the board for mines, and each time you find one, increment the count of each of its neighbors. Where two or more mines have a common neighbor you will still touch that neighbor multiple times, but if the number of mines is less than the number of unmined cells then you might see some improvement.
Upvotes: 1