解题,稍微有点长
2026-09-26 17:22:21
发布于:广东
0阅读
0回复
0点赞
#include <iostream>
#include <string>
using namespace std;
// 8个方向:上下左右 + 四个斜角
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int main()
{
int n, m;
cin >> n >> m;
string map[105];
for(int i = 0; i < n; i++)
{
cin >> map[i];
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(map[i][j] == '*')
{
cout << '*';
}
else
{
int cnt = 0;
// 遍历8个方向
for(int d = 0; d < 8; d++)
{
int x = i + dx[d];
int y = j + dy[d];
// 判断坐标是否在合法范围内
if(x >= 0 && x < n && y >=0 && y < m)
{
if(map[x][y] == '*')
cnt++;
}
}
cout << cnt;
}
}
cout << endl;
}
return 0;
}
这里空空如也



有帮助,赞一个