【POJ - 1562】Oil Deposits (dfs搜索,连通块问题)

题干:

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

解题报告:

     注意这题在写的时候,dfs中一定要先maze[][]='*',而不能后序赋值, 这样就会进入死循环,第二个样例就可以很明显的看出来.

AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,m;
char maze[105][105];
int nx[8] = {0,1,1,1,0,-1,-1,-1};
int ny[8] = {1,1,0,-1,-1,-1,0,1};
void dfs(int x,int y) {if(x < 1 ||x > n || y <1 || y > m) return ;
//	if(maze[x][y] == '*') return ;for(int k = 0; k<8; k++) {int tx = x+nx[k];int ty = y+ny[k];if(maze[tx][ty] == '@') {maze[tx][ty] = '*';dfs(tx,ty);}}
//	maze[x][y] = '*';}
int main()
{while(cin>>n>>m) {if(n == 0 && m == 0) break;int cnt = 0;for(int i = 1; i<=n; i++) {scanf("%s",maze[i] + 1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == '@') {maze[i][j] = '*';dfs(i,j);cnt++;}}}cout << cnt <<endl;}return 0 ;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441728.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python列表浅复制_Python列表深浅复制详解

转自&#xff1a;https://www.cnblogs.com/blaomao/p/7239203.html在文章《Python 数据类型》里边介绍了列表的用法&#xff0c;其中列表有个 copy() 方法&#xff0c;意思是复制一个相同的列表。例如1 names ["小明", "小红", "小黑", "小…

【HYSBZ - 1192】鬼谷子的钱袋(水题,二进制)

题干&#xff1a; 鬼谷子非常聪明&#xff0c;正因为这样&#xff0c;他非常繁忙&#xff0c;经常有各诸侯车的特派员前来向他咨询时政。有一天&#xff0c;他在咸阳游历的时候&#xff0c;朋友告诉他在咸阳最大的拍卖行&#xff08;聚宝商行&#xff09;将要举行一场拍卖会&a…

【CodeForces - 735B】Urbanization (找规律,思维)

题干&#xff1a; Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are n people who plan to move to the cities. The wealth of the i of them is equal to a…

java 文本查找_Java基于正则表达式实现查找匹配的文本功能【经典实例】

本文实例讲述了Java基于正则表达式实现查找匹配的文本功能。分享给大家供大家参考&#xff0c;具体如下&#xff1a;REMatch.java&#xff1a;package reMatch;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** Created by Frank*/public class REMatch {p…

【51nod - 1073】约瑟夫环问题模板

题干&#xff1a; N个人坐成一个圆环&#xff08;编号为1 - N&#xff09;&#xff0c;从第1个人开始报数&#xff0c;数到K的人出列&#xff0c;后面的人重新从1开始报数。问最后剩下的人的编号。 例如&#xff1a;N 3&#xff0c;K 2。2号先出列&#xff0c;然后是1号&am…

java oracle database user dsn_跨会话序列化数据库连接

我正在开发一个web应用程序&#xff0c;它需要使用最终用户提供的凭据登录到数据库&#xff1b;应用程序本身没有登录到数据库。在问题是如何为每个用户会话创建一个连接。在一种方法是&#xff1a;请求用户凭据检查针对数据库后端的凭据是否有效在会话级变量中存储凭据这种方法…

【POJ - 3125 】Printer Queue(模拟,队列+优先队列,STL)

题干&#xff1a; The only printer in the computer science students union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you may have to wait for hours to get a single page of output. Because some …

java循环单链表类构造函数_C++实现双向循环链表

本文实例为大家分享了C实现双向循环链表的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下一、概念1.在双链表中的每个结点应有两个链接指针&#xff1a;lLink -> 指向前驱结点 (前驱指针或者左链指针)rLink->指向后继结点(后驱指针或者右链指针)2.双链表常采用…

*【CodeForces - 799C】Fountains (线段树 或 树状数组,类似二元组问题)

题干&#xff1a; Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cos…

java 前后的区别_java中前后++的区别

java中前后的区别发布时间&#xff1a;2020-06-22 14:38:22来源&#xff1a;亿速云阅读&#xff1a;134作者&#xff1a;Leah这篇文章将为大家详细讲解有关java中前后的区别&#xff0c;小编觉得挺实用的&#xff0c;因此分享给大家做个参考&#xff0c;希望大家阅读完这篇文章…

【CodeForces - 735A 】Ostap and Grasshopper (水题,模拟)

题干&#xff1a; On the way to Rio de Janeiro Ostap kills time playing with a grasshopper he took with him in a special box. Ostap builds a line of length n such that some cells of this line are empty and some contain obstacles. Then, he places his grassh…

java私有成员的访问_java – 使用私有成员或公共访问器的方法

我意识到这可能无法回答,但我正在寻找是否有关于是否直接使用私有成员或类方法中的公共访问器的某种指导.例如,考虑以下代码(在Java中,但在C中看起来非常相似)&#xff1a;public class Matrix {// Private Membersprivate int[][] e;private int numRows;private int numCols;…

☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)

