ZOJ-1862 Mine Sweeper

Mine Sweeper


Time Limit: 2 Seconds      MemoryLimit: 65536 KB


The gameMinesweeper is played on an n by n grid. In this grid are hidden m mines, eachat a distinct grid location. The player repeatedly touches grid positions. If aposition with a mine is touched, the mine explodes and the player loses. If apositon not containing a mine is touched, an integer between 0 and 8 appearsdenoting the number of adjacent or diagonally adjacent grid positions thatcontain a mine. A sequence of moves in a partially played game is illustratedbelow.

  

Here, n is 8, m is10, blank squares represent the integer 0, raised squares represent unplayedpositions, and the figures resembling asterisks represent mines. The leftmostimage represents the partially played game. From the first image to the second,the player has played two moves, each time choosing a safe grid position. Fromthe second image to the third, the player is not so lucky; he chooses aposition with a mine and therefore loses. The player wins if he continues tomake safe moves until only m unplayed positions remain; these must necessarilycontain the mines.

Your job is toread the information for a partially played game and to print the correspondingboard.


Input

The first line of input contains a single postitive integer n <= 10. Thenext n lines represent the positions of the mines. Each line represents thecontents of a row using n characters: a period indicates an unmined positonwhile an asterisk indicates a mined position. The next n lines are each ncharacters long: touched positions are denoted by an x, and untouched positionsby a period. The sample input corresponds to the middle figure above.

Input containsmultiple test cases. Process to the end of file.


Output

Your output should represent the board, with each position filled inappropriately. Positions that have been touched and do not contain a mineshould contain an integer between 0 and 8. If a mine has been touched, allpositions with a mine should contain an asterisk. All other positions shouldcontain a period.

Separate outputfor subsequent test cases with a single blank line.


Sample Input

8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxx.....
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.....
xxxxx...


Sample Output

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


————————————————————————————————————————————————————————————————

如果點到了地雷,需要在最後的結果中將是地雷的點都表示出來。


————————————————————————————————————————————————————————————————

#include <stdio.h>
#include <string.h>
char calculate(int r, int l);
char map[10][10];
int n;
int main()
{
      int i, j, m, count;
      char touch[10][10];
      char str[11];
      char res[10][10];
      count = 0;
      while(scanf("%d", &n) != EOF) {
	    if(count) {
		  printf("\n");
	    } else {
		  count = 1;
	    }
	    for(i = 0; i < n; i++) {
		  scanf("%s", str);
		  for(j = 0; j < n; j++) {
			map[i][j] = str[j];
		  }
	    }
	    for(i = 0; i < n; i++) {
		  scanf("%s", str);
		  for(j = 0; j < n; j++) {
			touch[i][j] = str[j];
			res[i][j] = '.';
		  }
	    }
	    m = 0;
	    for(i = 0; i < n; i++) {
		  for(j = 0; j < n; j++) {
			if(touch[i][j] == 'x') {
			      if(map[i][j] == '*') {
				    m = 1;
			      } else {
				    res[i][j] = calculate(i, j);
			      }
			}
		  }
	    }
	    if(m) {
		  for(i = 0; i < n; i++) {
			for(j = 0; j < n; j++) {
			      if(map[i][j] == '*') {
				    res[i][j] = '*';
			      }
			}
		  }
	    }
	    for(i = 0; i < n; i++) {
		  for(j = 0; j < n; j++) {
			printf("%c", res[i][j]);
		  }
		  printf("\n");
	    }
      }
      return 0;
}
char calculate(int r, int l)
{
      char count = '0';
      if(r-1 >= 0 && map[r-1][l] == '*') {
	    count++;
      }
      if(r-1 >= 0 && l-1 >= 0 && map[r-1][l-1] == '*') {
	    count++;
      }
      if(l-1 >= 0 && map[r][l-1] == '*') {
	    count++;
      }
      if(r+1 < n && l-1 >= 0 && map[r+1][l-1] == '*') {
	    count++;
      }
      if(r+1 < n && map[r+1][l] == '*') {
	    count++;
      }
      if(r+1 < n && l+1 < n && map[r+1][l+1] == '*') {
	    count++;
      }
      if(l+1 < n && map[r][l+1] == '*') {
	    count++;
      }
      if(r-1 >= 0 && l+1 < n && map[r-1][l+1] == '*') {
	    count++;
      }
      return count;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章