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;
}
相关推荐
Cl_rown去掉l变成C3 分钟前
第J3-1周:DenseNet算法 实现乳腺癌识别
人工智能·pytorch·算法
努力学习的小廉3 分钟前
我爱学算法之—— 前缀和(中)
开发语言·redis·算法
保持学习ing5 分钟前
黑马Java面试笔记之 集合篇(算法复杂度+ArrayList+LinkedList)
java·笔记·算法·面试
呃m6 分钟前
加法c++
c++
LunaGeeking9 分钟前
三分算法与DeepSeek辅助证明是单峰函数
c语言·c++·算法·编程·信奥赛·ai辅助学习·三分
byte轻骑兵32 分钟前
【Bluedroid】蓝牙启动之 SMP_Init 源码解析
android·c++·smp·bluedroid
Darkwanderor1 小时前
数论——同余问题全家桶3 __int128和同余方程组
c++·算法·数论·中国剩余定理
Xyz_Overlord1 小时前
机器学习——聚类算法
算法·机器学习·聚类
dessler1 小时前
代理服务器-LVS的3种模式与调度算法
运维·服务器·网络·算法·nginx·tomcat·lvs
拼好饭和她皆失1 小时前
动态规划 熟悉30题 ---上
算法·动态规划