当前位置: 首页 > 工具软件 > Minesweeper > 使用案例 >

1096: Minesweeper扫雷

范翰海
2023-12-01


题目原型

题目描述


问题 1096: Minesweeper

时间限制: 1Sec 内存限制: 64MB 提交: 2393 解决: 981
题目描述
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

提示
无

代码

#include<iostream>
using namespace std;
int main()
{
    int row,column,first=0;
    while(cin>>row>>column){
            first++;

        if(row==0&&column==0)
            break;
        else{
            char aa[row+1][column+1];
            for(int i=0;i<row;i++){
                for(int j=0;j<column;j++){
                    cin>>aa[i][j];//cout<<111111<<endl;
                }
            }
            cout<<"Field #"<<first<<":"<<endl;

            char aaa[row][column];
                for(int i=0;i<row;i++){
                    for(int j=0;j<column;j++){
                            if(aa[i][j]=='*')
                            {
                                cout<<'*';continue;
                            }
                            int round=0;
                            if(i>0&&j>0&&i<row-1&&j<column-1){
                                    if(aa[i-1][j-1]=='*')round++;
                                    if(aa[i-1][j]=='*')round++;
                                    if(aa[i-1][j+1]=='*')round++;
                                    if(aa[i][j-1]=='*')round++;
                                    if(aa[i][j+1]=='*')round++;
                                    if(aa[i+1][j-1]=='*')round++;
                                    if(aa[i+1][j]=='*')round++;
                                    if(aa[i+1][j+1]=='*')round++;
                            }
                            else if(i==0&&j!=0&&j!=column-1){
                                   // cout<<123123<<endl;
                                     if(aa[i][j-1]=='*')round++;
                                     if(aa[i][j+1]=='*')round++;
                                     if(aa[i+1][j-1]=='*')round++;
                                     if(aa[i+1][j]=='*')round++;
                                     if(aa[i+1][j+1]=='*')round++;
                            }
                            else if(i!=0&&i!=row-1&&j==0){
                                    if(aa[i-1][j]=='*')round++;
                                    if(aa[i+1][j]=='*')round++;
                                    if(aa[i-1][j+1]=='*')round++;
                                    if(aa[i][j+1]=='*')round++;
                                    if(aa[i+1][j+1]=='*')round++;
                            }
                            else if(i==row-1&&j!=0&&j!=column-1){
                                     if(aa[i-1][j]=='*')round++;
                                     if(aa[i-1][j-1]=='*')round++;
                                     if(aa[i-1][j+1]=='*')round++;
                                     if(aa[i][j-1]=='*')round++;
                                     if(aa[i][j+1]=='*')round++;
                            }
                            else if(i!=0&&i!=row-1&&j==column-1){
                                    if(aa[i-1][j]=='*')round++;
                                    if(aa[i+1][j]=='*')round++;
                                    if(aa[i-1][j-1]=='*')round++;
                                    if(aa[i][j-1]=='*')round++;
                                    if(aa[i+1][j-1]=='*')round++;
                            }
                            else if(i==0&&j==0){
                                    if(aa[i][j+1]=='*')round++;
                                    if(aa[i+1][j]=='*')round++;
                                    if(aa[i+1][j+1]=='*')round++;
                            }
                            else if(i==0&&j==column-1){
                                    if(aa[i][j-1]=='*')round++;
                                    if(aa[i+1][j]=='*')round++;
                                    if(aa[i+1][j-1]=='*')round++;
                            }
                            else if(j==0&&i==row-1){
                                    if(aa[i-1][j]=='*')round++;
                                    if(aa[i-1][j+1]=='*')round++;
                                    if(aa[i][j+1]=='*')round++;
                            }
                            else if(j==column-1&&i==row-1){
                                    if(aa[i-1][j]=='*')round++;
                                    if(aa[i-1][j-1]=='*')round++;
                                    if(aa[i][j-1]=='*')round++;
                            }
                            cout<<round;
                        }
                        cout<<endl;
                    }
                    cout<<endl;
            }
    }
    return 0;
}

总结

1,只要思想不滑坡,办法总比困难多,
2,这题很简单,但是要分情况讨论,我的代码分了9总情况,可以根据题目中给的例子来分析这9种情况的代码应该怎么写
3,一定要注意细节,要不然真的卡的很难受eg,两层for循环中第一层为i,第二层为j,一定不要打乱
4,总结一个小tip,单个字符用int a,数字用aa[],来表示容易区分,最好能顾名思义,英语优先,汉语也比随便写强

 类似资料: