zoukankan      html  css  js  c++  java
  • POJ1979(Red and Black)--FloodFill

    题目在这里

    题目意思是这样的,一个人起始位置在    '@'  处,他在途中能到达的地方为 ' .  '     而  '#' 是障碍物,他不能到达。

    问途中他所有能到达的   '.'的数量是多少 ??当然,他自己本身也算一个能到达的点。

    其中两个样例的结果是这样的走出来的,这是"显而易见"的,哈哈~当然,当图很大的时候,数起来就能费事了。

    所用的这个方法叫做FlooFill(洪水覆盖),从它名字来看就是个很暴力直接的方法,只要我能到的地方,我都用水把你淹没了。可以联想一下,在田地里用水渠灌溉田地时,只要你把要灌溉的地方挖好水道,最后,只要打开总的水闸开关,那么所有你想灌溉的田里最后都会有水进去。那个水闸就是这里的@点了。

    再看百度百科的解释

    画图的填充就是这么来的,通俗的来说,无孔不入。所以,我把这个题看做画图填充,用这个方法做肯定不会错了。

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

    再对每个格子进行扩展填充时,可以有这上述两种填充方法(有点尴尬,我画图竟然没找到颜色填充。。。。)

    然后每扩充一个格子,就再以那个格子为起点,继续扩充,即递归的填充,直到所有的格子都被填完。 

     1 /*************************************************************************
     2     > File Name: poj1979.cpp
     3     > Author: YeGuoSheng
     4     > Description:  
     5     man at @,Q:how many points('.') he can arrive
     6     #:can not be arrived
     7     > Created Time: 2019年07月23日 星期二 16时32分10秒
     8  ************************************************************************/
     9 #include<iostream>
    10 #include<stdio.h>
    11 #include<cstring>
    12 #include<cmath>
    13 #include<vector>
    14 #include<stack>
    15 #include<map>
    16 #include<set>
    17 #include<list>
    18 #include<queue>
    19 #include<string>
    20 #include<algorithm>
    21 #include<iomanip>
    22 using namespace std;
    23 const int maxn = 20;
    24 char g[maxn][maxn];//cin matrix
    25 int G[maxn][maxn];//color the g ;
    26 int n,m;//row ,col 
    27 int ans = 0;
    28 int startx,starty;
    29 
    30 int GetColor(int x,int y)
    31 {
    32     return G[x][y];
    33 }
    34 
    35 void SetColor(int x,int y,int newColor)//change color 0  to 1
    36 {
    37     G[x][y] = newColor;
    38     ans++;//result ++
    39 }
    40 
    41 void FloodFill(int x,int y,int oldColor,int newColor)
    42 {
    43     if( x>= 0 && x< n && y >=0 && y < m && GetColor(x,y) == oldColor)
    44     {//Current position legal ,and color is oldColor => no access 
    45         SetColor(x,y,1);//change color
    46         FloodFill(x-1,y,oldColor,newColor);//Flood covers the upper right and lower left four points
    47         FloodFill(x,y+1,oldColor,newColor);
    48         FloodFill(x+1,y,oldColor,newColor);
    49         FloodFill(x,y-1,oldColor,newColor);
    50     }
    51 }
    52 
    53 int main()
    54 {
    55     while(scanf("%d%d",&m,&n)&& n != 0 && m!=0)
    56     {
    57         ans = 0;
    58         memset(G,0,sizeof(G));
    59         memset(g,0,sizeof(g));
    60         for(int i = 0;i< n;i++)
    61         {
    62             for(int j = 0;j < m;j++)
    63             {
    64                 cin>>g[i][j];
    65                 if(g[i][j]=='.')
    66                     G[i][j] = 0;//old color
    67                 if(g[i][j]=='#')
    68                     G[i][j] = 1;//new color && can not be covered
    69                 if(g[i][j]== '@')
    70                 {
    71                     G[i][j] = 0;//old color
    72                     startx = i;
    73                     starty = j;
    74                 }
    75             }
    76         }
    77         FloodFill(startx,starty,0,1);
    78         cout<<ans<<endl;
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    从构建分布式秒杀系统聊聊限流特技
    轻快的VIM(三):删除
    shell中各种括号的作用()、(())、[]、[[]]、{}
    java的重写规则
    UNIX命令,统计当前目录(含子目录)下所有后缀为.log的文件中ERROR出现的行数
    linux下使用 du查看某个文件或目录占用磁盘空间的大小
    linux如何查看系统占用磁盘空间最大的文件及让文件按大小排序
    管道命令和xargs的区别(经典解释)
    JAVA 一个或多个空格分割字符串
    shell替换一个或多个空格为逗号
  • 原文地址:https://www.cnblogs.com/ygsworld/p/11238031.html
Copyright © 2011-2022 走看看