uva 784 Maze Exploration(DFS遍历图)

uva 784 Maze Exploration

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
input(投入) file, paints them and writes the painted mazes on the standard
output(输出).

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 example below
illustrates(阐明) a simple input which contains a single maze and the corresponding 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
_____

题目大意:将可以走到的地方标记成‘#‘(下划线是终止一组数据)

解题思路:DFS遍历的同时直接修改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) {
			if (strcmp(map[cnt], "_____") == 0) {
				break;
			}
			cnt++;
		}
		for (int i = 0; i <= 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;
}
时间: 2024-10-12 14:07:23

uva 784 Maze Exploration(DFS遍历图)的相关文章

uva 784 Maze Exploration(简单dfs)

这道题看上去很麻烦,什么迷宫啊,门之类的,其实挺简单的,就是让把与 * 连通的都置为 # 包括 * , 直接dfs就可以了,不过我wa了好多次...最后竟然是多读了一个换行,忘了加getchar()了,gets()函数 会把缓冲区里面的换行给读进去的...应该把换行去掉,血的教训啊... 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int dx[4]={-1,0,0,1}; int dy[4]=

UVa 784 Maze Exploration

方法:dfs(flood fill) dfs code: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <stack> 8 #include <bitset> 9 #include &l

DFS遍历图时的小技巧

DFS遍历图时的小技巧 我们通常使用DFS遍历图时,用vis[i]=true;来标记访问过的节点,但是如果要让我们统计图中所有边的长度的话,如果我们还这样做的话,对于非环形图来说,没问题,但是对于环形图来说,就可能访问不到最后一条边,如A-B-C-A,A标记之后就不能统计到C-A了. 这时我们的办法是,每访问一条边后,就把它销毁,然后递归地去DFS时不再以vis[i]==false为条件去递归,而是直接以G[i][j]!=0为条件去递归. 这也是和平时DFS遍历图时有区别的地方,一个小技巧吧.

邻接表存储图,DFS遍历图的java代码实现

import java.util.*; public class Main{ static int MAX_VERTEXNUM = 100; static int [] visited = new int[MAX_VERTEXNUM]; public static void main(String [] args){ Graph G = new Graph(); creatGraph(G); output(G); for(int i=0;i<G.vertex_num;i++) visited[i

784 - Maze Exploration

#include <stdio.h> #include <string.h> char maze[50][100]; void search(int i,int j) { if(maze[i][j]!='*' && maze[i][j]!='_' && maze[i][j]!=' ') return; maze[i][j]='#'; search(i-1,j-1); search(i-1,j); search(i-1,j+1); search(i,j

图的邻接表存储c实现(DFS遍历)

先简要列出实现过程中所需要的数据结构. 如下图 对于这个图而言,它的邻接表可以这样表示,当然表现形式可以多样,这只是我随便画的一种表示方法. 顶点表                                          边表 我们把第一个表即上面标着fixedvex的这个表称作顶点表,后边的称为边表. 上图所示,边表的结构应该这样写: //定义一个边表节点的结构 typedef struct node{ int adjvex; //int Mark; //用于标记是否被访问过 nod

图的dfs遍历和bfs遍历

对如下图进行广度和深度遍历; dfs遍历,(依次输出遍历顶点): 用邻接矩阵存图(用一个二维数组把图存起来)! <span style="font-size:18px;">#include<stdio.h> #define MAX 9999999//当顶点之间不相通时,标记为一个很大的数 int sum=0;//记录遍历的顶点的个数 int v,s;//顶点数和边数 int book[50]={0},p[30][30];//标记数组和矩阵 void dfs(in

UVA 784-Maze Exploration(dfs)

Maze Exploration 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 printa

rwkj 1501 数据结构:图的DFS遍历

数据结构:图的DFS遍历 时间限制(普通/Java):1000MS/3000MS            运行内存限制:65536KByte 总提交:259            测试通过:183 描述 从已给的连通图中某一顶点出发,沿着一些边访遍图中所有的顶点,且使每个顶点仅被访问一次,就叫做图的遍历.图的遍历的遍历有DFS和BFS两种. 上面的图,从顶点0出发,按照顶点序号从小到大的顺序DFS,得到遍历顺序为0 1 2 3  4 5 6 7 8. 输入 输入图的顶点个数(<20)与边数,以及每