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]={0,-1,1,0};
char a[35][85];
void dfs(int x,int y)
{
	for(int i=0; i<4; i++)
	{
		int xx = x+dx[i];
		int yy = y+dy[i];
		if(a[xx][yy]==' ')
		{
			a[xx][yy]='#';
			dfs(xx,yy);
		}
	}
	return ;
}
int main()
{
	int T;
	scanf("%d",&T);
	getchar();
	while(T--)
	{
		int i =0;
		int j;
		memset(a,'\0',sizeof(a));
		while(gets(a[i++]))
		{
			if(a[i-1][0]=='_')
			{
				i--;
				break;
			}
		}
		for(int k=0; a[k][0]!='_'; k++)
		{
			for(j=0; a[k][j]!='\0'; j++)
			{
				if(a[k][j]=='*')
				{
					a[k][j]='#';
					dfs(k,j);
				}
			}
		}
		int k;
		for( k =0; a[k][0]!='_'; k++)
			puts(a[k]);
		puts(a[k]);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 19:43:42

uva 784 Maze Exploration(简单dfs)的相关文章

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 charact

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

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

UVa 572 Oil Deposits(简单DFS)

题意: 给出油田的数量,八连通的“@”认为是一个油田.和POJ 2386 Lake Counting这个题几乎一样. 直接上代码: #include<iostream> using namespace std; char maze[105][105]; int n,m; void dfs(int x,int y) { maze[x][y]='*'; for(int dx=-1;dx<=1;dx++) for(int dy=-1;dy<=1;dy++) { int nx=x+dx,n

UVa 784 迷宫探索

题意:迷宫里有X作为墙,空格为可通过,*标记起点.然后需要你现在来刷墙,刷那些由起点可达的房间的墙,即找由起点可达的连通块.这里题目说墙是除几个字符之外的可打印字符,没有说是X.做题时当成了X,结果AC了~题目说到墙都是3格子宽1格子厚,好像也没什么用~ 思路:输入时找到起点并记下,然后由起点深搜. 注意:从网站拷贝样例数据到txt时,它自动地少了很多空格,导致这个测试数据时不符合题意的.(不容易发现复制粘贴过程中少了很多空格~)这样的话,如果是我代码中dfx中的注释掉的那个 if 语句,程序就

UVA 10317 - Equating Equations 贪心 dfs

UVA 10317 - Equating Equations 贪心 dfs ACM 题目地址:UVA 10317 - Equating Equations 题意: 给一个等式,但是这个等式不一定是正确的,要你对等式中的数字重新排序,使得等式成立.等式只有+和-,数字个数小于16. 分析: 以a + b - c = d - e为例子. 1. 我们把等式右边的各项都换到左边,a + b - c - d + e = 0 2. 把+项和-项放一起,就变成(a + b + e) - (c + d) = 0

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

Red and Black(简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12519    Accepted Submission(s): 7753 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

ZOJ2165 简单DFS搜索

1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #define MAXN 25 6 using namespace std; 7 int h,w; 8 int ans; 9 int dir[4][2]={-1,0,1,0,0,-1,0,1}; 10 char map[MAXN][MAXN]; 11 12 bool ok(int x,int