Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

写了个代码,不知道算不算是DFS?

Posted by xcl119xcl at 2007-08-02 21:12:55 on Problem 1979
//这两天一直在研究什么是DFS,BFS,看了半天,有点感觉头晕,总的感觉是利用递归回朔的方法(不知道是不是正确)反正就是一个连一个的“传染”下去,没有合适的再回头看上一个有没有其他的路径,
代码如下,请批评指教:(我知道贴代码不好,但是我觉得有个ac的代码,起码可以给后面的朋友一些参考思路阿,我就喜欢研究别人的代码,研究多了,就会有感觉的阿,虽然我还是很差的)
//1979:Red and Black 
#include <iostream>
using namespace std;

int tile[20][20]={0};
int total=1;
int a,b;

void top(int x,int y)
{
		if(tile[x][y]==1)
		{
			total++;
			tile[x][y]=2;
		
			if(y>0&&tile[x][y-1]==1)
			{
				top(x,y-1);
			}
			if(y<(a-1)&&tile[x][y+1]==1)
			{
				top(x,y+1);
			}
			if(x>0&&tile[x-1][y]==1)
			{
				top(x-1,y);
			}
			if(x<(b-1)&&tile[x+1][y]==1)
			{
				top(x+1,y);
			}
		}
	
}

int main()
{
	char ch;
	int x=0,y=0;
	while(cin>>a>>b&&a!=0&&b!=0)
	{
		total = 0;
		for(int i=0;i<b;i++)
		{
			for(int j=0;j<a;j++)
			{
				cin>>ch;
				if(ch=='.')
				{
					tile[i][j]=1;
					continue;
				}
				if(ch=='#')
				{
					tile[i][j]=0;
					continue;
				}
			    if(ch=='@')
				{
					x=i;
					y=j;
					tile[i][j]=1;
				}
			}
		}
		//position(x,y);
		top(x,y);
		cout<<total<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator