zoukankan      html  css  js  c++  java
  • 9.9递归和动态规划(七)——实现很多图片编辑软件都支持的“填充颜色”功能

    /**
     * 功能:实现很多图片编辑软件都支持的“填充颜色”功能。
     * 给定一个屏幕(以二维数组表示,元素为颜色值)、一个点和一个新的颜色值,将新颜色填入这个店的周围区域,知道原来的颜色值全都改变。

     */


    	/**
    	 * 思路:如果要对一个像素(比方红色)调用paintFill。即对周围的像素逐一调用paintFill,
    	 * 向外扩张,一旦碰到非红色的像素就停止填充。
    	 * 
    	 * 注意:碰到图像问题,要注意screen[y][x]中x和y的顺序。x表示水平轴(即自左向右),实际上相应于列数。而非行数。

    y的值等于行数。

    * @param screen * @param x * @param y * @param ncolor * @return */ public static boolean paintFill(Color[][] screen,int x,int y,Color ncolor){ if(screen[y][x]==ncolor) return false; return paintFill(screen, x, y, screen[y][x], ncolor); } public static boolean paintFill(Color[][] screen,int x,int y,Color ocolor,Color ncolor){ if(x<0||x>=screen[0].length||y<0||y>=screen.length) return false; if(screen[y][x]==ocolor){ screen[y][x]=ncolor; paintFill(screen, x-1, y, ocolor, ncolor);//左 paintFill(screen, x+1, y, ocolor, ncolor);//右 paintFill(screen, x, y-1, ocolor, ncolor);//上!!! paintFill(screen, x, y+1, ocolor, ncolor);//下!

    !!

    } return true; }


    enum Color{
    	Black,White,Red,Yellow,Green
    }


  • 相关阅读:
    D. Constructing the Array
    B. Navigation System
    B. Dreamoon Likes Sequences
    A. Linova and Kingdom
    G. Special Permutation
    B. Xenia and Colorful Gems
    Firetrucks Are Red
    java getInstance()的使用
    java 静态代理和动态代理
    java 类加载机制和反射机制
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7216083.html
Copyright © 2011-2022 走看看