Malones


私信TA

用户名:Malones

访问量:4269

签 名:

seize the day

等  级
排  名 1546
经  验 2793
参赛次数 0
文章发表 22
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:
难的是翻译,再次体现学好英语的重要。

大意就是跟window自带的扫雷一样,数字提醒你它的四周有多少雷,输出末尾要空行,提交时忘了空行错了两次...

参考代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
       int n, m, t = 1;
       cin >> n >> m;
       while ( n != 0 && m != 0){
               int i, j;
               char saolei[n][m];
               int jilu[n][m];
               for( i = 0 ; i < n; i++)   //初始化
                        for( j = 0; j < m; j++ )
                                jilu[i][j] = 0;
               for( i = 0 ; i < n; i++)
                        for( j = 0; j < m; j++ ){
                                cin >> saolei[i][j];
                                if(  saolei[i][j] == '*' ){   //发现地雷
                                        jilu[i][j] = -1;   //把地雷记录为-1,方便后续处理
                                        if( i > 0)
                                                if(jilu[i-1][j] != -1 )   //如果此处是地雷,则无需++,下同
                                                        jilu[i-1][j]++;
                                        if( i+1 < n)
                                                if( jilu[i+1][j] != -1 )
                                                        jilu[i+1][j]++;
                                        if( j > 0)
                                                if( jilu[i][j-1] != -1)
                                                        jilu[i][j-1]++;
                                        if( j+1 < m)
                                                if( jilu[i][j+1] != -1)
                                                        jilu[i][j+1]++;
                                        if( i > 0 && j > 0)
                                                if( jilu[i-1][j-1] != -1)
                                                        jilu[i-1][j-1]++;
                                        if( i+1 < n && j+1 < m)
                                                if( jilu[i+1][j+1] != -1)
                                                        jilu[i+1][j+1]++;
                                        if( j+1 < m && i > 0)
                                                if( jilu[i-1][j+1] != -1)
                                                        jilu[i-1][j+1]++;
                                        if( i+1 < n && j>0)
                                                if( jilu[i+1][j-1] != -1)
                                                        jilu[i+1][j-1]++;
                                }
                        }
                cout << "Field #" << t++ << ":" << endl;
                for( i = 0; i < n; i++){
                        for( j = 0; j < m; j++){
                                if( jilu[i][j] == -1)   //如果是雷, 输出‘*’
                                        cout << "*" ;
                                else
                                        cout << jilu[i][j];
                        }
                        cout << endl;
                }
                cout <<endl;   //输出最后有个空行
                cin >> n >> m;
       }
        return 0;
}


 

0.0分

0 人评分

  评论区

  • «
  • »