LintCode 278·绘制填充 深度优先搜索 C++

描述
你将得到一个二维数组board,以及三个整数x,y,newColor。
board[i][j] 代表画板第i行第j列的像素点的颜色,不同的颜色将用不同的整数表示。
x与y代表你将要点击画板上第x行第y列的像素点,newColor则代表你将用于“填充”的颜色。
你需要直接在原数组board上进行操作,评测程序将检查数组board是否被修改正确。

输入:

board = [[1, 0, 2], [1, 0, 0], [3, 1, 0]]
x = 0
y = 1
newColor = 5
输出:

[[1, 5, 2], [1, 5, 5], [3, 1, 5]]

思路分析:题目感觉还没例子描绘的清楚,就是把这个坐标点(值为k)上下左右连通的相同值的点全部改为newcolor的值,那思路就很清晰了,遍历上下左右值为k的点及这些点派生的上下左右值为K的点,全部改为newcolor。感觉这种类型的都是这样,上下左右遍历,改值就OK了,还是发上来水一下文章。

class Solution {
public:
    /**
     * @param board: a two-dimensional array of colors
     * @param x: the abscissa of the click position
     * @param y: the ordinate of the click position
     * @param newColor: an integer that represent a new color
     * @return: nothing
     */

     int v[4][2]={{1,0},{-1,0},{0,-1},{0,1}};

    void DFS(vector<vector<int>> &board,int i,int j,int k,int m,int n,int newColor)
    {
        if(i>=0&&i<m&&j>=0&&j<n&&board[i][j]==k)
        {
        	//改值
            board[i][j]=newColor;
            //遍历上下左右
            for(int x=0;x<4;x++)
            {
                DFS(board,i+v[x][0],j+v[x][1],k,m,n,newColor);
            }
        }
    }

    void paintFill(vector<vector<int>> &board, int x, int y, int newColor) {
        // write your code here.
        int k=board[x][y];
        int m=board.size();
        int n=board[0].size();
        DFS(board,x,y,k,m,n,newColor);
    }
};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下坠丷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值