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

超时,帮看下

Posted by dynamic_study at 2009-08-01 17:21:03 on Problem 3051
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn1 100
#define maxn2  1001
char map[maxn2][maxn1];
bool flag[maxn2][maxn1];
int n,m,cnt,min1;
int dir[4][2]=
{
0,1,
1,0,
0,-1,
-1,0
};

void dfs(int si,int sj,int ans)
{
	int i;    
	if(sj<0||sj>=m||si<0||si>=n)
		return ;
	flag[si][sj]=false;
	if(ans>min1)
		min1=ans;
	for(i=0;i<4;i++)
	{
		if(flag[si+dir[i][0]][sj+dir[i][1]]==true)
		{
			flag[si+dir[i][0]][sj+dir[i][1]]=false;
			dfs(si+dir[i][0],sj+dir[i][1],ans+1);
			flag[si+dir[i][0]][sj+dir[i][1]]=true;
		}
	}
}

int main()
{
	int i,j,loop,maxx;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='*')
				   flag[i][j]=true;
				else
					flag[i][j]=false;
			}
		}
		maxx=-99999;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(flag[i][j]==true)
				{
					min1=-999999;
	         	    dfs(i,j,1);
					if(maxx<min1)
						maxx=min1;
				}
			}
		}
		cout<<maxx<<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