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

Re:dfs 纯是自己做的,一次ac了,太爽了!!

Posted by 731371663 at 2010-07-20 12:55:24 on Problem 1562
In Reply To:Re:dfs 纯是自己做的,一次ac了,太爽了!! Posted by:15038029147 at 2010-04-18 22:40:53
#include<iostream>
#include<cstring>
#define N 101
char map[N][N];
int n,m,sum;

void dfs(int i,int j)
{	
	if(map[i][j]!='@'||i<0||j<0||i>=m||j>=n) return;
	else
	{
		map[i][j]='.';//已访问,覆盖
		dfs(i-1,j-1);//朝其余的8个方向搜索
		dfs(i-1,j);
		dfs(i-1,j+1);
		dfs(i,j-1);
		dfs(i,j+1);
		dfs(i+1,j-1);
		dfs(i+1,j);
		dfs(i+1,j+1);
	}
}
int main()
{
	//freopen("in.txt","r",stdin);
	int i,j;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		if(m==0) break;
		sum=0;
		for(i=0;i<m;i++)
			scanf("%s",map[i]);
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			{
				if(map[i][j]=='@')
				{
					dfs(i,j);
				    sum++;
				}
			}
		}
		printf("%d\n",sum);
	}
	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