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

相关文章

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

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D