UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)

2024-03-05 21:08

本文主要是介绍UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

572 - Oil Deposits

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=513

http://poj.org/problem?id=1562

http://acm.hdu.edu.cn/showproblem.php?pid=1241

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 file 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 \le m \le 100$  and  $1 \le n \le 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 

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they 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

学英语:

Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally.

两个不同的油囊归属于相同的油田iff它们是水平、垂直或斜对地相邻的。


思路:

简单的dfs模板题。


完整代码:

递归:

/*UVaOJ: 0.015s*/
/*POJ: 16ms,164KB*/
/*HDU: 15ms,348KB*/#include <cstdio>
#include <cstring>
using namespace std;int m, n;
bool grid[102][102], vis[102][102];void dfs(int x, int y)
{if (!grid[x][y] || vis[x][y])return;vis[x][y] = true;dfs(x - 1, y - 1);dfs(x - 1, y);dfs(x - 1, y + 1);dfs(x, y - 1);dfs(x, y + 1);dfs(x + 1, y - 1);dfs(x + 1, y);dfs(x + 1, y + 1);
}int main()
{char temp;while (~scanf("%d%d", &m, &n)){if (!m)break;memset(grid, 0, sizeof(grid));memset(vis, 0, sizeof(vis));//下面这个专门对付蛋疼的poj样例,你懂的。while (getchar() == ' ');for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++){temp = getchar();if (temp == '@')grid[i][j] = true;}getchar();}int count = 0;for (int i = 1; i <= m; i++)for (int j = 1; j <= n; j++){if (grid[i][j] && !vis[i][j]){count++;dfs(i, j);}}printf("%d\n", count);}return 0;
}

非递归:(占内存小点)

/*UVaOJ: 0.015s*/
/*POJ: 0ms,160KB*/
/*HDU: 15ms,276KB*/#include <cstdio>
#include <cstring>
using namespace std;struct data
{int x, y;
} queue[20000], now, next;int in, out;
int mov[8][2] = {{ -1, 0}, { -1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, { -1, 1}};
char field[102][102];
int m, n;
int ans;
int k;void bfs(int i, int j)
{in = out = 0;queue[in].x = i;queue[in].y = j;field[i][j] = '*';in++;while (in != out){now = queue[out++];for (k = 0; k < 8; k++){next.x = now.x + mov[k][0];next.y = now.y + mov[k][1];if (field[next.x][next.y] == '@'){field[next.x][next.y] = '*';queue[in++] = next;}}}
}int main()
{int i, j;while (scanf("%d%d", &m, &n), m)//逗号表达式的值是最后一个表达式的值{ans = 0;memset(field, '*', sizeof(field));while (getchar() == ' ');for (i = 1; i <= m; i++)scanf("%s", field[i] + 1);for (i = 1; i <= m; i++)for (j = 1; j <= n; j++)if (field[i][j] == '@'){bfs(i, j);ans++;}printf("%d\n", ans);}
}

Source

Mid-Central USA 1997

这篇关于UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/777808

相关文章

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

Python实现阶乘的四种写法

《Python实现阶乘的四种写法》本文主要介绍了Python实现阶乘的六种写法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录第一种:推导式+循环遍历列表内每个元素相乘第二种:调用functools模块reduce的php累计

MySQL中删除重复数据SQL的三种写法

《MySQL中删除重复数据SQL的三种写法》:本文主要介绍MySQL中删除重复数据SQL的三种写法,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下... 目录方法一:使用 left join + 子查询删除重复数据(推荐)方法二:创建临时表(需分多步执行,逻辑清晰,但会

Python读取TIF文件的两种方法实现

《Python读取TIF文件的两种方法实现》本文主要介绍了Python读取TIF文件的两种方法实现,包括使用tifffile库和Pillow库逐帧读取TIFF文件,具有一定的参考价值,感兴趣的可以了解... 目录方法 1:使用 tifffile 逐帧读取安装 tifffile:逐帧读取代码:方法 2:使用

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M