题解 | #扫雷#
扫雷
https://www.nowcoder.com/practice/d5f277427d9a4cd3ae60ea6c276dddfd
#include <iostream> using namespace std; int main() { int n, m; cin >> n >> m; char mat[n][m]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> mat[i][j]; } } char res[n][m]; int direction[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; char a; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if ((*(*(mat + i) + j)) == '*') a = '*'; else { a = '0'; for (int k = 0; k < 8; k++) { int ir = i + direction[k][0]; int jr = j + direction[k][1]; if (ir < 0 || ir >= n || jr < 0 || jr >= m) continue; else { if ((*(*(mat + ir) + jr)) == '*') ++a; } } } res[i][j] = a; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cout << res[i][j]; } cout << endl; } } // 64 位输出请用 printf("%lld")
按照题目要求写代码就好了
C++题解 文章被收录于专栏
记录在牛客网用C++刷题的题解思路