• Minesweeper


    你玩过扫雷吗?这个可爱的小游戏带有一个我们记不住名字的操作系统。游戏的目标是找到所有地雷在M x N场中的位置。游戏在一个正方形中显示一个数字,它告诉你在这个正方形附近有
    多少个地雷。每个方块最多有八个相邻方块。左侧的4x 4字段包含两个地雷,每个地雷由一个“`*”字符表示。如果我们用上面描述的提示数来表示同一个字段,
    我们最后得到右边的字段:*……*…*100 2210 1*10 1110
    输入将由任意数量的字段组成。每个字段的第一行包含两个整数n和m(0<n,m$le$100),分别代表字段的行数和列数。接下来的n行中的每一行正好包含m个字符,表示字段。安全方格
    用`.'表示,矿山方格用`.*表示,两者都没有引号。n=m=0的第一行字段表示输入结束,不应进行处理。
    For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next n lines
    should contain the field withthe ``.'' characters replaced by the number of mines adjacent to that square. There must be an empty line
    between field outputs. 对于每个字段,单独在一行上打印消息字段x,其中x代表从1开始的字段编号。接下来的n行应该包含带有“..”字符的字段,替换为该广场附近的地雷数量。字段输出之间必须有空行。
    //扫雷游戏
    #include<iostream>
    #include<cstring>
    using namespace std;
    int i,j;
    char g[100][100];
    int book[100][100]; 
    int gcd(int x,int y){
        int num=0;
        if(g[x][y]=='*') return -1;
    //    cout<<"hello"<<endl;
        for(int xx=x-1;xx<=x+1;xx++){
            for(int yy=y-1;yy<=y+1;yy++){
                
                if(xx==x&&yy==y) continue;
                if(g[xx][yy]=='*') num++;
            }
        }
    return num;
    }
    int main()
    {
        memset(book,0,sizeof(book));
        memset(g,0,sizeof(g));
        
        int m,n;
        int num=0;
        while(cin>>m>>n&&m&&n){
            num++;
            memset(book,0,sizeof(book));
            memset(g,0,sizeof(g));
            for(i=1;i<=m;i++){
                for(j=1;j<=n;j++)
                cin>>g[i][j];
            }
                for(i=1;i<=m;i++){
                for(j=1;j<=n;j++){
                    book[i][j]=gcd(i,j);
                }
    
            }
            printf("Field #%d:
    ",num);
                for(i=1;i<=m;i++){
                for(j=1;j<=n;j++){
                    if(book[i][j]!=-1) cout<<book[i][j];
                    else cout<<"*";
    //                cout<<book[i][j]<<" ";
                }
                cout<<endl;
            }
    cout<<endl;
        }
    } 
    View Code

    广度优先搜索就行

    主要问题是害怕看英文,多看看英语阅读

    题目描述
    Minesweeper Have you ever played Minesweeper? This cute little game comes with a certain
     operating system whose name we can't remember. The goal of the game is to find where all
     the mines are located within a M x N field. The game shows a number in a square which tells
     you how many mines there are adjacent to that square. Each square has at most eight 
    adjacent squares. The 4 x 4 field on the left contains two mines, each represented by a ``*''
     character. If we represent the same field by the hint numbers described above, we end up with
     the field on the right: *... .... .*.. .... *100 2210 1*10 1110
    输入
    The input will consist of an arbitrary number of fields. The first line of each field contains two
     integers n and m ( 0 < n, m$ le$100) which stand for the number of lines and columns of the
     field, respectively. Each of the next n lines contains exactly m characters, representing the field. Safe squares are denoted by ``.'' and mine squares by ``*,'' both without the quotes. The first field line where n = m = 0 represents the end of input and should not be processed.
    输出
    For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next n lines should contain the field with the ``.'' characters replaced by the number of mines adjacent to that square. There must be an empty line between field outputs.
    样例输入
    4 4
    *...
    ....
    .*..
    ....
    3 5
    **...
    .....
    .*...
    0 0
    样例输出
    Field #1:
    *100
    2210
    1*10
    1110
    
    Field #2:
    **100
    33200
    1*100
    

      

     

  • 相关阅读:
    蛙蛙请教:问几个面向对象设计的问题
    推荐一些flash和asp.net结合开发的文章
    写一个跟踪的类库
    蛙蛙推荐:有关随机数的一些讨论
    蛙蛙推荐:用ASP.NET WEB Services和Flash MX 2004打造MP3播放器
    HiveQL学习
    Linux rpm 命令参数使用详解[介绍和应用]
    java.util.concurrent.locks.Condition 例子程序探讨
    [转]sudoers设置
    配置linux电脑
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10526403.html
Copyright © 2020-2023  润新知