扫雷 C++
扫雷
https://ac.nowcoder.com/acm/problem/22232
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
char a[105][105], b[105][105];
int main(){
int n, m;
cin >> n >> m;
getchar();
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
cin >> a[i][j];
}
getchar();
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
int cnt = 0;
if(a[i][j] == '*') b[i][j] = '*';
else{
if(i > 0 && j > 0 && a[i - 1][j - 1] == '*') ++cnt;
if(i > 0 && a[i - 1][j] == '*') ++cnt;
if(i > 0 && j + 1 < m && a[i - 1][j + 1] == '*') ++cnt;
if(j > 0 && a[i][j - 1] == '*') ++cnt;
if(j + 1 < m && a[i][j + 1] == '*') ++cnt;
if(i + 1 < n && j > 0 && a[i + 1][j - 1] == '*') ++cnt;
if(i + 1 < n && a[i + 1][j] == '*') ++cnt;
if(i + 1 < n && j + 1 < m && a[i + 1][j + 1] == '*') ++cnt;
b[i][j] = cnt + '0';
}
cout << b[i][j];
}
cout << endl;
}
return 0;
}
查看14道真题和解析