UVA 784 Maze Exploration (DFS || 种子填充)
2016-02-28 19:59
399 查看
Problem Description
A maze of rectangular rooms is represented on a two dimensional grid as illustrated 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) Initial maze 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 as illustrated
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 program input is a text file structured 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 is terminated
by a separation line full of underscores (‘ ’). There are at most 30 lines and at most 80 characters in a
line for each maze. The program reads the mazes from the standard input.
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 program writes the painted mazes on the standard output.
Sample Input
2XXXXXXXXX
X X X
X * X
X X X
XXXXXXXXX
X X
X X
X X
XXXXX
XXXXX
X X
X * X
X X
XXXXX
Sample Output
XXXXXXXXXX###X###X
X#######X
X###X###X
XXXXXXXXX
X X
X X
X X
XXXXX
XXXXX
X###X
X###X
X###X
XXXXX
题解:
发现UVa的题题目描述一向很吓人,题目描述很长,要耐心看。此题其实很简单,属于很裸的DFS,是求联通块的,这题题还有个很好听的别名,种子填充,代码简单易懂,就不多说了~
#include <iostream> #include <cstring> using namespace std; const int MAXW = 100; const int MAXH = 200; int cases, r, c, ty, tx; int direct[4][2] = { {0,-1}, {0,1}, {-1,0}, {1,0} }; char pic[MAXH][MAXW]; void dfs(int y, int x) { for (int i = 0; i < 4; ++i) { int nexty = y + direct[i][0]; int nextx = x + direct[i][1]; if (nexty < 0 || nextx < 0 || nexty >= r || nextx >= c || pic[nexty][nextx] != ' ') { continue; } pic[nexty][nextx] = '#'; dfs(nexty, nextx); } } int main() { while (scanf("%d%*c", &cases) != EOF) { while (cases--) { memset(pic, 0, sizeof(pic)); r = 0, c = 0; bool getloc = false; while (true) { cin.getline(pic[r++], MAXW); int len = strlen(pic[r - 1]); if (len > c) c = len; if (pic[r - 1][0] == '_') break; if (!getloc) { for (int i = 0; i < len; ++i) { if (pic[r - 1][i] == '*') { ty = r - 1; tx = i; getloc = true; break; } } } } pic[ty][tx] = '#'; dfs(ty, tx); for (int i = 0; i < r; ++i) for (int j = 0; ; ++j) { if (pic[i][j] == '\0') { putchar('\n'); break; } else { putchar(pic[i][j]); } } } } return 0; }
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- Hadoop FS Shell
- DFS使用方法总结
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- C 语言实现迷宫 DFS算法
- 一幅图弄清DFT与DTFT,DFS的关系
- HDFS---Namenode
- HDFS ---- Services startup
- POJ1523 SPF dfs
- poj1731 Orders dfs
- Surrounded Regions
- Binary Tree Zigzag Level Order Traversal,Restore IP Addresses,Word Search,Simplify Path
- DFS基础(1)
- HDU1241 Oil Deposits
- DFS算法有趣小题目
- ZOJ Problem Set - 1711 解题报告