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 dynamic_study at 2009-07-16 14:42:43 on Problem 1562
#include<iostream>
using namespace std;
char map[100][100];
bool use[100][100];
int m,n;
void dfs(int i,int j)
{
	if((i==0||j==0||i==(m+1)||j==(n+1))&&map[i][j]=='*')
		return;
	use[i][j]=true;
	if(map[i][j]!='*'&&map[i-1][j-1]=='@'&&(i-1)>=1&&(j-1)>=1&&(i-1)<=m&&(j-1)<=n)
	  dfs(i-1,j-1);
	if(map[i][j]!='*'&&map[i-1][j]=='@'&&(i-1)>=1&&j>=1&&(i-1)<=m&&j<=n)
	  dfs(i-1,j);
	if(map[i][j]!='*'&&map[i-1][j+1]=='@'&&(i-1)>=1&&(j+1)>=1&&(i-1)<=m&&(j+1)<=n)
	  dfs(i-1,j+1);
	if(map[i][j]!='*'&&map[i][j+11]=='@'&&i>=1&&(j+1)>=1&&i<=m&&(j+1)<=n)
	  dfs(i,j+1);
	if(map[i][j]!='*'&&map[i+1][j+1]=='@'&&(i+1)>=1&&(j+1)>=1&&(i+1)<=m&&(j+1)<=n)
	  dfs(i+1,j+1);
	if(map[i][j]!='*'&&map[i+1][j]=='@'&&(i+1)>=1&&j>=1&&(i+1)<=m&&j<=n)
	  dfs(i+1,j);
	if(map[i][j]!='*'&&map[i+1][j-1]=='@'&&(i+1)>=1&&(j-1)>=1&&(i+1)<=m&&(j-1)<=n)
	  dfs(i+1,j-1);
	if(map[i][j]!='*'&&map[i][j-1]=='@'&&i>=1&&(j-1)>=1&&i<=m&&(j-1)<=n)
	  dfs(i,j-1);

}
int main()
{
	
	    int i,j,ans;
		ans=0;
		while(1)
		{
		cin>>m>>n;
	    if(m==0&&n==0)
			return 0;
		for(i=0;i<=m+1;i++)
		{
			map[i][0]='*';
			use[i][0]=true;
			map[i][n+1]='*';
			use[i][n+1]=true;
		}
		for(i=0;i<=n+1;i++)
		{
			map[0][i]='*';
			use[0][i]=true;
			map[m+1][i]='*';
			use[m+1][i]=true;
		}
		for(i=1;i<=m;i++)
		{	for(j=1;j<=n;j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='*')
					use[i][j]=true;
				else if(map[i][j]=='@')
					use[i][j]=false;

			}

		}
		for(i=1;i<=m;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(map[i][j]=='@'&&use[i][j]==false)
				{
					dfs(i,j);
					ans++;
					//cout<<ans<<endl;
				}
			}
		}
	
	cout<<ans<<endl;
		}

}

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