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
2
XXXXXXXXX
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
XXXXXXXXX
X###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;
}