问题
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
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. )
回答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 ...
input
char map[5][5]
sample..... .*... ...*. ..... *....
create a counter
int cnt[5][5]
It should have the same size as input
map
. Init it with0
map cnt ..... 00000 .*... 00000 ...*. 00000 ..... 00000 *.... 00000
loop through whole
map
and if
map[i][j]=='*'
then simply increment all the adjacent counters incnt
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:
- Mine sweep in Turbo cpp
回答2:
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.
来源:https://stackoverflow.com/questions/40045480/understanding-the-minesweeper-programming-p-roblem