UVA - 572_Oil Deposits(FloodFill)

要求:计算二维中连通块个数。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100 + 3;
const int maxm = 100 + 3;
int m, n;
char buf[maxm][maxn];
bool book[maxm][maxn];

void floodfill(int r, int c)
{
    book[r][c] = true;
    for(int dr = -1; dr < 2; dr++){
        for(int dc = -1; dc < 2; dc++){
            int rr = r + dr, cc = c + dc;
            if((rr < m && rr >= 0 && cc < n && cc >= 0) && buf[rr][cc] == ‘@‘ && !book[rr][cc]){
                floodfill(rr, cc);
            }
        }
    }
}

int main()
{
    while(cin >> m >> n && m && n){
        memset(book, 0, sizeof(book));
        for(int i = 0; i < m; i++){
            scanf("%s", buf[i]);
        }
        int cnt = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(buf[i][j] == ‘@‘ && !book[i][j]){
                    floodfill(i, j);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }
}

收获:

递归框架可以有两种类型:

  1. 显式写明递归边界,即递归中的返回条件。

  2. 限定进入递归的条件,而省掉返回条件。

原文地址:https://www.cnblogs.com/sanshi-2018/p/10411493.html

时间: 2024-07-30 07:28:20

UVA - 572_Oil Deposits(FloodFill)的相关文章

UVA Oil Deposits (BFS)

 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.

UVA - 10534Wavio Sequence(LIS)

题目:UVA - 10534Wavio Sequence(LIS) 题目大意:给出N个数字,找出这样的序列:2 * n + 1个数字组成.前面的n + 1个数字单调递增,后面n + 1单调递减. 解题思路:从前往后找一遍LIS,再从后往前找一遍LIS.最后只要i这个位置的LIS的长度和LDS的长度取最小值.再*2 - 1就是这个波浪数字的长度.注意这里的求LIS要用nlog(n)的算法,而且这里的波浪数字的对称并不是要求i的LIS == LDS,而是只要求LIS和LDS最短的长度就行了,长的那个

uva 725 Division(除法)暴力法!

uva 725  Division(除法) A - 暴力求解 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that t

Uva 11889 - Benefit( 数论 )

Uva 11889 - Benefit( 数论 ) 题意: calculate the lowest integer B such that LCM(A, B) = C 分析: LCM(A,B) = C = A*B/GCD(A,B)C*GCD(A,B) = A*BC/A = B/GCD(A,B)如果C%A != 0 无解否则, 令t = C/AB = t * GCD(A,B) 即B 一定是 t 的整数倍从t开始枚举B #include <cstdio> typedef long long LL

UVA 10139 Factovisors(数论)

Factovisors The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n > 0) We say that a divides b if there exists an integer k such that k*a = b The input to your program consists of several lines, each conta

UVA 1371 - Period(DP)

6.4 一些说明 数据属性可以重写同名的方法属性.这是为了避免在大型系统中产生问题的意外名称冲突.所以用一些减少冲突的常用方法是很有效果的.常用的方法包括:大写字母方法名称,用唯一的字符串来做为数据属性的名称(可以是个下划线_)或者用动词命名方法和用名字命名数据属性. 数据属性就像和对象的普通用户一样可以被方法引用.换句话说,类不能用来实现纯净的数据类型.事实上,在python中不能强制数据隐藏,一切基于约定.(另一方面,如C中写的,python的实现可以做到完全隐藏实现细节并且在必要是可以控制

POJ 1562-Oil Deposits(BFS)

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 6888 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular r

UVa 1391 Astronauts (2SAT)

题意:给出一些宇航员他们的年龄,x是他们的平均年龄,其中A任务只能给年龄大于等于x的人,B任务只能给小于x的人,C任务没有限制.再给出m对人,他们不能同任务.现在要你输出一组符合要求的任务安排. 思路:2SAT. 设Ai表示第i个人的任务,如果i的年龄大于等于x,那么Ai=true表示分到A任务,flase表示分到C任务.如果i年龄小于x则Ai=true表示分到B任务,flase表示分到C任务. 考虑对于这m对里的每对人,如果他们是同组的,那么(Ai并非Aj)或(非Ai并Aj)等价于 (非Ai或

UVA - 111History Grading(LIS)

题目:UVA - 111History Grading(LIS) 题目大意:找最长的LIS.但是题意讲的实在是有问题. 例如:3 1 2 4 9 5 10 6 8 7,意思是第一个历史事件的时间是排在第三位,第二个历史事件是在第1位,那么首先先要将这个事件按照时间顺序重新排序.新的排列顺序:2 3 1 4 6 8 10 9 5 7. 解题思路:LIS. 代码: #include <cstdio> #include <cstring> #include <algorithm&g