1

I am trying to understand the minesweeper problem:
Problem statement:
Have you ever played Minesweeper? This cute little game comes with a certain operating system whose name we can’t remember. The goal of the game is to find where all the mines are located within a M ×N field. The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4×4 field on the left contains two mines, each represented by a “ * ” character. If we represent the same field by the hint numbers described above, we end up with the field on the right:
The test case
here is the test case

I am not understanding the problem. just explain me the problem so that I could solve it on my own. PLEASE DO NO EXPLAIN THE SOLUTION.

( I have already seen this problem and many others like this but they are not talking about the core programming problems , they are game projects. )

Community
  • 1
  • 1
log0
  • 2,206
  • 2
  • 14
  • 24

2 Answers2

1

In theory minesweeper can be made as grid of objects. When player use any object then (in classical minesweeper) surrounding objects checks THEIR surrounding objects and count how many are marked as mine.

Universus
  • 386
  • 1
  • 13
1

The problem is just to count the number of adjacent bombs per each map cell. The game shows only the already dig up cells of coarse.

You simply count the number of mines nearby ...

  1. input char map[5][5] sample

    .....
    .*...
    ...*.
    .....
    *....
    
  2. create a counter int cnt[5][5]

    It should have the same size as input map. Init it with 0

     map   cnt
    ..... 00000
    .*... 00000
    ...*. 00000
    ..... 00000
    *.... 00000
    
  3. loop through whole map

    and if map[i][j]=='*' then simply increment all the adjacent counters in cnt something like:

    for (i=0;i<5;i++)
     for (j=0;j<5;j++)
      if (map[i][j]=='*')
        {
        cnt[i-1][j-1]++;
        cnt[i-1][j  ]++;
        cnt[i-1][j+1]++;
        cnt[i  ][j-1]++;
        cnt[i  ][j+1]++;
        cnt[i+1][j-1]++;
        cnt[i+1][j  ]++;
        cnt[i+1][j+1]++;
        }
    

    To avoid access violations You should add range checks or separate inner and border parts. You can also use array size enlarged by 1 cell from each side and use just internal part.

    Result iterations on each success of the condition:

     map   cnt
    ..... 11100
    .*... 10100
    ...*. 11100
    ..... 00000
    *.... 00000
    
    ..... 11100
    .*... 10211
    ...*. 11201
    ..... 00111
    *.... 00000
    
    ..... 11100
    .*... 10211
    ...*. 11201
    ..... 11111
    *.... 01000
    

For more info/ideas take a look at:

Spektre
  • 49,595
  • 11
  • 110
  • 380