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 ...
随机推荐
- Remix 搭建与简单使用,并支持外部访问
Remix 搭建与简单使用,并支持外部访问 转 https://blog.csdn.net/linshenyuan1213/article/details/83444911 remix是基于浏览器的在 ...
- DataSource接口 Connection pooling(连接池
一.DataSource接口是一个更好的连接数据源的方法: JDBC1.0是原来是用DriverManager类来产生一个对数据源的连接.JDBC2.0用一种替代的方法,使用DataSource的实 ...
- 【插件式框架探索系列】使用多UI线程提升性能
了解WPF线程模型的都知道,UI线程负责呈现和管理UI,而UI元素(派生自 DispatcherObject)只能由创建该元素的线程来访问,这就导致了一些耗时的UI操作将影 响到整个应用程序性能,未响 ...
- linux普通用户添加root权限
新增一个普通用户并进入该用户: [root@VM_0_7_centos ~]# groupadd mall [root@VM_0_7_centos ~]# useradd mall -m -d /ho ...
- ArrayList数组操作
String字符类型的操作方法 public static void main(String[] args) { // ArrayList ArrayList<String> list = ...
- 【Leetcode_easy】949. Largest Time for Given Digits
problem 949. Largest Time for Given Digits solution: class Solution { public: string largestTimeFrom ...
- Clean Code 代码整洁之道
军规:让营地比你来时更干净. 整洁代码 Leblanc : Later equals never. (勒布朗法则:稍后等于永不) 对代码的每次修改都影响到其他两三处代码. 修改无小事. 如同医生不能遵 ...
- 虚拟机中windows下制作超级隐藏账户
这篇博客非原创,我只是将很多大佬写的东西理解了一下写了出来. 接下来的实验最好在虚拟机进行,因为可以快照~ 制作隐藏用户可以说是两种方法但是基本操作一样,所以我们穿插着进行一种是隐藏账户,一种是影子账 ...
- [EF] - "已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭" 之解决
错误 解决 在 ConnectionString 中添加 MultipleActiveResultSets=true(适用于SQL 2005以后的版本).MultipleActiveResultSet ...
- [Xamarin] - Xamarin.Forms Project with .Net Standard 2.0
1. Install .NET Core 2.0 SDK .https://www.microsoft.com/net/download/core 2. Install Android 7.1 (AP ...