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;
}
相关推荐
三体世界27 分钟前
Mysql基本使用语句(一)
linux·开发语言·数据库·c++·sql·mysql·主键
etcix35 分钟前
wrap cpp variant as dll for c to use
java·c语言·开发语言
月殇_木言2 小时前
算法基础 第3章 数据结构
数据结构·算法
John_ToDebug2 小时前
JS 与 C++ 双向通信实战:基于 WebHostViewListener 的消息处理机制
前端·c++·chrome
亮亮爱刷题2 小时前
算法提升之树上问题-(LCA)
数据结构·算法·leetcode·深度优先
papership2 小时前
【入门级-C++程序设计:11、指针与引用-引 用】
c语言·开发语言·c++·青少年编程
火车叨位去19493 小时前
力扣top100(day03-01)--二叉树 03
算法·leetcode·职场和发展
岁忧3 小时前
(LeetCode 每日一题) 1780. 判断一个数字是否可以表示成三的幂的和 (数学、三进制数)
java·c++·算法·leetcode·职场和发展·go
浩少7023 小时前
LeetCode-16day:栈
java·数据结构·算法
rhythmcc5 小时前
【visual studio】visual studio配置环境opencv和onnxruntime
c++·人工智能·opencv