UVA 784 Maze Exploration (DFS traversal graph)

Source: Internet
Author: User
Tags compact

UVA 784 Maze Exploration


AMaze(Maze)OfRectangular(rectangular)Rooms is represented on adimensional(of space)Grid asIllustrated(clarified)In Figure 1a. Each point of the grid was represented by a character. The points of the walls is marked by the same character which can is anyprintable(printed)Character different than '*', '_' and space. In Figure 1 This character is 'X‘. All the other points of the grid is 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               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(Maze)b) Painted Maze

Figure 1. Mazes of rectangular(rectangular) rooms

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

                     Door                       |                     xx xx                     X. X   measured from within the               room door-...-Walls is  3 points wide                     X. x__                     XXXXX  |                       | ___ Walls is one point  thick
Figure 2. A-Class with 3 doors

Your problem is to paint all rooms of a maze which can being visited starting from a given, called the ' Start hostel ' whic H is marked by a star ('*') positioned in the middle of the guest. A-class can be visited from another, if there is a door on the wall which separates the rooms. by Convention(General Assembly), A-a-painted if its entire surface, including the doors, was marked by the Charac Ter '#' as shown in Figure 1b.

InputThe programinput(Put) is a text filestructured(structured) as follows:

1.
The first line contains a positive integer (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 (termination) by a separation line full Of Underscores (bottom line) (' _ '). There is at the most of lines and at the most, characters in a line for Eachmaze (Maze)

The program reads the mazes from the input(input) file, paints them and writes the painted mazes on the STA Ndardoutput (outputs).

Outputthe output text of a painted maze have the same format as that which have been read for that maze, including the Separat Ion lines. The example belowillustrates(clarified) a simple input which contains a single maze and the corresponding OUTPU T.

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_____

To mark the place you can go to ' # ' (the underscore is to terminate a set of data)

Solution idea: DFS traversal while directly modifying the 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) { C0/>if (strcmp (map[cnt], "_____") = = 0) {break;  } cnt++;  } for (int i = 0; l <= 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;}





UVa 784 Maze Exploration (DFS traversal graph)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.