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 ...
随机推荐
- kubernetes 亲和性调度详解
文章目录 1 概述: 2 场景一:调度到一组具有相同特性的主机上(label+nodeSelector) 3 场景二:部署的应用不想调度到某些节点上(nodeaffinity) 4 场景三:部署的应用 ...
- Python和Pygame游戏开发 pdf
Python和Pygame游戏开发 目录 第1章 安装Python和Pygame 11.1 预备知识 11.2 下载和安装Python 11.3 Windows下的安装说明 11.4 Mac OS X ...
- IBM System x3650 M3_RAID服务器进入阵列卡配置界面(webBIOS)
按 F1 进入 UEFI, 选择“System Settings”- “Adapters and UEFI Drivers”- “Please press ENTER to compile the l ...
- ip地址分类和网段详解
IP地址分类/IP地址10开头和172开头和192开头的区别/判断是否同一网段 简单来说在公司或企业内部看到的就基本都是内网IP,ABC三类IP地址里的常见IP段. 每个IP地址都包含两部分,即网络号 ...
- 透过SourceTree再谈Git
初出茅庐之基础篇 1. Download SourceTree from: https://www.sourcetreeapp.com/ 2.Complete the installation. 3. ...
- prometheus数据格式
注意区分以下两种“数据格式”: 1.自定义exporter的时候所需要遵循的给prometheus提供数据的数据格式: https://yunlzheng.gitbook.io/prometheus- ...
- ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists
通过service mysql status 命令来查看mysql 的启动状态 报错如下: ERROR! MySQL is not running, but lock file (/var/lock/ ...
- shell每隔一秒钟就记录下netstat状态
说明 木马可能类似随机发送心跳包的操作,随机sleep.对这类情况写好了一个监听shell脚本,每隔一秒钟就记录下netstat状态. 代码 #!/bin/bash #功能:用于定时执行lsof 和 ...
- Web调试利器fiddler(转)
http://blog.chinaunix.net/uid-27105712-id-3738821.html
- 【GStreamer开发】GStreamer基础教程05——集成GUI工具
目标 本教程展示了如何在GStreamer集成一个GUI(比如:GTK+).最基本的原则是GStreamer处理多媒体的播放而GUI处理和用户的交互. 在这个教程里面,我们可以学到: 如何告诉GStr ...