题干&#xff1a; Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci. Now its time for Timofey birthday, and his mother asked him to re…

java中实现线程互斥的关键词_简单的互斥同步方式——synchronized关键字详解

2. synchronized的原理和实现细节2.1 synchronized可以用在那些地方静态方法,锁对象为当前类的class对象,不用显式指定实例方法,锁对象为当前实例对象,不用显式指定同步代码块,锁对象为括号中指定的对象&#xff0c;必须显式指定被synchronized修饰的方法或者代码块,同一时刻只…

【51nod - 1875】 丢手绢(约瑟夫问题,可打表,用STL模拟)

题干&#xff1a; 六一儿童节到了&#xff0c;小朋友们在玩丢手绢的游戏。总共有C个小朋友&#xff0c;编号从1到C&#xff0c;他们站成一个圈&#xff0c;第i(1<i<C)个人的左边是i-1&#xff0c;第1个人的左边是C。第i(1<i<C)个人的右边是i1&#xff0c;第C个人的…

mysql server远程连接_本地远程连接 MySQL server

问题MySql Server 出于安全方面考虑默认只允许本机(localhost, 127.0.0.1)来连接访问。如果想远程访问&#xff0c;需要额外做下操作。配置修改定位文件/etc/mysql/mysql.conf.d/mysqld.cnf定位属性skip-networking #注释掉 因为它是屏蔽掉一切TCP/IP连接bind-address 127.0.0…

关闭VS警告#pragma warning(disable:4996)

代码实现&#xff1a; #pragma warning(disable:4996) 1. #pragma warning只对当前文件有效&#xff08;对于.h&#xff0c;对包含它的cpp也是有效的&#xff09;&#xff0c;而不是对整个工程的所有文件有效。当该文件编译结束&#xff0c;设置也就失去作用。 2. #pragma wa…

【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)

题干&#xff1a; 题目大意&#xff1a; 题意是在一条直线上坐落着不同位置的灯塔&#xff0c;每一个灯塔有自己的power level&#xff0c;当作是射程范围。现在从最右边的灯塔开始激发&#xff0c;如果左边的灯塔在这个灯塔的范围之内&#xff0c;那么将会被毁灭。否则会被激…

java关闭文本_如何更优雅的关闭java文本、网络等资源

通常在 java 中对文本、网络资源等操作起来是很繁杂的&#xff0c;要声明&#xff0c;读取&#xff0c;关闭三个阶段&#xff0c;还得考虑异常情况。假设我们要读取一段文本显示到控制台&#xff0c;通常会有如下的代码&#xff1a;public static void main(String[] args) {Fi…

java类匿名类实例_Java匿名类,匿名内部类实例分析

本文实例讲述了Java匿名类&#xff0c;匿名内部类。分享给大家供大家参考&#xff0c;具体如下&#xff1a;本文内容&#xff1a;内部类匿名类首发日期 &#xff1a;2018-03-25内部类&#xff1a;在一个类中定义另一个类&#xff0c;这样定义的类称为内部类。【包含内部类的类可…