| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
第一次深搜。。。看了下别人的 总结下 代码+注释。。。继续努力!//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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator