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开发系列26--openresty中使用redis模块
openresty开发系列26--openresty中使用redis模块 在一些高并发的场景中,我们常常会用到缓存技术,现在我们常用的分布式缓存redis是最知名的, 操作redis,我们需要引入re ...
- 【Dart学习】--Dart之数组(List)的相关方法总结
一,初始化List 非固定长度list var testList = List(); print(testList.length);//输出0 固定长度List var testList2 = Lis ...
- 重启WMS服务
一.重启API服务 查看进程ps ef|grep java 进入目录 cd /usr/local/tomcat-api/bin ./shutdown.sh ps –ef|grep 查看服务是否真的停止 ...
- 【html】css、js实现网页内容禁止选中
网页内容不能选中.复制应该如何实现呢? 通过css *{ moz-user-select: -moz-none; -moz-user-select: none; -o-user-select:none ...
- Qt编写气体安全管理系统5-数据监控
一.前言 本项目对设备的监控有四种视图模式,可以任意切换,数据监控.地图监控.设备监控.曲线监控,其中数据监控是最常用的,所以在主界面导航中也排在第一位,综合观察分析了很多气体安全或者组态监控软件,大 ...
- k8s记录-ntpd时间同步配置(五)
1)服务端配置 在192.168.0.1 root用户下操作 yum install -y ntp ntpdate 修改etc/ntp.conf 注释所有的server和restrict 加入: se ...
- 【helm & Tiller】报错Error: incompatible versions client[v2.14.1] server[v2.13.0] │
helm是helm的客户端部分 tiller是helm的服务器端部分 报错 报错Error: incompatible versions client[v2.14.1] server[v2.13.0] ...
- jQuery 控制網頁捲軸移動 & Ignore link '#' method jump action
$('a.gotoheader').click(function(){ // 讓捲軸移動到 0 的位置 $(); // ignore link "#" method return ...
- Swoole练习 TCP
TCP <?php $serv = new swoole_server("127.0.0.1", 9501); //监听连接进入事件 $serv->on('connec ...
- 最新 农信互联java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.农信互联等10家互联网公司的校招Offer,因为某些自身原因最终选择了农信互联.6.7月主要是做系统复习.项目复盘.Leet ...