POJ 1562(L - 暴力求解、DFS)

油田问题(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++

代码:

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4
 5 char map[101][101];
 6 int n,m,sum;
 7
 8 void dfs(int i,int j)
 9 {
10     if(map[i][j]!=‘@‘||i<0||j<0||i>=m||j>=n) //不是油田或是边界
11         return;
12     else          //从8个方向搜索
13     {
14         map[i][j]=‘!‘;
15         dfs(i-1,j-1);
16         dfs(i-1,j);
17         dfs(i-1,j+1);
18         dfs(i,j-1);
19         dfs(i,j+1);
20         dfs(i+1,j-1);
21         dfs(i+1,j);
22         dfs(i+1,j+1);
23     }
24 }
25
26 int main()
27 {
28     int i,j;
29     while(scanf("%d%d",&m,&n)!=EOF)
30     {
31         if(m==0||n==0)
32             break;
33         sum=0;
34         for(i=0;i<m;i++)
35             for(j=0;j<n;j++)
36                 cin>>map[i][j];
37         for(i=0;i<m;i++)
38         {
39             for(j=0;j<n;j++)
40             {
41                 if(map[i][j]==‘@‘) //以map[i][j]==‘@‘为中心
42                 {
43                     dfs(i,j);
44                     sum++;
45                 }
46             }
47         }
48         printf("%d\n",sum);
49     }
50
51     return 0;
52 }  

这道题很明显和8皇后问题类似,所以解起来还很简单。
				
时间: 2024-08-04 15:20:18

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

Program L 暴力求解

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

3thweek——E.暴力求解+DFS

Description Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are calledequivalent in one of the two cases: They are equal. If we split string a into two halves of the same siz

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4

poj 1724ROADS(bfs和dfs做法)

1 /* 2 dfs比较好想,就是测试数据的问题,导致在遍历边的时候要倒着遍历才过! 3 */ 4 #include<iostream> 5 #include<cstdio> 6 #include<cstring> 7 #include<vector> 8 #include<algorithm> 9 #define Max 0x3f3f3f3f 10 using namespace std; 11 12 struct node{ 13 int D

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

POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行是两个正整数,n k,用

POJ 3321 Apple Tree (dfs+线段树)

题目大意: 修改树上的节点,然后求子树的和. 思路分析: dfs 重新编号,烂大街了... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 100005 #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e using namespace std

POJ 2029 DP || 暴力

在大矩形中找一个小矩形 使小矩形包含的*最多 暴力或者DP  水题 暴力: #include "stdio.h" #include "string.h" int main() { int n,m,w,i,s,t,j,k,l,ans,sum,x,y; int map[101][101]; while (scanf("%d",&w)!=EOF) { if(w==0) break; scanf("%d%d",&n,&

第四章 分治策略 4.1 最大子数组问题 (暴力求解算法)

/** * 最大子数组的暴力求解算法,复杂度为o(n2) * @param n * @return */ static MaxSubarray findMaxSubarraySlower(int[] n) { long tempSum = 0; int left = 0; int right = 0; long sum = Long.MIN_VALUE; for (int i = 0; i < n.length; i++) { for (int j = i; j < n.length; j++

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string