比较简单,4个方向dfs。
#include#include #include char maze[35][85];int lines;void printMaze() { int i; for (i = 0; i < lines; i++) { printf("%s\n", maze[i]); }}void dfs(int i, int j) { if (i < 0 || j < 0 || i > 35 || j > 85) return; if (isprint(maze[i][j]) && maze[i][j] != ' ' && maze[i][j] != '*') return; maze[i][j] = '#'; dfs(i - 1, j); dfs(i + 1, j); dfs(i, j - 1); dfs(i, j + 1);}int main() { int cases; scanf("%d", &cases); getchar(); while (cases--) { memset(maze, 0, sizeof(maze)); lines = 0; while (1) { gets(maze[lines++]); if (maze[lines - 1][0] == '_') break; } int i, j; for (i = 0; i < 35; i++) { for (j = 0; j < 85; j++) if (maze[i][j] == '*') dfs(i, j); } printMaze(); } return 0;}