使用dfs算法时出现运行时错误

问题描述 投票:0回答:1

我在使用dfs时遇到了运行时错误。

这里是错误:

AddressSanitizer:地址0x7ffce7e6eff8(pc 0x000000359715 bp 0x7ffce7e6f040 sp 0x7ffce7e6f000 T0上的堆栈溢出

这里是问题链接:

https://leetcode.com/problems/flood-fill/

这是我的代码:

class Solution {
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {

        int color=image[sr][sc];
        dfs(image,sr,sc,color,newColor);
        return image;
    }

    void dfs(vector<vector<int>> &image,int sr,int sc,int color,int newcolor)
    {
         if(sr<0 || sr>=image.size() || sc<0 || sc>=image[0].size() || image[sr][sc]!=color)
        {
            return;
        }

            image[sr][sc]=newcolor;
            dfs(image,sr,sc+1,color,newcolor);
            dfs(image,sr,sc-1,color,newcolor);
            dfs(image,sr+1,sc,color,newcolor);
            dfs(image,sr-1,sc,color,newcolor);

    }


};

在以下测试用例上失败:

[[[0,0,0],[0,1,1]]1个1个1

我无法弄清楚哪里出了问题。

谢谢。

graph c++14 depth-first-search
1个回答
0
投票
这正是失败的测试用例会发生的情况。
© www.soinside.com 2019 - 2024. All rights reserved.