• BJTU1113扫雷问题


    1.首次使用cin,cout。使用cin时不能再继续直接在字符串中添加字符,需要用到insert(),而且只能添加字符串;

    2.判断边界的函数,解决了数组下标越界的问题;

    3.cin、cout总结“http://www.newsmth.net/pc/pccon.php?id=10002714&nid=359771

    4.自己写测试数据,用自己的测试数据调试程序。

    以下是源代码

    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    int n,i,j,k,l,m;//循环变量
    int num1[8]= {-1,-1,-1,0,0,1,1,1};//便于使用循环历遍周围的八个数
    int num2[8]= {-1,0,1,-1,1,-1,0,1};
    int count[10][10];//记录周围雷的个数
    char point[10][10];//记录雷和非雷的位置
    string a[22];//字符串数组
    
    bool check(int a, int b )//判断是不是在边界上
    {
        if(a>=1 && a<=n && b>=0 && b<n )
            return true;
        else return false;
    }
    
    int main()
    {
        memset(count,0,sizeof(count));
        memset(point,'.',sizeof(point));
    
        scanf("%d",&n);
        for( i = 1 ; i <= 2*n ; i++)
        {
            cin >> a[i] ;
        }
    
        for( i = n+1 ; i < 2*n+1 ; i++ )
        {
            for( j = 0 ; j < n ; j++ )
            {
                if( a[i][j] == 'x' && a[i-n][j] == '.')
                {
                    for( k = 0 ; k < 8 ; k++ )
                    {
                        if( check(i - n + num1[k],j + num2[k]))//判断是不是在边界上
                        {
                            if( a[i - n + num1[k]] [j + num2[k]] == '*')
                            {
                                count[i-n-1][j] += 1;
                            }
                        }
    
                    }
                }
                else if( a[i][j] == 'x' && a[i-n][j] == '*')//扫中雷
                {
                    for(l=1;l<=n;l++)
                        for(m=0;m<=n;m++)
                            if(a[l][m]=='*')
                                point[l-1][m]='*';
                }
            }
        }
    
        for( i=0 ; i < n ; i++ )
        {
            for( j=0 ; j < n ; j++ )
            {
                if( a[n+1+i][j] == 'x' && a[1+i][j] != '*')//扫中雷
                    printf( "%d" , count[i][j] );
                else
                    printf( "%c" , point[i][j]);
            }
            printf("\n");
        }
    
    
        return 0;
    }
    

    按照题目的要求 测试数据有以下两种:

    第一种:扫中雷,所有有雷的位置出现*

    9 
    ......... 
    **.*..... 
    ....*.... 
    ....*.... 
    .....*... 
    ......... 
    ......... 
    .*....... 
    .**.*.... 
    ....xxxxx 
    ...xxxxxx 
    xxxx.xxxx 
    xxxx.xxxx 
    xxxxx.xxx 
    xxxxxxxxx 
    xxxxxxxxx 
    x.xxxxxxx 
    .....xxxx
    第二种:题目中给的输入输出

    9 
    ......... 
    **.*..... 
    ....*.... 
    ....*.... 
    .....*... 
    ......... 
    ......... 
    .*....... 
    .**.*.... 
    ....xxxxx 
    ....xxxxx 
    xxxx.xxxx 
    xxxx.xxxx 
    xxxxx.xxx 
    xxxxxxxxx 
    xxxxxxxxx 
    x.xxxxxxx 
    .....xxxx



  • 相关阅读:
    前端错误知识提示积累
    插件介绍之一:常用插件
    css小技巧积累
    设置网页地址栏小图标
    SEO优化篇——meta用法
    获取客户端的cookie
    come on,make a date progress bar together!
    教教你不用table制作出表格
    js实现快捷键绑定按钮点击事件
    Sublime Text3常用快捷键
  • 原文地址:https://www.cnblogs.com/dollarzhaole/p/3188946.html
Copyright © 2020-2023  润新知