#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
constexpr int N = 110;
char map[N][N], ans[N][N];
int n, group;
bool st[N][N];
#define fore(i, a, n) for (int i = a; i <= n; i ++)
struct awa{
int x, y;
}le[N];
const int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy[] = {1, 0, -1, 1, -1, 0, 1, -1};
const string str = "0yizhong"; // 记得下表要 -1
void print(char map[][110]){
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= n; j ++)
cout << map[i][j];
cout << endl;
}
}
void dfs(int x, int y, int num){ // 在(x, y)搜索 在搜num字母
if (num > 7) return;
for (int i = 0; i < 8; i ++){
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || xx > n || yy < 1 || yy > n || st[xx][yy]) continue;
// 这里我想的是 我只匹配一次 不会再搜第二次同一个字母
if (map[xx][yy] == str[num]){
ans[xx][yy] = str[num];
st[xx][yy] = true;
dfs(xx, yy, num + 1);
st[xx][yy] = false;
}
}
}
int main(){
memset(ans, '*', sizeof ans);
cin >> n;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++){
cin >> map[i][j];
if (map[i][j] == 'y'){ // 找y开始搜
group ++;
le[group].x = i;
le[group].y = j;
}
}
for (int i = 1; i <= group; i ++)
dfs(le[i].x, le[i].y, 1);
print(ans);
}
样例2运行结果:
*yizhong
gy**h***
n*i*****
o**z****
hh**h***
zz***o**
i****gng
yyyy*ggg