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;
}
相关推荐
feiyangqingyun7 分钟前
Qt/C++开发监控GB28181系统/录像文件查询/录像回放/倍速播放/录像文件下载
c++·qt·gb28181·录像回放·录像文件下载
健康胡15 分钟前
仿射变换 与 透视变换
图像处理·人工智能·深度学习·opencv·算法·机器学习·计算机视觉
L_cl17 分钟前
【Python 算法零基础 2.模拟 ④ 基于矩阵】
python·算法·矩阵
2301_8076114921 分钟前
310. 最小高度树
c++·算法·leetcode·深度优先·回溯
@ chen33 分钟前
常见排序算法及其java实现
java·算法·排序算法
四谷夕雨1 小时前
C++八股——智能指针
c++
wgc2k1 小时前
Java游戏服务器开发流水账(5)Spring 在游戏开发中的使用简介
java·服务器·游戏
学习中的码虫2 小时前
数据结构中的高级排序算法
数据结构·算法·排序算法
passionSnail2 小时前
《用MATLAB玩转游戏开发》推箱子游戏的MATLAB趣味实现
开发语言·游戏·matlab
人类恶.2 小时前
C 语言学习笔记(6)
c语言·笔记·学习