Oil Deposits HDU - 1241 (简单bfs)(找有多少个连通块)

题目链接:https://cn.vjudge.net/problem/HDU-1241

注意:搜索八个方向

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <stack>
 6 #include <algorithm>
 7 #include <cmath>
 8 #include <map>
 9 #define mem(a,b) memset(a,b,sizeof(a));
10 using namespace std;
11 #define INF 0x3f3f3f3f
12 typedef long long ll;
13 int dir[8][2] = {0,1,0,-1,1,0,-1,0,1,-1,1,1,-1,1,-1,-1};
14 const int maxn = 5000005;
15 int r,c,vis[105][105];
16 string s[105];
17 struct node{
18     int x,y;
19     node(int x1,int y1):x(x1),y(y1){};
20 };
21 void bfs(int x,int y) {
22     queue<node>q;
23     vis[x][y] = 1;
24     q.push(node(x,y));
25     while(!q.empty()) {
26         node temp = q.front();
27         q.pop();
28         for(int i = 0; i < 8; i++) {
29             int fx = temp.x + dir[i][0],fy = temp.y + dir[i][1];
30             if(fx >=0 && fx < r && fy >=0 && fy < c && !vis[fx][fy] && s[fx][fy] == ‘@‘)
31             {
32                 vis[fx][fy] = 1;
33                 q.push(node(fx,fy));
34             }
35         }
36     }
37 }
38 int main()
39 {
40     while(cin >> r >> c && r &&  c) {
41         mem(vis,0);
42         for(int i = 0; i < r; i++) {
43             cin >> s[i];
44         }
45         int ans = 0;
46         for(int i = 0; i < r; i++) {
47             for(int j = 0; j < c; j++) {
48                 if(s[i][j] == ‘@‘ && !vis[i][j]) {
49                     bfs(i,j);
50                     ans++;
51                 }
52             }
53         }
54         cout << ans << endl;
55     }
56     return 0;
57 }

原文地址:https://www.cnblogs.com/LLLAIH/p/11376865.html

时间: 2024-10-12 18:32:38

Oil Deposits HDU - 1241 (简单bfs)(找有多少个连通块)的相关文章

(搜索)Oil Deposits -- hdu -- 1241

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18758    Accepted Submission(s): 10806 Problem Description The GeoSurvComp geologic survey comp

Oil Deposits DFS FloodFill漫水填充法求连通块问题

Oil Deposits 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

hdu 1241 Oil Deposits(八方向简单深搜,对新手挺经典)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12461 Accepted Submission(s): 7245 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground

Oil Deposits HDU杭电1241

Problem 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 pl

Oil Deposits (poj 1241) 搜索 (深度优先搜素)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8274 Accepted Submission(s): 4860 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground

hdu 1548 简单bfs。。。

由于题目过水.. 我就在这里把bfs的模板写一些吧.. bfs的思想是利用队列的特性 对树的每一层先遍历 每一次访问时取出队首 然后排出~ #include<queue>void bfs(int start){ int t;//保存队首的元素 queue<int>q;//定义一个名字叫q的队列 q.push(start);//树根入队  while(!q.empty())//当队列里面还有元素  即还没有搜索结束  {  t=q.front();  int next;  if() 

hdu 4355 简单三分找最小值

#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> using namespace std; #define exp 1e-6 int n; double pos[50010],weight[50010]; double min(double a,double b) { return a<b?a:b; } double query(double x) { in

HDU 搜索练习 Oil Deposits

Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 61 Accepted Submission(s) : 25 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground oil

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M