Mine Sweeping Game

来源:互联网 发布:淘宝贷款在哪里 编辑:程序博客网 时间:2024/06/06 07:01

Description

    As everyone knows, Mine Sweeping is a very famous computer game. When Diao Fei was young, he also liked to play this game very much. A sample of situation when win the game is like this below:

In a situation of winning this game, there are three kinds of grid:

·number grid:the grid has a number in it.Like 

·blank grid:the grid has nothing in it. Like 

·mine grid:the grid has a mine in it. Like 

    The game’s rule is that, if a grid is a number grid and it has an number  x in it, the eight grids (up, down, left, right, up-left, up-right, down-left, down-right, if exists) which around it should have excatly x mine grids in total. If a grid is a blank grid, then any grid around it should not be a mine grid.

    Now the problem is, in a situation of winning this game, giving all of the mine grids, your task is to determine what kind the remain grids are.

Input

    The first line contains an integer T, indicating the total number of test cases.

    In each test case, the first line is three integers NMK(   ) indicating the game size is N rows (numbered from 1 to N) and M columns (numbered from 1 to M), and there are K mines in total. ThenK lines follow, each line contains two integers xy() indicating the grid in xth row andyth column is a mine grid. You can assume that there are not any two mine grids in the same position.

Output

For each test case, output N lines, each line contain M characters, the jth character in the ith line indicates the grid in the ith row and the jth column. If the grid is a number grid, output the number in the grid. If the grid is a blank grid, output the character ‘.’ (without quotes). If the grid is a mine grid, output the character ‘M’ (without quotes). Notice that after each test case, you should output a blank line at the end.

Sample Input

29 9 101 12 12 63 35 96 36 86 97 68 25 5 13 3

Sample Output

M2..111..M3111M1..12M1111...111...11.111..13M.1M1112MM12211M2221M1.111..111............111..1M1..111......
方法一:
此方法比较简便。map[][]这个函数当时做题时,
#include<stdio.h>
#include<string.h>
#define BANNER 10
int main()
{
    int T;
    int N,M,K;
    int map[11][11];
    int key[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
    scanf("%d",&T); 
    while(T--)
    {
        int x,y;
        memset(map,0,sizeof(map));
        scanf("%d%d%d",&N,&M,&K);
        for(int i=1;i<=K;i++)
        {
            scanf("%d%d",&x,&y);
            map[x][y]=BANNER; 
            for (int j=0;j<8;j++)
            {
                map[x+key[j][0]][y+key[j][1]]++;//此处比较重要,当时没想出来。
            }
        }       
        for (int i=1;i<=N;i++)
        {
            for (int j=1;j<=M;j++)
            {
                if (map[i][j]>=BANNER)
                {
                    printf("M");
                }
                else if(map[i][j]==0)
                {
                    printf(".");
                }
                else
                {
                    printf("%d",map[i][j]);
                }
            }
            printf("\n");
        }        
    }
    return 0;
}

方法二:此方法简单易懂,不过就是代码比较麻烦
#include<stdio.h>
#include<string.h>
int main()
{
  int t,n,m,k,i,j,x[105],y[105];
  int map[15][15];
  scanf("%d",&t);
  while(t--)
  {
  scanf("%d%d%d",&n,&m,&k);
  memset(map,0,sizeof(map));
  for(i=0;i<k;i++)
  {
  scanf("%d%d",&x[i],&y[i]);
  map[x[i]][y[i]]=-1;
}
for(i=0;i<k;i++)
{
if(map[x[i]-1][y[i]-1]!=-1)
  map[x[i]-1][y[i]-1]+=1;
  if(map[x[i]-1][y[i]]!=-1)
  map[x[i]-1][y[i]]+=1;
  if(map[x[i]-1][y[i]+1]!=-1)
  map[x[i]-1][y[i]+1]+=1;
  if(map[x[i]][y[i]-1]!=-1)
  map[x[i]][y[i]-1]+=1;
  if(map[x[i]][y[i]+1]!=-1)
  map[x[i]][y[i]+1]+=1;
  if(map[x[i]+1][y[i]-1]!=-1)
  map[x[i]+1][y[i]-1]+=1;
  if(map[x[i]+1][y[i]]!=-1)
  map[x[i]+1][y[i]]+=1;
  if(map[x[i]+1][y[i]+1]!=-1)
  map[x[i]+1][y[i]+1]+=1;
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(map[i][j]==-1)
printf("M");
else if(map[i][j]==0)
{
printf(".");
}
else
{
printf("%d",map[i][j]);
}
}
printf("\n");
}
printf("\n");
}
return 0;
}
 
0 0
原创粉丝点击