油田问题(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. Git Version recovery command introduction - git reset

    reset命令有3种方式: git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息 git re ...

  2. chrome dev tools

    chrome dev tools介绍一下Chrome dev tools 的基本使用和一些意想不到的小技巧.\\Chrome Developer Tools 是Chrome内嵌的一系列编辑和调试的工具 ...

  3. ftp一些东东

    ftp如果绑定域名 登录时登录名应该写成 域名|用户名

  4. hdoj 3549 Flow Problem(最大网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 思路分析:该问题为裸的最大网络流问题,数据量不大,使用EdmondsKarp算法求解即可:需要注 ...

  5. JS动态增加页面上的控件实例

    <input   type="button"   value="继续添加"   onclick="append();"/ > & ...

  6. SPOJ1811最长公共子串问题(后缀自动机)

    题目:http://www.spoj.com/problems/LCS/ 题意:给两个串A和B,求这两个串的最长公共子串. 分析:其实本题用后缀数组的DC3已经能很好的解决,这里我们来说说利用后缀自动 ...

  7. Android学习资料PDF免费大放送,每日更新!有需要的亲看这里

    说明:以前有资料分享的时候,都叫大家留下自己的QQ邮箱,这样被很多的程序猿们认为我是专门来钓你们的QQ邮箱的.为此给大家带来的顾虑,深表抱歉.从现在开始,以后的分享资料我都会放在自己的百度网盘中,提供 ...

  8. mahout贝叶斯算法开发思路(拓展篇)2

    如果想直接下面算法调用包,可以直接在mahout贝叶斯算法拓展下载,该算法调用的方式如下: $HADOOP_HOME/bin hadoop jar mahout.jar mahout.fansy.ba ...

  9. Compare the value of entity field.

    public class Program { static void Main(string[] args) { Program p = new Program(); p.Test(); Consol ...

  10. discuz bbs注册,登录流程整理!想打通bbs又不想读一遍代码可以参考一下

    bbs 用户注册流程 第一步: /source/class/class_member.php: on_register注册入口 L602 左右 if(!$activation) {//不为空,说明用户 ...