UVa572 Oil Deposits DFS求连通块
技巧:遍历8个方向
for(int dr = -1; dr <= 1; dr++)
for(int dc = -1; dc <= 1; dc++)
if(dr != 0 || dc != 0) dfs(r+dr, c+dc, id);
我的解法:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N=102;
char buf[N][N];
int m, n;
int cnt;
int dr[]={0, 0, 1, 1, 1, -1, -1, -1};
int dc[]={1, -1, -1, 0, 1, -1, 0, 1}; void dfs(int r, int c)
{
buf[r][c]='*';
for(int i=0;i<8;i++)
{
if(buf[r+dr[i]][c+dc[i]]=='@')
dfs(r+dr[i], c+dc[i]);
} } int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva572.in", "r", stdin);
#endif
while(cin>>m>>n && m)
{
memset(buf, '*', sizeof buf);
cnt=0;
for(int i=1;i<=m;i++)
{
cin>>(buf[i]+1);
} for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(buf[i][j]=='@')
{
dfs(i, j);
cnt++;
}
}
}
cout<<cnt<<endl;
} return 0;
}
lrj的解法在统计的时候把连通块的序号也求出来了,保存在idx中。
// UVa572 Oil Deposits
// Rujia Liu
// 题意:输入一个字符矩阵,统计字符@组成多少个四连块
#include<cstdio>
#include<cstring>
const int maxn = 100 + 5; char pic[maxn][maxn];
int m, n, idx[maxn][maxn]; void dfs(int r, int c, int id) {
if(r < 0 || r >= m || c < 0 || c >= n) return;
if(idx[r][c] > 0 || pic[r][c] != '@') return;
idx[r][c] = id;
for(int dr = -1; dr <= 1; dr++)
for(int dc = -1; dc <= 1; dc++)
if(dr != 0 || dc != 0) dfs(r+dr, c+dc, id);
} int main() {
while(scanf("%d%d", &m, &n) == 2 && m && n) {
for(int i = 0; i < m; i++) scanf("%s", pic[i]);
memset(idx, 0, sizeof(idx));
int cnt = 0;
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
if(idx[i][j] == 0 && pic[i][j] == '@') dfs(i, j, ++cnt);
printf("%d\n", cnt);
}
return 0;
}
UVa572 Oil Deposits DFS求连通块的更多相关文章
- HDU1241 Oil Deposits —— DFS求连通块
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
- [C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
- UVA 572 dfs求连通块
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- 用DFS求连通块(种子填充)
[问题] 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横.竖或者对角线方向),就说它们属于同一个八连块.例如,图6-9中有两个八连块. 图6-9 [分 ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- DFS:POJ1562-Oil Deposits(求连通块个数)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Description The GeoSurvComp geologic survey com ...
随机推荐
- LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)
题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...
- 【C#学习笔记】LinkedList容器使用
using System; using System.Collections.Generic; namespace ConsoleApplication { class Program { stati ...
- Android设计模式之命令模式、策略模式、模板方法模式
命令模式是其它很多行为型模式的基础模式.策略模式是命令模式的一个特例,而策略模式又和模板方法模式都是算法替换的实现,只不过替换的方式不同.下面来谈谈这三个模式. 命令模式 将一个请求封装为一个对象,从 ...
- Android-AnimationDrawable(二)
首先可以先定义一个逐帧播放的xml: <?xml version="1.0" encoding="utf-8"?> <animation-li ...
- linux 下 NetBeans 字体大小设置
在linux mint 12下安装了 NetBeans7.1.2使用之后,觉得字体不好看,字体普遍特别大,分三个方面改NetBeans的字体. 1. 代码字体大小 点击NetBeans菜单,工具--& ...
- 滑动菜单栏开源项目SlidingMenu的使用
一.SlidingMenu简介相信大家对SlidingMenu都不陌生了,它是一种比较新的设置界面或配置界面的效果,在主界面左滑或者右滑出现设置界面效果,能方便的进行各种操作.很多优秀的应用都采用了这 ...
- webdriver(python)学习笔记二
自己开始一个脚本开始学习: # coding = utf-8 from selenium import webdriver browser = webdriver.Firefox() browser. ...
- IOS 支付宝 SDK 申请
https://b.alipay.com/order/productDetail.htm?productId=2013080604609654&tabId=4#ps-tabinfo-hash
- 在eclipse.ini中指定jdk的方式
在eclisep的安装目录,打开eclipse.ini文件,加上这么一行,如下红色所示,注意加在-Vmargs前面,这两种方式的区别是:第二种方式除了会有eclipse进程外还会启动个java进程. ...
- bzoj 2594 [Wc2006]水管局长数据加强版(LCT+最小生成树)
[深坑勿入] [给个链接] http://blog.csdn.net/popoqqq/article/details/41348549 #include<cstdio> #include& ...