付翔的专栏
在鄙视中成长 记录成长的点滴
posts - 106,  comments - 32,  trackbacks - 0
找出油田的数量 简单的dfs 搜索遍历 入门题

http://acm.hdu.edu.cn/showproblem.php?pid=1241
#include <stdio.h>
#include 
<string.h>
#define N 110
int map[N][N];

int n,m,ans;
int visted[N][N];

void dfs( int x,int y)
{
    
if(map[x][y]==0 || visted[x][y] == 1)
        
return;
    visted[x][y] 
= 1;
    dfs(x
-1,y-1);dfs(x-1,y);dfs(x-1,y+1);dfs(x,y+1);
    dfs(x
+1,y+1);dfs(x+1,y);dfs(x+1,y-1);dfs(x,y-1);
    
}
void Solve()
{
    
int i,j;
    
for(i = 1; i <=m;i++ )// map的边界全是0
        for(j = 1; j <=n; j ++)
            
if(map[i][j] == 1 && !visted[i][j])
                ans
++,dfs(i,j);
}
int main()
{
    
int i,j;
    
char str[N];
    
while(scanf("%d%d",&m,&n),m!=0||n!=0)
    {
        memset(map,
0,sizeof(map));
        memset(visted,
0,sizeof(visted));
        
for(i = 0; i <m;i++ )
        {
                scanf(
"%s",str);
            
for(j = 0; j < n; j ++)
            {
                
if(str[j]=='@') map[i+1][j+1= 1;
            
            }
        }
        ans 
= 0;
        Solve();
        printf(
"%d\n",ans);
    }
    
return 0;
}


posted on 2010-05-18 13:16 付翔 阅读(1212) 评论(1)  编辑 收藏 引用 所属分类: ACM 数据结构

FeedBack:
# re: 1241 Oil Deposits
2010-05-18 13:20 | 罗莱家纺官方网站
不错~支持了  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2011年2月>
303112345
6789101112
13141516171819
20212223242526
272812345
6789101112

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

CSDN - 我的blog地址

博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