油田问题(L - 暴力求解、DFS)

Description

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.      

Input

The input contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.        

Output

are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.      

Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

Sample Output

0
1
2
2 题目大意:
@代表有油田,*代表没有油田。相邻的两个@代表一个油田,输入一个二维数组找出这个数组里共有多少油田? 分析:
1.这是一个典型的八皇后问题,需要从8个方向遍历搜索
2.DFS(深度优先搜索)、递归方法
3.找到一个@后,从8个方向遍历,并记录sum++ 代码:
 #include <cstdio>
#include <iostream>
using namespace std; char map[][];
int n,m,sum; void dfs(int i,int j)
{
if(map[i][j]!='@'||i<||j<||i>=m||j>=n) //不是油田或是边界
return;
else //从8个方向搜索
{
map[i][j]='!';
dfs(i-,j-);
dfs(i-,j);
dfs(i-,j+);
dfs(i,j-);
dfs(i,j+);
dfs(i+,j-);
dfs(i+,j);
dfs(i+,j+);
}
} int main()
{
int i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==||n==)
break;
sum=;
for(i=;i<m;i++)
for(j=;j<n;j++)
cin>>map[i][j];
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
if(map[i][j]=='@') //以map[i][j]=='@'为中心
{
dfs(i,j);
sum++;
}
}
}
printf("%d\n",sum);
} return ;
}
这道题很明显和8皇后问题类似,所以解起来还很简单。

POJ 1562(L - 暴力求解、DFS)的更多相关文章

  1. CodeForces 339C Xenia and Weights(暴力求解DFS)

    题意:给定 1-10的某几种砝码,给定的每种有无穷多个,然后放 m 个在天平上,要满足,相邻的两次放的砝码不能是同一种,然后是在天平两端轮流放,并且放在哪一个托盘上,那么天平必须是往哪边偏. 析:这个 ...

  2. Program L 暴力求解

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

  3. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

  4. POJ 1321-棋盘问题(DFS 递归)

    POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  5. POJ - 2528 Mayor's posters(dfs+分治)

    POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...

  6. 逆向暴力求解 538.D Weird Chess

    11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...

  7. 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型

    先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...

  8. BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~

    jrMz and angle       Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Other ...

  9. Poj 2187 凸包模板求解

    Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...

随机推荐

  1. BZOJ 1083: [SCOI2005]繁忙的都市(MST)

    裸的最小生成树..直接跑就行了 ---------------------------------------------------------------------- #include<c ...

  2. Spring学习之注入方式

    我们知道,Spring对象属性的注入方式有两种:设值注入和构造注入. 假设有个类为People,该对象包含三个属性,name和school还有age,这些属性都有各自的setter和getter方法, ...

  3. Laravel 单元测试

    前言 今天是第十三周周一,虽然接下来的时间会比较忙,比如各科的课设.考试.磨锤子.但是还是有种涅槃重生的感觉,昨晚的睡眠确实不怎么样,但是今天十分精神,已经想不起来多久没有这么早起了~让我累并快乐着吧 ...

  4. A Byte of Python 笔记(11)异常:try..except、try..finally

    第13章 异常 当你的程序中出现某些 异常的 状况的时候,异常就发生了. 错误 假如我们把 print 误拼为 Print,注意大写,这样 Python 会 引发 一个语法错误. 有一个SyntaxE ...

  5. js数组对象常用方法小结

    ~~~数组添加元素后一般返回数组的新长度 如: push(ele1[,ele2...]), unshift(ele1[,ele2...]) ~~~数组删除元素后一般返回被删除的元素 如: pop() ...

  6. 关于GROUP BY的应用

    前面收藏了别人的SQL语句操作,可是没有实战,也未知学的如何 正好今天有个事需要做一下 (sql server 2000) 三个表:stuInf,sType,sinInf分别为学生信息表,类型表,信息 ...

  7. 用javap命令反编译来分析字符串问题

    编写Test.java.编译完后生成Test.class文件,然后对该文件运行javap -c Test命令,生成字节码指令.阅读并得出结论 一.s1和s2指向常量池的不同常量 ①java代码 pub ...

  8. VCS引起的oracle数据库异常重新启动一例

    1. 环境描写叙述 操作系统版本号:SUSE Linux Enterprise Server 10 sp2 (x86_64) 数据库版本号:Oracle 11.1.0.7.16 VCS版本号:5.1 ...

  9. OutLook 2010 收件箱子文件夹收到新邮件时没有桌面通知

    开始---规则----管理规则和通知 规则和通知---电子邮件规则---批量选择账号---更改规则---在新邮件通知和窗口显示(选中)---确定 录入通知邮件消息---确定 效果如下:

  10. 第一篇:GCD多线程的概念

    1.什么叫GCD? 简单来说就是:Grand Central Dispatch的简称,中文翻译就是:”牛逼的中枢调度器“ 这是纯C语言,还提供了非常多强大的函数 2.GCD的相对优势: (1)GCD是 ...