油田问题(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. ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'information_schema'

    在我想把备份的数据库导入到本地数据的时候,发生这个错误,我使用的工具是dbForge Studio for MySQL ERROR 1044 (42000): Access denied for us ...

  2. [LeetCode]题解(python):130-Surrounded Regions

    题目来源: https://leetcode.com/problems/surrounded-regions/ 题意分析: 给定给一个二维的板,这个板只包括‘X’和‘O’.将被‘X’包围的‘O’变成‘ ...

  3. IOS 播放音频

    1,播放短音频 #import <AudioToolbox/AudioToolbox.h>#import "GLYViewController.h"static voi ...

  4. Bootstrap dropdown 使用

    同样是2种方式 参考http://www.bootcss.com/javascript.html#dropdowns JS方式调用 http://www.w3resource.com/twitter- ...

  5. 用QComboBox实现tree状结构(QComboBox居然有setView和setModel函数)

    实现的效果图如下:  #include "mainwindow.h" #include <QApplication> #include <QTreeView> ...

  6. MultiView空间例子

    CSS代码: body { font-size:11pt; font-family:宋体; } .mainTitle { font-size:11pt; font-weight:bold; font- ...

  7. cocos2d-x中的尺寸之三

    通过上面两个文章的分析,我们在这个博文里做个总结: CCEGLView::getFrameSize()返回的是窗口相对于屏幕像素的尺寸,这个尺寸,只要窗口没变化,值就不会变化 CCDirector:: ...

  8. jQuery.fn.extend与jQuery.extend 的区别

    1 jquery.extend 是jquery 静态的方法 实例 jQuery.extend({     liu: function(){         alert('liu');     } }) ...

  9. 快速注册Uber司机,兼职月入轻松过万

    Uber是世界领先的即时用车网络平台.当前部分中国城市,Uber司机只需每周完成70单,可获得7000元的激励制度回报司机,月收入近3万元.[加入条件]1. 车辆为本地牌照2. 车龄在5年以内3. 裸 ...

  10. poj1144 Network【tarjan求割点】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4319585.html   ---by 墨染之樱花 [题目链接]http://poj.org/p ...