UVA 784 Maze Exploration

來源:互聯網
上載者:User

標籤:uva   dfs   graph   acm   

題目如下:

Maze Exploration 

A maze of rectangular rooms is represented on a twodimensional grid as illustrated in figure 1a. Each point of thegrid is represented by a character. The points of room walls aremarked by the same character which can be any printable characterdifferent than `*‘, `_‘ and space. In figure 1 this character is`X‘. All the other points of the grid are markedby 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) Initial maze                    b) Painted maze

Figure 1. Mazes of rectangular rooms

All rooms of the maze are equal sized with all walls 3points wide and 1 point thick as illustrated in figure 2. Inaddition, a wall is shared on its full length by the separatedrooms. The rooms can communicate through doors, which arepositioned 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 bevisited starting from a given room, called the `start room‘ whichis marked by a star (`*‘) positioned in the middle of theroom. A room can be visited from another room if there is a dooron the wall which separates the rooms. By convention, a room ispainted if its entire surface, including the doors, is markedby the character `#‘ as shown in figure 1b.

Input The program input is a text file structured as follows:
1.
The first line contains a positive integer which shows thenumber 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. Thetext which represents a maze is terminated by a separation linefull of underscores (`_‘). There are at most 30 lines andat most 80 characters in a line for each maze

The program reads the mazes from the input file, paints themand writes the painted mazes on the standard output.

Output The output text of a painted maze has the same format as thatwhich has been read for that maze, including the separationlines. The example below illustrates a simple input whichcontains 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題,我用兩種思路都寫了。一種是利用題目的特點直接類比,一種是DFS,兩種思路的代碼都給出來。中間RTE了無數次TAT,原因就是把x和y搞反了,一直沒發現。找出起始房間然後直接DFS即可,題目挺簡單的。

AC的代碼如下:

直接類比版

DFS版



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.