POJ1979(Red and Black)--FloodFill
题目意思是这样的,一个人起始位置在 '@' 处,他在途中能到达的地方为 ' . ' 而 '#' 是障碍物,他不能到达。
问途中他所有能到达的 '.'的数量是多少 ??当然,他自己本身也算一个能到达的点。

其中两个样例的结果是这样的走出来的,这是"显而易见"的,哈哈~当然,当图很大的时候,数起来就能费事了。
所用的这个方法叫做FlooFill(洪水覆盖),从它名字来看就是个很暴力直接的方法,只要我能到的地方,我都用水把你淹没了。可以联想一下,在田地里用水渠灌溉田地时,只要你把要灌溉的地方挖好水道,最后,只要打开总的水闸开关,那么所有你想灌溉的田里最后都会有水进去。那个水闸就是这里的@点了。
再看百度百科的解释

画图的填充就是这么来的,通俗的来说,无孔不入。所以,我把这个题看做画图填充,用这个方法做肯定不会错了。
由于存在计数问题,所以稍微处理下,首先将每个'.'看做是oldColer,即我还没填充到它,后面,就对所有我没填充到的点(颜色为oldColor的)并且是我能到达的点进行填充(一定是能到达的才能),把它变为(newColor1),每成功的涂色一次,就把计数加1,这样,就不存在计数问题了。

再对每个格子进行扩展填充时,可以有这上述两种填充方法(有点尴尬,我画图竟然没找到颜色填充。。。。)
然后每扩充一个格子,就再以那个格子为起点,继续扩充,即递归的填充,直到所有的格子都被填完。
/*************************************************************************
> File Name: poj1979.cpp
> Author: YeGuoSheng
> Description:
man at @,Q:how many points('.') he can arrive
#:can not be arrived
> Created Time: 2019年07月23日 星期二 16时32分10秒
************************************************************************/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;
const int maxn = ;
char g[maxn][maxn];//cin matrix
int G[maxn][maxn];//color the g ;
int n,m;//row ,col
int ans = ;
int startx,starty; int GetColor(int x,int y)
{
return G[x][y];
} void SetColor(int x,int y,int newColor)//change color 0 to 1
{
G[x][y] = newColor;
ans++;//result ++
} void FloodFill(int x,int y,int oldColor,int newColor)
{
if( x>= && x< n && y >= && y < m && GetColor(x,y) == oldColor)
{//Current position legal ,and color is oldColor => no access
SetColor(x,y,);//change color
FloodFill(x-,y,oldColor,newColor);//Flood covers the upper right and lower left four points
FloodFill(x,y+,oldColor,newColor);
FloodFill(x+,y,oldColor,newColor);
FloodFill(x,y-,oldColor,newColor);
}
} int main()
{
while(scanf("%d%d",&m,&n)&& n != && m!=)
{
ans = ;
memset(G,,sizeof(G));
memset(g,,sizeof(g));
for(int i = ;i< n;i++)
{
for(int j = ;j < m;j++)
{
cin>>g[i][j];
if(g[i][j]=='.')
G[i][j] = ;//old color
if(g[i][j]=='#')
G[i][j] = ;//new color && can not be covered
if(g[i][j]== '@')
{
G[i][j] = ;//old color
startx = i;
starty = j;
}
}
}
FloodFill(startx,starty,,);
cout<<ans<<endl;
}
return ;
}
POJ1979(Red and Black)--FloodFill的更多相关文章
- POJ1979 Red and Black (简单DFS)
POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- poj1979 Red And Black(DFS)
题目链接 http://poj.org/problem?id=1979 思路 floodfill问题,使用dfs解决 代码 #include <iostream> #include < ...
- POJ1979 Red and Black
速刷一道DFS Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- Poj1979 Red and Black (DFS)
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 47466 Accepted: 25523 D ...
- poj-1979 red and black(搜索)
Time limit1000 ms Memory limit30000 kB There is a rectangular room, covered with square tiles. Each ...
- POJ-1979 Red and Black(DFS)
题目链接:http://poj.org/problem?id=1979 深度优先搜索非递归写法 #include <cstdio> #include <stack> using ...
- 《挑战程序设计竞赛》2.1 深度优先搜索 POJ2386 POJ1979 AOJ0118 AOJ0033 POJ3009
POJ2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25366 Accepted: ...
- 图的遍历之深度优先搜索(DFS)
深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...
- 算法总结—深度优先搜索DFS
深度优先搜索(DFS) 往往利用递归函数实现(隐式地使用栈). 深度优先从最开始的状态出发,遍历所有可以到达的状态.由此可以对所有的状态进行操作,或列举出所有的状态. 1.poj2386 Lake C ...
随机推荐
- openresty开发系列23--lua面向对象
openresty开发系列23--lua面向对象 面向对象编程(Object Oriented Programming,OOP)是一种非常流行的计算机编程架构.java,c++,.net等都支持面向对 ...
- visual studio code 调试ROS的插件
ctrl+p搜索: ext install ros https://marketplace.visualstudio.com/items?itemName=ajshort.ros 进行安装 其他可以调 ...
- jQuery根据style筛选元素
<div style="display:block;"> <input/> </div> <div style="display ...
- SAP标准屏幕中字段描述增强
转自:https://www.cnblogs.com/sap-ronny/p/7921817.html如果想要改变标准屏幕中的字段名称,如把物料主数据基本数据元素的名字改为我们想要的名字 . 1.首先 ...
- SharpGL学习笔记(一) 平台构建与Opengl的hello World (转)
(一)平台构建与Opengl的hello World OpenGL就是3d绘图的API,微软针和它竞争推出D3D,也就是玩游戏时最常见的DirectorX组件中的3d功能. 所以不要指望windows ...
- 海康威视实时预览回调PS流用EasyRTMP向RTMP服务器推流中视频数据处理的代码
在上一篇方案<EasyRTMP结合海康HCNetSDK获取海康摄像机H.264实时流并转化成为RTMP直播推流(附源码)>我们介绍了将海康安防摄像机进行互联网直播的整体方案流程,其中有一个 ...
- MySQL插入大批量数据时报错“The total number of locks exceeds the lock table size”的解决办法
事情的原因是:我执行了一个load into语句的SQL将一个很大的文件导入到我的MySQL数据库中,执行了一段时间后报错"The total number of locks exceeds ...
- git如何新建分支
1) 切换到基础分支,如主干 git checkout master 2)创建并切换到新分支 git checkout -b panda git branch可以看到已经在panda分支上 3)更新分 ...
- Mstar方案软件运行基本原理
1. MApp_Main.c里有个while(1)循环: 2. 通过 while(1)循环MApp_MultiTasks 里面的 MApp_ProcessUserInput 可以 得到 当前的 u8K ...
- pandas对时间列分组求diff遇到的问题
例子: df = pd.DataFrame() df['A'] = [1, 1, 2] df['B'] = [datetime.date(2018, 1, 2), datetime.date(2018 ...