UVa10189 Minesweeper

题目大意

输入行数与列数,紧接着输入一个字符矩阵,矩阵中’.’代表没有雷,‘*‘代表雷,进行的处理是将所有的.替换为周围的雷的数目,模拟了扫雷的过程
英文原文:
Have you ever played Minesweeper? It’s a cute little game which comes within a certain Operating System which name we can’t really remember. Well, the goal of the game is to nd where are all the mines within a M N eld. To help you, the game shows a number in a square which tells you how many mines there are adjacent to that square. For instance, supose the ollowing 4 4 eld with 2 mines (which are represented by an `*’ character):

*...
....
.*..
....

If we would represent the same eld placing the hint numbers described above, we would end up
with:
*100
2210
1*10
1110
As you may have already noticed, each square may have at most 8 adjacent squares.
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 stands for the number of lines and columns of the eld respectively. The next n lines contains exactly m characters and represent the eld. Each safe square is represented by an ‘.’ character (without the quotes) and each mine square is represented by an ‘*’ character (also 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, you must print the following message in a line alone:
Field #x:
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 adjacent mines 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

主要方法

对‘.’周围格子进行遍历即可,其中还有些小技巧和要注意的地方:

技巧:

  1. 行数与列数的范围是1~100,与遍历时边缘的格子情况特殊这一问题相结合,在设char数组时,设置了102*102的矩阵,这样可以将整个输入的矩阵整个包围起
    来,然后将输入矩阵外的元素全部设为‘.’,这样在遍历边缘格子的时候,就不用进行特殊处理了,依然测试周围的8个即可
  2. 对每个格子遍历周围8个时,设两个变量k,l,分别取-1,0,1,然后i,j分别加k,l即可(也会遍历到a[i][j],但没关系,‘.’不会增加雷数)

注意点:

输出格式一定要注意,要求输出的每个矩阵之间要空一行,但是注意,最后一个矩阵输出后不能再输出空行了,不然会报格式错误,处理方法是把输出空行移到下一个矩阵输出之前。

代码呈上

#include
int main()
{
    char a[102][102];
    int n,m,i,j,cons,ans,k,l;
    ans=0;
    while(1){
        for(i=0;i<=101;i++)
        {
            for(j=i;j<=101;j++)
            {
                a[i][j]=a[j][i]='.';
            }
        }
        scanf("%d%d",&n,&m);
        getchar();
        ans++;
        if(n==0&&m==0)
        break;
        if(ans!=1) printf("\n");
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf("%c",&a[i][j]);
            }
            getchar();
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(a[i][j]!='*'){
                    cons=0;
                    for(k=-1;k<=1;k++)
                    {
                        for(l=-1;l<=1;l++)
                        {
                            if(a[i+k][j+l]=='*')
                            cons++;
                        }
                    }
                    a[i][j]='0'+cons;
                }   
            }
        }
        printf("Field #%d:\n",ans);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                 printf("%c",a[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

也常会做些ACM水题,虽然简单,也是自己的成果。

你可能感兴趣的:(ACM)