UVA 784 Maze Exploration (DFS || 种子填充)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用深度优先搜索(DFS)解决迷宫问题的Java代码示例: ```java import java.util.Stack; public class MazeSolver { private int[][] maze; private boolean[][] visited; private int startRow, startCol; private int endRow, endCol; public MazeSolver(int[][] maze, int startRow, int startCol, int endRow, int endCol) { this.maze = maze; this.visited = new boolean[maze.length][maze[0].length]; this.startRow = startRow; this.startCol = startCol; this.endRow = endRow; this.endCol = endCol; } public boolean solve() { Stack<Integer> stack = new Stack<>(); stack.push(startRow); stack.push(startCol); while (!stack.isEmpty()) { int col = stack.pop(); int row = stack.pop(); if (row < 0 || col < 0 || row >= maze.length || col >= maze[0].length) { continue; } if (visited[row][col] || maze[row][col] == 1) { continue; } visited[row][col] = true; if (row == endRow && col == endCol) { return true; } stack.push(row + 1); stack.push(col); stack.push(row - 1); stack.push(col); stack.push(row); stack.push(col + 1); stack.push(row); stack.push(col - 1); } return false; } public static void main(String[] args) { int[][] maze = { {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 0, 0, 0}, {0, 1, 1, 0} }; MazeSolver solver = new MazeSolver(maze, 0, 0, 3, 3); if (solver.solve()) { System.out.println("Maze solved!"); } else { System.out.println("Maze cannot be solved!"); } } } ``` 这个示例中,我们使用了一个栈来实现DFS算法,每次从栈中弹出一个坐标,然后检查是否越界、已经访问过或者是墙壁,如果是,则忽略这个坐标,否则标记为已访问并将其四周的坐标入栈。如果最终栈为空,则说明无法从起点到达终点,否则,说明找到了一条能够从起点到达终点的路径。 这只是一个简单的示例,实际上,如果要解决更复杂的迷宫问题,需要使用更高级的算法来优化搜索过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值