#温馨提示:此文章使用的是MD编辑器,在小程序上查看可能会出现乱码,敬请谅解
【题目链接】P2670 [NOIP2015 普及组] 扫雷游戏
题解代码及思路
对于这题,最简单粗暴的方法就是爆搜一遍,反正题目中数据范围是100*100
,不会超时,超空间。
AC代码如下
#include <bits/stdc++.h>
using namespace std;
int a[105][105];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char tmp;
cin>>tmp;
if(tmp=='*') a[i][j]=1; // 地雷格标记为 1
// 这里默认非地雷格为 0
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]==1) cout<<"*"; // 如果是地雷,则输出 *
else cout<<a[i-1][j-1]+a[i][j-1]+a[i+1][j-1]+a[i-1][j]+a[i+1][j]+a[i-1][j+1]+a[i][j+1]+a[i+1][j+1]; // 否则计算并输出周围8个格子中的地雷数
}
cout<<endl;
}
return 0;
}