oj 1.8编程基础之多维数组 14:扫雷游戏地雷数计算

描述

扫雷游戏是一款十分经典的单机小游戏。它的精髓在于,通过已翻开格子所提示的周围格地雷数,来判断未翻开格子里是否是地雷。

现在给出n行m列的雷区中的地雷分布,要求计算出每个非地雷格的周围格地雷数。

注:每个格子周围格有八个:上、下、左、右、左上、右上、左下、右下。

输入

第一行包含两个整数n和m,分别表示雷区的行数和列数。1 <= n <= 100, 1 <= m <= 100。

接下来n行,每行m个字符,'*'表示相应格子中是地雷,'?'表示相应格子中无地雷。字符之间无任何分隔符。

输出

n行,每行m个字符,描述整个雷区。若相应格中是地雷,则用'*'表示,否则用相应的周围格地雷数表示。字符之间无任何分隔符。

样例输入

复制代码
3 3
*??
???
?*?

样例输出

复制代码
*10
221
1*1
cpp 复制代码
#include<stdio.h>


int main()
{
    int n,m;
    scanf("%d %d",&n,&m);

    char arr[100][100];
    char brr[100][100];
    int i,j;
    for(i=1;i<=n;i++)
    {   getchar();
        for(j=1;j<=m;j++)
        {
            scanf("%c",&arr[i][j]);
        }

    }

    printf(" the original one is :\n");


    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
           printf("%c",arr[i][j]);
        }
        printf("\n");

    }

      int ii,jj,count;
      printf("the after one is \n");

     for(j=0;j<=m+1;j++)
     {
         arr[0][j]='?';
     }
     for(j=0;j<=m+1;j++)
     {
         arr[n+1][j]='?';
     }
     for(i=1;i<=n;i++)
     {
         arr[i][0]='?';
     }
     for(i=1;i<=n;i++)
     {
         arr[i][n+1]='?';
     }

     for(i=0;i<=n+1;i++)
    {
        for(j=0;j<=m+1;j++)
        {
            printf("%c",arr[i][j]);
        }
        printf("\n");

    }


     for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
           if(arr[i][j]=='*')
           {
               brr[i][j]='*';
           }
           else
           {
                count=0;
                printf("this is the mine around %d lint %d colum\n",i,j);
                for(ii=-1;ii<=1;ii++)
                 {
                   for(jj=-1;jj<=1;jj++)
                   {
                       //cao,为什么为啥

                        printf("%c",arr[i+ii][j+jj]);
                       if(!(ii==0&&jj==0))
                       {
                           if(arr[i+ii][j+jj]=='*')
                            count++;
                       }
                    }
                    printf("\n");

                }
                printf("the final count is %d \n",count);

                brr[i][j]='0'+count;
            }
        }


    }


   for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            printf("%c",brr[i][j]);
        }
        printf("\n");

    }
    return 0;
}

运行结果

cpp 复制代码
3 3
*??
???
?*?
 the original one is :
*??
???
?*?
the after one is
?????
?*???
?????
??*??
?????
this is the mine around 1 lint 2 colum
???
*??
???
the final count is 1
this is the mine around 1 lint 3 colum
???
???
???
the final count is 0
this is the mine around 2 lint 1 colum
?*?
???
??*
the final count is 2
this is the mine around 2 lint 2 colum
*??
???
?*?
the final count is 2
this is the mine around 2 lint 3 colum
???
???
*??
the final count is 1
this is the mine around 3 lint 1 colum
???
??*
???
the final count is 1
this is the mine around 3 lint 3 colum
???
*??
???
the final count is 1
*10
221
1*1

Process returned 0 (0x0)   execution time : 13.686 s
Press any key to continue.

最终的代码

cpp 复制代码
#include<stdio.h>


int main()
{
    int n,m;
    scanf("%d %d",&n,&m);

    char arr[110][110];
    char brr[110][110];//因为扩展了数组,所以100就会出错
    int i,j;
    for(i=1;i<=n;i++)
    {   getchar();
        for(j=1;j<=m;j++)
        {
            scanf("%c",&arr[i][j]);
        }

    }//注意输入的形式





      int ii,jj,count;


     for(j=0;j<=m+1;j++)
     {
         arr[0][j]='?';
     }
     for(j=0;j<=m+1;j++)
     {
         arr[n+1][j]='?';
     }
     for(i=1;i<=n;i++)
     {
         arr[i][0]='?';
     }
     for(i=1;i<=n;i++)
     {
         arr[i][n+1]='?';
     }
   //扩展数组,不扩展的话,那么在下面遍历的时候可能会有一些奇怪的结果


     for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
           if(arr[i][j]=='*')
           {
               brr[i][j]='*';
           }
           else
           {
                count=0;

                for(ii=-1;ii<=1;ii++)
                 {
                   for(jj=-1;jj<=1;jj++)
                   {
                       //cao,为什么为啥


                       if(!(ii==0&&jj==0)) //这里我是排除了中间那种可能,实际上不写也没有关系
                        //但是有一点很奇怪,我写ii!=0&&jj!=0就会出错,如果有人知道的话请告诉我一下,球球了
                       {
                           if(arr[i+ii][j+jj]=='*')
                            count++;
                       }
                    }


                }

                brr[i][j]='0'+count;
            }
        }


    }


   for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)//注意范围
        {
            printf("%c",brr[i][j]);
        }
        printf("\n");

    }
    return 0;
}
相关推荐
挥剑决浮云 -3 分钟前
Linux 之 安装软件、GCC编译器、Linux 操作系统基础
linux·服务器·c语言·c++·经验分享·笔记
Mephisto.java17 分钟前
【力扣 | SQL题 | 每日四题】力扣2082, 2084, 2072, 2112, 180
sql·算法·leetcode
robin_suli18 分钟前
滑动窗口->dd爱框框
算法
丶Darling.20 分钟前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo52030 分钟前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
Indigo_code41 分钟前
【数据结构】【链表代码】合并有序链表
数据结构·windows·链表
jiyisuifeng199141 分钟前
代码随想录训练营第54天|单调栈+双指针
数据结构·算法
小柯J桑_44 分钟前
C++:STL简介
c++·stl
一颗星星辰1 小时前
C语言 | 第十章 | 函数 作用域
c语言·开发语言