UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)
来源:互联网 发布:大学英语教材听力软件 编辑:程序博客网 时间:2024/06/06 21:41
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 and . 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
0122
学英语:
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
- UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)
- [uva-572]Oil Deposits (BFS和DFS两种
- POJ 1562 Oil Deposits (HDU 1241 ZOJ 1562) DFS
- POJ 1562 && HDU 1241 Oil Deposits(dfs)
- UVa 572 Oil Deposits(DFS)
- UVa 572 Oil Deposits(DFS)
- Uva-572 - Oil Deposits(DFS)
- UVA 572Oil Deposits(DFS)
- POJ 1562 Oil Deposits(dfs)
- poj 1562 Oil Deposits(dfs)
- <DFS> POJ 1562 Oil Deposits
- hdu 1241 Oil Deposits (dfs)
- HDU-1241 Oil Deposits (DFS)
- HDU 1241 Oil Deposits (DFS)
- 【DFS】hdu:1241 Oil Deposits
- hdu 1241dfs Oil Deposits
- hdu 1241 Oil Deposits(DFS)
- HDU--1241:Oil Deposits (DFS)
- php将文字写在图片上,图片加水印
- opengl+vs2010+win7环境搭建
- Android地图开发之OpenStreetMap基础教程
- Linux平台基于S3C2440的MPlayer的移植
- Run-Time Check Failure #2 - Stack around the variable 'dlg1' was corrupted.
- UVa 572/POJ 1562/HDU 1241 Oil Deposits(DFS,两种写法)
- 用友 NC客户化开发手册
- 快速安装PHP-Mysql
- 常用查询入口[保持更新ing]{请善待Ctrl+F}
- JSONP跨域访问
- T-SQL循环语句
- 博客开通
- 初学者如何查阅自然语言处理(NLP)领域学术资料
- android sdcard创建使用