UVa 784 - Maze Exploration

此題是我做的搜索中最簡單的一道........很快就AC了,主要要注意scanf和gets輸入之間的差別,,,,scanf後要把後面的換行符用getchar取走,不然gets會出錯.....

#include<cstdio>
#include<cstring>
const int LINE=32;
const int CLOUMN=82;
char str[LINE][CLOUMN];
int row,col;
int h;
void init()
{
	int i;
	row=0;col=0;
	memset(str, 0, sizeof(str));
	for( i=0; i<30; i++)
	{
		gets(str[i]);
		if(str[i][0] == '_')
			break;
		if(strchr(str[i],'*'))
		{
			col=strchr(str[i],'*')-str[i];
			row=i;
		}
	}
	h=i;
}
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
void dfs(int x, int y)
{
	str[x][y]='#';
	int nx,ny;
	for(int i=0; i<4; i++)
	{
		nx=x+dx[i];
		ny=y+dy[i];
		if(str[nx][ny] == ' ')
		{
			//str[nx][ny]='#';
			dfs(nx,ny);
		}
	}
}
int main()
{
	int n;
	scanf("%d", &n);
	getchar();
	while(n--)
	{
		init();
		dfs(row,col);
		for(int i=0; i<=h; i++)
			printf("%s\n",str[i]);
	}
	return 0;
}


發佈了59 篇原創文章 · 獲贊 8 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章