题目链接

题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket 。

思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接变为*,这样可以避免重复搜索。

 //POJ 1562 ZOJ 1709

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <algorithm> using namespace std ; char ch[][] ;
int dir[][] = {{-,-},{-,},{-,},{,},{,},{,},{,-},{,-}} ;
int sum ;
int m,n ; void DFS(int x,int y)
{
ch[x][y] = '*' ;
for(int i = ; i < ; i++)
{
int xx = x + dir[i][] ;
int yy = y + dir[i][] ;
if(xx >= && xx <= m && yy >= && yy <= n && ch[xx][yy] == '@')
{
DFS(xx,yy) ;
}
}
}
int main()
{ while(~scanf("%d %d",&m,&n))
{
if(m == ) break ;
for(int i = ; i < m ; i++)
scanf("%s",ch[i]) ;
sum = ;
for(int i = ; i < m ; i++)
for(int j = ; j < n ; j++)
{
if(ch[i][j] == '@')
{
DFS(i,j) ;
sum++ ;
}
}
printf("%d\n",sum) ;
}
return ;
}

POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)的更多相关文章

  1. ZOJ 1709 Oil Deposits(dfs,连通块个数)

    Oil Deposits Time Limit: 2 Seconds      Memory Limit: 65536 KB The GeoSurvComp geologic survey compa ...

  2. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  3. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  4. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  5. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  6. HDOJ/HDU 1241 Oil Deposits(经典DFS)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

  7. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

  8. Oil Deposits(DFS连通图)

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  9. 【HDU - 1241】Oil Deposits(dfs+染色)

    Oil Deposits Descriptions: The GeoSurvComp geologic survey company is responsible for detecting unde ...

随机推荐

  1. asp.net中C#获取字符串中汉字的个数实例

    符串可以包括数字,字母,汉字或者其他的字符.使用Char类型的IsDigit静态方法可以判断字符串中的字符是否为数字,使用Char类型中的 IsLetter静态方法可以判断字符串中是否为字母.我们来实 ...

  2. 使用ptrace向已运行进程中注入.so并执行相关函数

    这个总结的很好,从前一个项目也用到这中技术 转自:http://blog.csdn.net/myarrow/article/details/9630377 1. 简介 使用ptrace向已运行进程中注 ...

  3. 使用edgesForExtendedLayout遇到的麻烦

    今天在写一个多界面之间来回返回的工程时,遇到的问题,建了两个类:FirstViewController 和 ButtonViewController. 由 FirstViewController 进入 ...

  4. CSS样式一

    样式 首先明确: HTML标签也有标签的属性,CSS中的样式也称作为属性,而且某些html中的属性与css中的属性同名,并且作用也相同,但是属于不同的技术. 尺寸样式: 几乎所有的标签有可以设置 wi ...

  5. 函数 sort,unique,stable_sort,count_if,谓词

    bool isShorter(const string &s1,const string &s2) { return s1.size() < s2.size(); } bool ...

  6. js中的一些零碎方法

    ajax表单提交: $.post("do_login", f.serializeArray(), function(data) { if (data.status + " ...

  7. 取消定时-CICS

    CICE CA R 做定时的时候最好加上REqID

  8. opencv java api提取图片sift特征

    opencv在2.4.4版本以后添加了对java的最新支持,可以利用java api了.下面就是我利用opencv的java api 提取图片的sift特征. import org.opencv.co ...

  9. TFS遇到TF14446错误的解决方法

    先上图 使用TFS,之前遇到文件被删除直接获取最新版本就行了,今天遇到这个异常:[TF14446: 无法签出“$/****/****/**/Models.pdb”以进行编辑.您的客户端或团队项目配置为 ...

  10. Centos下如何修改Mysql的root密码

    1.用帐号登录mysql mysql –u root 或#mysql –uroot –p 2.改变用户数据库 命令:mysql>use mysql mysql> use mysqlRead ...