SSOJ 2316 面积【DFS/Flood Fill】

题目描述

编程计算由“1”号围成的下列图形的面积。面积计算方法是统计1号所围成的闭合曲线中点的数目。

如图所示,在10*10的二维数组中,“1”围住了15个点,因此面积为15.

题目大意:对于给定的10*10的01矩阵,请问有多少个0被1包围了?(包围是指不能由上下左右通向边缘)
本文来源于OIER博客,原文出处:http://www.oier.cc/ssoj2316%E9%9D%A2%E7%A7%AF/

解题思路 图形学中Flood Fill是满水法填充,是用来填充区域的。就好比在一个地方一直到水,水会往四周满延开,直到高地阻挡。Flood Fill就是从一个点开始往四周寻找相同的点填充,直到有不同的点为止。 Flood Fill既可以用深度优先搜索实现,也可以用广度优先搜索实现,由于每个点只会被访问一次,两种算法的时间复杂度差不多。 本题中,要求被1包围的0有多少个,那么我们可以把没被1包围的0全部都填充成1,最后再数一下有多少个1即可。 什么是没被1包围的0?边缘的0都是没被包围的,只有与边缘的0相连就不被包围。因此,我们可以搜索每个边缘的0,并将他以及与他相连的0全部变为1。 小技巧:在读入数据之前,让mp数组各个元素都是非0,这样即使搜索到了mp[0][i]或者mp[i][0],也不会走出地图,因为遇到0才继续走。
本文来源于OIER博客,原文出处:http://www.oier.cc/ssoj2316%E9%9D%A2%E7%A7%AF/

时间: 2024-08-02 22:45:11

SSOJ 2316 面积【DFS/Flood Fill】的相关文章

Openjudge1388 Lake Counting【DFS/Flood Fill】

http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:   65536kB 描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 1

小白书 黑白图像【DFS/Flood Fill】

http://blog.csdn.net/u010470972/article/details/33415617 Description 输入一个n×n的黑白图像(1表示黑色,0表示白色),任务是统计其中八连块的个数.如果两个黑格子有公共边或者公共顶点,就说它们属于同一个八连块.如下图所示的图形有3个八连块. Input 第1行输入一个正整数n(n≤700),此后输入n行,每行是由n个0或1组成的字符串. Output 在输入黑白图像中,八连块的个数 Sample Input 6 100100 

洛谷 P1506 拯救oibh总部【DFS/Flood Fill】

题目背景 oibh总部突然被水淹没了!现在需要你的救援…… 题目描述 oibh被突来的洪水淹没了>.<还好oibh总部有在某些重要的地方起一些围墙,用号表示,而一个封闭的号区域洪水是进不去的……现在给出oibh的围墙建设图,问oibh总部没被淹到的重要区域(由”0”表示)有多少. 输入输出格式 输入格式: 第一行是两个数,x和y(x,y<=500) 第二行及以下是一个由*和0组成的x*y的图. 输出格式: 输出没被水淹没的oibh总部的“0”的数量. 输入输出样例 输入样例#1: 样例输

USACO The Castle(flood fill)

题目请点我 题解: 这道题真的好蛋疼啊,首先题意不好理解,搞了半天复杂的要死,有那么多要求,还要求那么多东西,做到一半都不想做了...感觉没什么技术含量,还做起来死费劲儿.但是强迫症非得按顺序做题啊,最后还是一点点把它给调出来了,说什么flood fill,其实也就是那么回事,没什么算法上的技巧,就是见招拆招的感觉... 题意搞懂再做题,题意,不谢! 第一步:根据他的规则把房间画出来,遍历一遍把每个节点四面的墙给补上: 第二步:深搜: 目的1:记录深搜的次数,房间数: 目的2:记录深搜的深度,最

733. Flood Fill

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor,

733. Flood Fill 简单型染色问题

[抄题]: An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newC

733. Flood Fill - Easy

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor,

洪水填充(Flood fill)算法

从一个起始节点开始把附近与其连通的节点提取出或填充成不同颜色颜色,直到封闭区域内的所有节点都被处理过为止,是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法. 因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名.在GNU Go和扫雷中,Flood Fill算法被用来计算需要被清除的区域. 洪水填充算法接受三个参数:起始节点,目标节点特征和针对提取对象要执行的处理. 目前有许多实现方式,基本上都显式的或隐式的使用了队列或者栈. 洪水填充算法实现最常见有四邻域

[LeetCode] Flood Fill 洪水填充

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor,