油田问题(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. SQLite学习手册(目录)

    链接地址:http://www.cnblogs.com/stephen-liu74/archive/2012/01/22/2328757.html 在实际的应用中,SQLite作为目前最为流行的开源嵌 ...

  2. 1.2. chromium源代码分析 - chromiumframe - 入口函数

    ChromiumFrame的入口函数在main.cpp中,打开main.cpp.中包含3个类和_tWinMain函数._tWinMain就是我们要找的入口函数.我做了部分注释: int APIENTR ...

  3. Linux内核学习笔记-2.进程管理

    原创文章,转载请注明:Linux内核学习笔记-2.进程管理) By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...

  4. 射频识别技术漫谈(19)——Desfire的3次握手认证和段密码生成

    3次握手认证并生成临时的通讯密钥在通讯技术中的应用非常普遍,Mifare Desfire也使用了这种成熟的认证加密方法.Desfire在卡片数据传输前使用DES或3DES进行3次握手认证,认证成功一方 ...

  5. Sublime Text 2 - There are no packages available for installation

    解决Sublime Text 2 package Control 无法安装插件的问题 错误提示 here are no packages available for installation 问题解决 ...

  6. Servlet 基础知识

    8.Response  8.1.getOutputStream和getWriter方法分别用于得到输出二进制数据.输出文本数据的ServletOutputStream .Printwriter对象.  ...

  7. LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

    获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.L ...

  8. 84. 从视图索引说Notes数据库(下)

    作用和代价上文介绍了关系型数据库里的索引.Notes数据库里的索引隐藏在视图概念里(本文的讨论仅仅针对Notes的视图索引,不包括全文索引.).开发者创建的视图仅仅是存放在数据库里的一条设计文档.数据 ...

  9. IOS开发新手教程(一)-数据类型和运算符

    OC语法入门(一) 数据类型和运算符 1.1凝视 凝视和其它语言一样,同意单行 ,多行凝视,一份规范的代码里面须要有一些正式的凝视,例如以下凝视: /* 这是多行 凝视 */ //这是多行凝视 OC语 ...

  10. 数据科学家:神话 &amp; 超能力持有者

    一个打破神话的季节,正在降临.        我将坦诚地揭穿人们关于数据科学家所持有的惯有看法.在下文中,我将一个一个展示这些观点,宛如将一个又一个的玻璃瓶子摔碎在墙壁上一样.        关于数据 ...