【藍橋杯】-Minesweeper
阿新 • • 發佈:2018-12-09
解題思路:
題目大意是說找‘.’ 的周圍有多少個‘*’這個周圍是八個方向。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
static const int MAX=101;
int n,m;
int main(){
int i,j,c=1;
while(cin>>n>>m&&n&&m){
//新增外圈
int b[MAX][MAX]={0};
char a[n+2][m+2];
//輸入
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cin>>a[i][j];
}
}
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(a[i][j]=='.'){
if(a[i-1][j-1]=='*') b[i][j]++;
if(a[i-1][j]=='*') b[i][j]++;
if(a[i-1][j+1]=='*') b[i][j]++;
if(a[i+1][j-1]=='*') b[i][j]++;
if(a[i+1][j+1]=='*') b[i][j]++;
if(a[i][j-1]=='*') b[i][j]++;
if(a[i][j+1]=='*') b[i][j]++;
a[i][j]=b[i][j]+'0';
}
else continue;
}
}
cout<<"Field #"<<c<<":"<<endl;
for(i=1;i<=n;i++){
cout<<a[i][j];
}
cout<<endl;
}
c++;
cout<<endl;
}
}