还是普通的DFS,没有任何坑。。
#include<stdio.h>
#include<string.h>
const int N = 100 ;
char maze[N][N];
void init () {
for (int i = 0; i < N ; i++) {
for (int j = 0; j < N ;j ++) {
maze[i][j] = 'X';
}
}
}
void dfs (int x,int y) {
if (maze[x][y] == 'X' || maze[x][y] == '#')
return ;
maze[x][y] = '#';
dfs(x - 1,y - 1);
dfs(x - 1,y);
dfs(x - 1,y + 1);
dfs(x , y - 1);
dfs(x , y + 1);
dfs(x + 1, y - 1);
dfs(x + 1,y);
dfs(x + 1, y + 1);
}
int main () {
char ch;
int t;
scanf("%d",&t);
getchar();
while (t--) {
init();
int i;
for (i = 0 ; ;i++) {
for (int j = 0 ;; j++) {
ch = getchar();
if (ch == '\n') {
maze[i][j] = '\0';
break;
}
maze[i][j] = ch;
}
if (maze[i][0] == '_')
break;
}
for (int k = 0 ; k < i ;k++) {
for (int j = 0 ; j < strlen(maze[k]) ; j++) {
if (maze[k][j] == '*') {
dfs(k ,j);
}
}
}
for (int k = 0; k <= i; k++)
printf ("%s\n",maze[k]);
}
return 0;
}