Let's talk about:
Shows the meaning of the question in advance:
XXXXXXXXX
X
X * x
X
XXXXXXXXX
X
X
X
XXXXX
You can think of it as a House. The character 'X' is the wall, and you are now in the location of '*'. Ask you to fill in the blank space around, '*' is replaced. Note that the firewall cannot be crossed. In fact, it is the most basic issue of deep traversal. You only need to find the location of '*' and check whether there are spaces around it. If yes, fill in '#' and then recursion, otherwise, the function will return directly.
Source code:
# Include <stdio. h> # include <string. h> # define maxw 80 + 5 # define maxh 30 + 5 char room [maxh] [maxw]; void DFS (INT, INT); int main () {int t, n, x, y, I; freopen ("data", "r", stdin); // scanf ("% d", & T); getchar (); while (t --) {n = 0; while (gets (room [N]) if (room [N] [0] = '_') break; else n ++; For (x = 0; x <n; X ++) // locate the initial position for (y = 0; y <strlen (room [x]); Y ++) if (room [x] [Y] = '*') DFS (x, y); For (x = 0; x <= N; X ++) printf ("% s \ n", room [x]);} return 0;} void DFS (int x, int y) {If (room [x] [Y] = ''| room [x] [Y] = '*') room [x] [Y] = '#'; else return; // traverse the perimeter of the current point DFS (x-1, Y-1); DFS (x, Y-1 ); DFS (x + 1, Y-1); DFS (x-1, Y); DFS (x + 1, Y); DFS (x-1, Y + 1); DFS (X, Y + 1); DFS (x + 1, Y + 1); return ;}
Maze interval ation (Simple Deep-first search)