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

描述

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

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

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

输入

第一行包含两个整数n和m,分别表示雷区的行数和列数。1 <= n <= 100, 1 <= m <= 100。
接下来n行,每行m个字符,‘*’表示相应格子中是地雷,‘?’表示相应格子中无地雷。字符之间无任何分隔符。

输出

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

样例输入

3 3
*??
???
?*?

样例输出

*10
221
1*1
#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;
}


 

运行结果

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.

最终的代码

#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;
}

相关推荐

  1. oj 1.8编程基础 14:扫雷游戏地雷计算

    2023-12-28 17:10:03       33 阅读
  2. oj 1.8编程基础 16:矩阵剪刀石头布

    2023-12-28 17:10:03       35 阅读
  3. oj 1.8编程基础 12:变幻的矩阵

    2023-12-28 17:10:03       37 阅读
  4. oj 1.8编程基础 13:图像模糊处理

    2023-12-28 17:10:03       34 阅读
  5. oj 1.8编程基础 24:蛇形填充数组

    2023-12-28 17:10:03       36 阅读
  6. oj 1.8编程基础 07:矩阵归零消减序列和

    2023-12-28 17:10:03       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-28 17:10:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-28 17:10:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-28 17:10:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-28 17:10:03       18 阅读

热门阅读

  1. python的WebSocket编程详解,案例群聊系统实现

    2023-12-28 17:10:03       41 阅读
  2. docker——数据卷(volume)概念及使用案例

    2023-12-28 17:10:03       33 阅读
  3. oj 1.8编程基础之多维数组 07:矩阵归零消减序列和

    2023-12-28 17:10:03       34 阅读
  4. 模式识别与机器学习(十):梯度提升树

    2023-12-28 17:10:03       34 阅读
  5. 需求更改实现方式提升效率

    2023-12-28 17:10:03       39 阅读
  6. centos 扩充swap分区

    2023-12-28 17:10:03       37 阅读
  7. CentOS上安装MySQL 8.0的详细教程

    2023-12-28 17:10:03       34 阅读
  8. centos 安装 配置 zsh

    2023-12-28 17:10:03       46 阅读
  9. mysql-5.6.16的内存泄漏问题

    2023-12-28 17:10:03       43 阅读