Paint Fill java 走地牙 CTCI 8.10

package com.company;
/*
* Paint Fill: Implement the "paint fill" function that one might see on many image editing programs.
* That is, given a screen (represented by a two-dimensional array of colors), a point, and a new color,
* fill in the surrounding area until the color changes from the original color.
* */

public class Main {

    public static void main(String[] args) {
   // write your code here
        int r = 1, c = 1;
        Color[][] screen = new Color[3][3];
        screen[0][0] = Color.black;
        screen[0][1] = Color.black;
        screen[0][2] = Color.black;
        screen[1][0] = Color.black;
        screen[1][1] = Color.red;
        screen[1][2] = Color.red;
        screen[2][0] = Color.red;
        screen[2][1] = Color.red;
        screen[2][2] = Color.red;
        Color newColor = Color.white;
        boolean isChanged = paintFill(screen, r, c, newColor);
        System.out.println(isChanged);
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                System.out.print(screen[i][j]);
            }
            System.out.println("");
        }
    }

    public static boolean paintFill(Color[][] screen, int r, int c, Color newColor) {
        if(screen[r][c] == newColor) {
            return false;
        }
        return paintFill(screen, r, c, screen[r][c], newColor);
    }

    public static boolean paintFill(Color[][] screen, int r, int c, Color oldColor, Color newColor) {
        if(r < 0 || r >= screen.length || c < 0 || c >= screen[0].length) {
            return false;
        }
        if(screen[r][c] != oldColor){
            return false;
        }
        screen[r][c] = newColor;
        paintFill(screen, r - 1, c, oldColor, newColor);
        paintFill(screen, r + 1, c, oldColor, newColor);
        paintFill(screen, r, c - 1, oldColor, newColor);
        paintFill(screen, r, c + 1, oldColor, newColor);
        return true;
    }
}

 

package com.company;

enum Color {
    black, white, red, green, yellow
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值