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 datacw at 2011-10-16 21:40:52 on Problem 3051
#include <iostream>
using namespace std;
char map[1005][82];
bool visited[1005][82];
int tmp[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int count;
int w,h;
void dfs(int a,int b)
{
	//if(visited[a][b]==true) return ;  //多余啊,导致调了半天不知道哪错啦!!!
	int i,a1,b1;
	for(i=0;i<4;i++)
	{
       a1=a+tmp[i][0];
	   b1=b+tmp[i][1];
	   if(a1>=0 && b1>=0 && a1<h && b1<w && visited[a1][b1]==false && map[a1][b1]=='*')
	   {
		    count++;
		   visited[a1][b1]=true;
		   dfs(a1,b1);
	   }
	}
	return ;
}
int main()
{
	int i,j;
	int max;
	char st[9];
	cin>>w>>h;
	cin.getline(st,9);
	for(i=0;i<h;i++)
	  cin.getline(map[i],82);
		//	for(i=0;i<h;i++)
		//		cout<<map[i]<<endl;
	memset(visited,false, sizeof(visited));
	max=0;
	for(i=0;i<h;i++)
		for(j=0;j<w;j++)
		{
			if(visited[i][j]==false && map[i][j]=='*')
			{
				count=1;
				visited[i][j]=true;
				dfs(i,j);
				if(max<count) max=count;
			}
		}
		cout<<max<<endl;
//	system("pause");
	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