NZAU-C-Mine Sweeping Game

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 N, M, K( , ) 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. Then K lines follow, each line contains two integers x, y(, ) indicating the grid in xth row and yth 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
2
9 9 10
1 1
2 1
2 6
3 3
5 9
6 3
6 8
6 9
7 6
8 2
5 5 1
3 3

Sample Output
M2..111..
M3111M1..
12M1111..
.111…11
.111..13M
.1M1112MM
12211M222
1M1.111..
111……

…..
.111.
.1M1.
.111.
…..

一道水题,然而,一开始我卡住了,怎么也过不去,明明代码方法是对的,然后就抱着试一试的心态,试各种可能出现的问题,终于在纠结了半个小时后,发现,竟然是在输出完一组数据后,要打一个空行,因为我是过一组输一组,所以没有想到测试数据给的那个表示的是中间要打一个空格,这就尴尬了……怪自己没有看清楚中间有一个空格,哎,以后一定要多注意了……

代码如下(C):

#include <stdio.h>
#include <string.h>
#define BANNER 10

int main(int argc, const char * argv[])
{
    int T;
    int N, M, K;
    int map[12][12];
    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");
        }
        printf("\n");

    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值