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

洪水又来了dfs0秒过!!!!!附代码!!!

Posted by wocha at 2012-05-01 14:38:44 on Problem 1979
#include<stdio.h>
char str[32][32];
int t=0,m,n;
void Dfs(int x,int y)
{
//	char c=str[x][y];
	//str[x][y]='#';
	if(str[x][y-1]=='.'&&x>=0&&x<m&&y-1>=0&&y-1<n)
	{
   	t++;
		str[x][y-1]='#';
		//	printf("%d\n",t);
		Dfs(x,y-1);
	}
	if(str[x][y+1]=='.'&&x>=0&&x<m&&y+1>=0&&y+1<n)
	{
		 t++;
    str[x][y+1]='#';
    //	printf("%d\n",t);
		Dfs(x,y+1);
	}
	if(str[x-1][y]=='.'&&x-1>=0&&x-1<m&&y>=0&&y<n)
	{
  str[x-1][y]='#';
    t++;
		Dfs(x-1,y);
		//	printf("%d\n",t);
	}
	if(str[x+1][y]=='.'&&x+1>=0&&x+1<m&&y>=0&&y<n)
	{
		t++;
    str[x+1][y]='#';
    //	
		Dfs(x+1,y);
	}
//	str[x][y]=c;
	return ;
}
int main()
{
	int i,j,k,l,q,w,e,r,p;
	while(scanf("%d%d",&n,&m),n,m)
	{
		 for(i=0;i<m;i++)
			 scanf("%s",&str[i]);
			 for(i=0;i<m;i++)
			 {
				for(j=0;j<n;j++)
				if(str[i][j]=='@')
					  break;
					if(str[i][j]=='@')
					break;
				}
				Dfs(i,j);
			printf("%d\n",t+1);
			t=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