`
Jianquan
  • 浏览: 18892 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

UVa 784 Maze Exploration

    博客分类:
  • UVa
阅读更多

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=725

这道题也是一道非常裸的dfs

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;

char map[40][90];
int line,len[40];//迷宫的行数、每一行的字符数

void dfs(int i,int j)
{
	map[i][j]='#';
	int dir[4][2]={-1,0,0,-1,0,1,1,0};
	for(int k=0;k<4;k++)
	{
		int nexti=i+dir[k][0],nextj=j+dir[k][1];
		if(map[nexti][nextj]==' ')
			dfs(nexti,nextj);
	}
}
void print()
{
	for(int i=0;i<line;i++)
	{
		for(int j=0;j<len[i];j++)
			cout<<map[i][j];
		cout<<'\n';
	}
}		
int main()
{
	//freopen("in.txt","r",stdin);
	int i,j,n,starti,startj;
	string str;
	cin>>n;
	getchar();
	while(n--)
	{		
		i=0;
		while(getline(cin,str)&&str[0]!='_')
		{
			len[i]=str.length();
			for(j=0;j<len[i];j++)
			{
				if(str[j]=='*')
				{
					starti=i;
					startj=j;
				}
				map[i][j]=str[j];
			}
			i++;
		}
		line=i;
		dfs(starti,startj);
		print();
		cout<<str<<endl;//打印最后一行的下划线
	}
	return 0;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics