题目链接

http://poj.org/problem?id=1562

题意

输入一个m行n列的棋盘,棋盘上每个位置为'*'或者'@',求'@'的连通块有几个(连通为8连通,即上下左右,两条对角线)。

思路

floodfill问题,用dfs解决。

代码

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int N = ;
char grid[N][N];
int visit[N][N];
int dir[][] = { //八个邻接点
{, }, {-, }, {, }, {, -},
{-, -}, {-, }, {, }, {, -}
};
int m, n; void dfs(int r, int c)
{
for(int i=; i<; i++)
{
int nr = r+dir[i][];
int nc = c+dir[i][]; if(nr>= && nr<m && nc>= && nc<n && grid[nr][nc]=='@' && !visit[nr][nc])
{
visit[nr][nc] = ;
dfs(nr, nc);
}
}
} int main()
{
//freopen("poj1562.txt", "r", stdin);
while(cin>>m>>n && m)
{
for(int i=; i<m; i++)
cin>>grid[i]; memset(visit, , sizeof(visit));
int cnt = ;
for(int i=; i<m; i++)
{
for(int j=; j<n; j++)
{
if(grid[i][j]=='@' && !visit[i][j])
{
visit[i][j] = ;
dfs(i, j);
cnt++;
}
}
}
cout<<cnt<<endl;
}
return ;
}

poj1562 Oil Deposits(DFS)的更多相关文章

  1. hdu 1241:Oil Deposits(DFS)

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

  2. HDU 1241 Oil Deposits (DFS)

    题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...

  3. UVA - 572 Oil Deposits(dfs)

    题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...

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

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

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

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

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

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

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

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

  8. Oil Deposits(DFS连通图)

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

  9. Oil Deposits(油田)(DFS)

    题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...

随机推荐

  1. tp查询中2个表格中字段,比较大小

    $where['_string'] = '`has_number` < `number`';//~~~注意:这里`不能丢了: $coupon_flag = $coupon->where($ ...

  2. select、poll和epoll多路I/O复用

    一.三者的区别 select  select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select()返回后,该数组中就绪的文件描述符便会被 ...

  3. 「CSS」文本编排相关的CSS属性设置

    1.font-family:设置字体族. 格式为font-family:字体1,字体2,……,通用字体族|inherit. 通用字体族,是指一类相似的字体.W3C的CSS规则规定,要指定一个通用字体族 ...

  4. Debian9(8)下python3和python2同时安装如何使用pip

    在bash下Python会调用python2.x python3会调用python3.x 如果使用pip install命令安装模块会安装python2的模块. 而python3的pip命令使用的是p ...

  5. HDU 4978 计算 凸包

    有以无限间隔$D$的水平线分割的平面,在上面随机投下一个圆,圆中有一些点,点之间两两成一条线段,问随机投下至少有一条线段于平行线相交的概率. 以下是不严(luan)谨(lai)的思路. 首先都知道对于 ...

  6. MDI窗体简单方法(调用,闪屏)

    调用方式: RibbonForm mdishow = new RibbonForm(); //实例化 midshow.MdiParent = this; //设置在主窗体,以MDI的方式显示,关键属性 ...

  7. 微信小程序rpx单位

    rpx单位是微信小程序中css的尺寸单位,rpx可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则750rpx = 375p ...

  8. Ajax+innerHTML+Dgls=好的用户体验+高性能+高效率

    为了引入Dgls,我们从创建Dom节点说起. 用JS创建Dom节点 var div = document.createElement('div'); div.className = 'gdls'; v ...

  9. hdu 1498 50 years, 50 colors(二分匹配_匈牙利算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1498 50 years, 50 colors Time Limit: 2000/1000 MS (Ja ...

  10. FastStoneCapture(FSCapture)录屏、剪辑教程

    FastStoneCapture软件编辑视频的使用方法: http://www.tudou.com/programs/view/2eD-s5HP1xw/