kuangbin 简单搜索 L题
来源:互联网 发布:淘宝在国外的影响力 编辑:程序博客网 时间:2024/05/18 21:49
L - 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 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 <= 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
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
题解:
经典的种子填充。借鉴了挑战程序设计P33。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 100+10;int n,m;char field[maxn][maxn];void dfs(int x,int y){ field[x][y]='.'; for(int dx=-1;dx<=1;dx++) for(int dy=-1;dy<=1;dy++) { int nx = x+dx;int ny = y+dy; if(0<=nx&&nx<n&&0<=ny&&ny<m&&field[nx][ny]=='@') dfs(nx,ny); }}void solve(){ int res=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(field[i][j]=='@') { dfs(i,j); res++; } } printf("%d\n",res);}int main(){ while(cin>>n>>m) { if(n==0&&m==0) break; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>field[i][j]; solve(); } return 0;}
- kuangbin 简单搜索 L题
- [kuangbin带你飞]专题一 简单搜索L
- [kuangbin带你飞]专题一 简单搜索 L
- kuangbin带你飞:专题一 简单搜索 L
- kuangbin 简单搜索C题
- kuangbin 简单搜索 F题
- kuangbin 简单搜索 H题
- kuangbin [简单搜索专题]
- kuangbin 简单搜索 B
- kuangbin 简单搜索 E
- kuangbin 简单搜索 I
- [kuangbin带你飞]专题一 简单搜索L - Oil Deposits(HDU 1241)
- [kuangbin带你飞]专题一 简单搜索 L - Oil Deposits HDU1241
- [kuangbin带你飞]专题一 简单搜索 L HDU 1241
- kuangbin求带飞简单搜索 棋盘问题
- kuangbin 简单搜索 A 棋盘问题
- kuangbin 简单搜索 J 双bfs
- kuangbin专题一简单搜索总结
- Python基础教程第五章学习笔记——条件、循环和其他语句
- 嘻嘻嘻
- [数论 && 二次剩余 && BSGS] Codechef FN. FIBNACCI NUMBER
- Java的内存分配
- 黑客入门
- kuangbin 简单搜索 L题
- 51NOD1495 中国好区间 【尺取法】
- iOS同步后couldn't load project
- object的常用方法及作用
- XMLHttpRequest 对象使用案例
- 虚拟机小问题:克隆后不兼容、ifconfig找不到eth0、虚拟内存不足
- 《Java从入门到放弃》JavaSE入门篇:面向对象语法一(入门版)
- logback使用(springboot 和springmvc)
- Unique Paths