Minesweeper

来源:互联网 发布:六年级新教材辅导软件 编辑:程序博客网 时间:2024/04/25 08:12

题意:

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 nd where all the mines are located within
a M  N eld.
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 eld on the left contains two mines,
each represented by a \*" character. If we represent the same eld by the hint numbers described
above, we end up with the eld on the right:
*...
....
.*..
....
*100
2210
1*10
1110
Input
The input will consist of an arbitrary number of elds. The rst line of each eld contains two
integers n and m (0 < n; m  100) which stand for the number of lines and columns of the eld,
respectively. Each of the next n lines contains exactly m characters, representing the eld.
Safe squares are denoted by \." and mine squares by \*," both without the quotes. The rst eld
line where n = m = 0 represents the end of input and should not be processed.
Output
For each eld, print the message Field #x: on a line alone, where x stands for the number of
the eld starting from 1. The next n lines should contain the eld with the \." characters replaced
by the number of mines adjacent to that square. There must be an empty line between eld outputs.
Sample Input
4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0
Sample Output
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100


解题思路:

建立一个数组,以某一个点作为远点,然后旁边八个点作为左边,形成一个【8】【2】的二维数组,然后依次找出每个点的值


代码:

#include <stdio.h> #define MAXSIZE 100  int  range_checking(int x, int y, int line, int row)  {      return ((0 <= x && x < line) && (0 <= y && y < row));  }      void display(char matrix[][MAXSIZE], int line, int row)  {          int i,j,k,b;               int bounds[8][2] =          { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0},          {1, 1} };        for ( i = 0; i < line; i++)      {          for ( j = 0; j < row; j++)          {              // 如果该位置为地雷则原样输出地雷的符号。              if (matrix[i][j] == '*')                  printf( "*");              else              {                  // 统计该点周围 8 点的地雷总数。                  int mines = 0;                  for ( k = 0; k < 8; k++)                  {                      int m = i + bounds[k][0];                      int n = j + bounds[k][1];                      b = range_checking(m, n, line, row);                    if (b && matrix[m][n] == '*')                          mines++;                  }                    printf("%d",mines);              }          }       printf("\n");      }  }         int main()  {      char matrix[MAXSIZE][MAXSIZE];      int line,i,j,row, field = 0;      scanf("%d %d",&line,&row);    while ( line>0&& line <= 100 && row > 0 && row <= 100)      {          //memset(matrix, 0, sizeof(matrix));            for ( i = 0; i < line; i++)              for ( j = 0; j < row; j++)                 //printf("%c", matrix[i][j]);                     scanf("%c", &matrix[i][j]);                 field++;            printf("Field #%d:\n",field);          display(matrix, line, row);                                 scanf("%d %d",&line,&row);      }        system("pause"); }    


但是出现了错误,至今没能找出。

原创粉丝点击