cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2016 · EasyGrid neighbors

Minesweeper

Autumn Tournament (2016)

Submission name: minesweeper.cpp

Problem summary

A 3 by 3 board contains 9 for a bomb and 0 for an empty cell. Replace each empty cell by the number of bombs sharing a side or corner with it. Keep bomb cells equal to 9.

Input

Three rows of three integers, each 0 or 9.

Output

The resulting three rows, with spaces between entries.

Constraints

The board is exactly 3×3; every input entry is 0 or 9.

Worked example

Original statement sample.

Input

9 0 0
0 0 9
0 0 0

Output

9 2 1
1 2 9
0 1 1

Solution analysis

  1. Keep the original board unchanged so all bomb checks refer to the input. Process each cell separately. A bomb cell is printed directly as 9.
  2. For an empty cell, examine row and column offsets −1,0,1. Skip the offset (0,0), and ignore positions outside the board. Count the remaining positions whose input value is 9.
  3. In the sample, the top-middle cell touches both bombs and becomes 2. The bottom-left cell touches neither and stays 0. Bomb positions keep their 9 values.

Why it works

The offset loops enumerate each of the eight potential neighboring cells once. The bounds check removes exactly those outside the board, and the bomb test counts exactly the remaining bombs. Thus every empty cell receives its required number, while bombs are preserved.

Complexity: O(1) time and O(1) space for the fixed 3×3 board.

Watch out. Corners have only three neighbors. Count diagonal bombs too, and never access outside the array.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
using namespace std;
int main(){
    int board[3][3];
    for(auto&row:board){
        for(int&cell:row){
            cin>>cell;
        }
    }
    for(int row=0;row<3;++row){
        for(int col=0;col<3;++col){
            int value=board[row][col];
            if(value!=9){
                value=0;
                for(int dr=-1;dr<=1;++dr){
                    for(int dc=-1;dc<=1;++dc){
                        if(dr==0&&dc==0){
                            continue;
                        }
                        int nr=row+dr;
                        int nc=col+dc;
                        // Only valid neighboring board cells can contain bombs.
                        if(nr>=0&&nr<3&&nc>=0&&nc<3&&board[nr][nc]==9){
                            ++value;
                        }
                    }
                }
            }
            if(col!=0){
                cout<<' ';
            }
            cout<<value;
        }
        cout<<'\n';
    }
}

Source: Arena Olimpiici ↗