uva 784 Maze Exploration(DFS遍历图)

来源:互联网 发布:国中土耳其 知乎 编辑:程序博客网 时间:2024/06/01 08:09

                         uva 784 Maze Exploration


A maze(迷宫) ofrectangular(矩形的) rooms is represented on a twodimensional(空间的) grid asillustrated(阐明) in figure 1a. Each point of the grid is represented by a character. The points of room walls are marked by the same character which can be any printable(印得出的) character different than `*', `_' and space. In figure 1 this character is `X'. All the other points of the grid are marked by spaces.

               XXXXXXXXXXXXXXXXXXXXX             XXXXXXXXXXXXXXXXXXXXX               X   X   X   X   X   X             X###X###X###X   X   X               X           X   X   X             X###########X   X   X               X   X   X   X   X   X             X###X###X###X   X   X               XXXXXX XXX XXXXXXXXXX             XXXXXX#XXX#XXXXXXXXXX               X   X   X   X   X   X             X   X###X###X###X###X               X   X     *         X             X   X###############X               X   X   X   X   X   X             X   X###X###X###X###X               XXXXXXXXXXXXXXXXXXXXX             XXXXXXXXXXXXXXXXXXXXX
a) Initialmaze(迷宫)                    b) Painted maze

Figure 1. Mazes of rectangular(矩形的) rooms

All rooms of the maze are equal sized with all walls 3 points wide and 1 point thick asillustrated(阐明) in figure 2. In addition, a wall is shared on its full length by the separated rooms. The rooms can communicate through doors, which are positioned in the middle of walls. There are no outdoor doors.

                     door                       |                     XX XX                     X . X   measured from within the room               door - ...--  walls are 3 points wide                     X . X__                     XXXXX  |                       |___  walls are one point thick
Figure 2. A room with 3 doors

Your problem is to paint all rooms of a maze which can be visited starting from a given room, called the `start room' which is marked by a star (`*') positioned in the middle of the room. A room can be visited from another room if there is a door on the wall which separates the rooms. By convention(大会), a room is painted if its entire surface, including the doors, is marked by the character `#' as shown in figure 1b.

Input 

The programinput(投入) is a text filestructured(有结构的) as follows:

1.
The first line contains a positive(积极的)integer(整数) which shows the number of mazes to be painted.
2.
The rest of the file contains the mazes.

The lines of the input file can be of different length. The text which represents a maze isterminated(终止) by a separation line full ofunderscores(底线) (`_'). There are at most 30 lines and at most 80 characters in a line for eachmaze(迷宫)

The program reads the mazes from the input(投入) file, paints them and writes the painted mazes on the standardoutput(输出).

Output 

The output text of a painted maze has the same format as that which has been read for that maze, including the separation lines. The example belowillustrates(阐明) a simple input which contains a single maze and the corresponding output.

Sample Input 

2XXXXXXXXXX   X   XX *     XX   X   XXXXXXXXXXX   XX   XX   XXXXXX_____XXXXXX   XX * XX   XXXXXX_____

Sample Output 

XXXXXXXXXX###X###XX#######XX###X###XXXXXXXXXXX   XX   XX   XXXXXX_____XXXXXX###XX###XX###XXXXXX_____

题目大意:将可以走到的地方标记成'#'(下划线是终止一组数据)

解题思路:DFS遍历的同时直接修改map.


#include<stdio.h>#include<string.h>char map[35][85], cnt = 0;void DFS(int a, int b) {map[a][b] = '#';for (int i = -1; i <= 1; i++) {for (int j = -1; j <= 1; j++) {if (i == 0 && j == 0) {continue;}if (a + i < 0 || a + i > cnt) {continue;}if (b + j < 0 || b + j > strlen(map[a + i])) {continue;}if (map[a + i][b + j] == '#' || map[a + i][b + j] == 'X') {continue;}DFS(a + i, b + j);}}}int main() {int n;scanf("%d\n", &n);while (n--) {memset(map, 0, sizeof(map));cnt = 0;while (gets(map[cnt]) != NULL) {  if (strcmp(map[cnt], "_____") == 0) { break;  }cnt++;  }for (int i = 0; i <= cnt; i++) {for (int j = 0; j < strlen(map[i]); j++) {if (map[i][j] == '*') {DFS(i, j);}}}for (int i = 0; i <= cnt; i++) {puts(map[i]);}}return 0;}





0 0