[UVA10279]Mine Sweeper

Posted by John on 2016-02-18
Words 695 and Reading Time 4 Minutes
Viewed Times

The game Minesweeper is played on an n by n grid. In this grid are hidden m mines, each at a distinct grid location. The player repeatedly touches grid positions. If a position with a mine is touched, the mine explodes and the player loses.

If a position not containing a mine is touched, an integer between 0 and 8 appears denoting the number of adjacent or diagonally adjacent grid positions that contain a mine. A sequence of moves in a partially played game is illustrated below.

Here, n is 8, m is 10, blank squares represent the integer 0, raised squares represent unplayed positions, and the gures resembling asterisks represent mines. The leftmost image represents the partially played game. From the rst image to the second, the player has played two moves, each time choosing a safe grid position. From the second image to the third, the player is not so lucky; he chooses a position with a mine and therefore loses. The player wins if he continues to make safe moves until only m unplayed positions remain; these must necessarily contain the mines.

Your job is to read the information for a partially played game and to print the corresponding board.

Input

The rst line of input contains how many game you have to solved followed by blank line. The rst line of each description contains a single postitive integer n 10. The next n lines represent the positions of the mines. Each line represents the contents of a row using n characters: a period indicates an unmined position while an asterisk indicates a mined position. The next n lines are each n characters long: touched positions are denoted by an `x, and untouched positions by a period. The sample input corresponds to the middle gure above. There is a blank line between each consecutive game description.

Output

For each test case, your output should represent the board, with each position lled in appropriately. Positions that have been touched and do not contain a mine should contain an integer between 0 and 8. If a mine has been touched, all positions with a mine should contain an asterisk. All other positions should contain a period. Print a blank line between each consecutive 2 consecutive test cases.

Sample Input

1
8
…..*
……*.
….…
……..
……..
…....
….*.
…..*..
xxx…..
xxxx….
xxxx….
xxxxx…
xxxxx…
xxxxx…
xxx…..
xxxxx…

Sample Output

001…..
0013….
0001….
00011…
00001…
00123…
001…..
00123…

題意: 模擬踩地雷遊戲,第一張地圖給你有地雷的地方,要你輸出第二張地圖中x的位置是什麼東西。

想法: 基本上I/O做好就沒問題,把地雷四周都多+1即可,要注意在字元以及整數的I/O轉換間換行會被吃進去造成輸入錯誤

#include <stdio.h> 
#include <stdlib.h>
int main() {
int n = 0,m = 0;
while(scanf("%d",&m) == 1) {
bool f = true;
while(m--) {
if(!f) printf("\n");
f = false; scanf("%d",&n);
char map[10+1][10+1] = {0};
int ans[10+1][10+1] = {0};
for(int i = 1 ; i <= n ; ++i) {
while(getchar()!='\n'){}
for(int j = 1 ; j <= n ; ++j) {
scanf("%c",&map[i][j]);
if(map[i][j] == '*') {
ans[i-1][j]++;
ans[i-1][j-1]++;
ans[i-1][j+1]++;
ans[i+1][j]++;
ans[i+1][j+1]++;
ans[i+1][j-1]++;
ans[i][j-1]++;
ans[i][j+1]++;
ans[i][j] = -1;
}
}
}
bool fir = true;
for(int i = 1 ; i <= n ; ++i) {
if(!fir)printf("\n");
fir = false;
while(getchar()!='\n'){}
for(int j = 1 ; j <= n ; ++j) {
scanf("%c",&map[i][j]);
if(map[i][j] == 'x') {
printf("%d",ans[i][j]);
if(ans[i][j] == -1) printf("*");
} else {
printf("%c",map[i][j]);
}
}
}
}
}
return 0;
}

>