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 niuzheng168 at 2009-07-21 23:15:46 on Problem 1164
//POJ1164
//DFS
#include <stdio.h>
#include <string.h>

int visit[100][100];
int s,map[100][100],m,n,number=0,max_size=0;

void dfs(int i,int j)
{
	s++;//temp size add
	visit[i][j]=1;//has been visited
	if(map[i][j]%2==0)//judge west
	{
		if(!visit[i][j-1])
			dfs(i,j-1);
	}
	map[i][j]>>=1;//the value divide 2,the value of west is 1,when divide 2,it lose and 0,1,2,4 remain. so the west didn't work.
	if(map[i][j]%2==0)//judge north
	{
		if(!visit[i-1][j])
			dfs(i-1,j);
	}
	map[i][j]>>=1;//the north didn't work,0 0 1 2 remain.
	if(map[i][j]%2==0)//judge east
	{
		if(!visit[i][j+1])
			dfs(i,j+1);
	}
	map[i][j]>>=1;//the east didn't work
	if(map[i][j]%2==0)//judge south
	{
		if(!visit[i+1][j])
			dfs(i+1,j);
	}
}

int main()
{
	int i,j;
	scanf("%d%d",&m,&n);
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
			scanf("%d",&map[i][j]);//input
	memset(visit,0,sizeof(visit));//clear the array
	for(i=0;i<m;i++)
		for(j=0;j<n;j++)
		{
			if(!visit[i][j])//has not been visited
			{
				s=0;//temp size
				number++;// the number of rooms
				dfs(i,j);
				if(s>max_size)//find the max_size
					max_size=s;
			}
		}
	printf("%d\n%d",number,max_size);
	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