题目:
思想:
1、遍历每个点是否有地雷,有地雷则直接返回为9,无地雷则遍历该点的周围八个点,计数一共有多少个地雷,则返回该数。
代码:
#include<iostream>
using namespace std;
int g[110][110];
int dx[] = { -1,-1,0,1,1,1,0,-1 }, dy[] = { 0,1,1,1,0,-1,-1,-1 };
int dfs(int x, int y)
{int ans = 0;if (g[x][y] == 1) return 9;for (int i = 0; i < 8; i++)if (g[x + dx[i]][y + dy[i]] == 1)ans++;return ans;}
int main()
{int n, m;cin >> n >> m;for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)cin >> g[i][j];for (int i = 1; i <= n; i++){for (int j = 1; j <= m; j++)cout << dfs(i, j) << " ";cout << endl;}}