洪水填充

题目描述
在一个nxm矩阵形状的城市里爆发了洪水,洪水从(0,0)的格子流到这个城市,在这个矩阵中有的格子有一些建筑,洪水只能在没有建筑的格子流动。请返回洪水流到(n - 1,m - 1)的最早时间(洪水只能从一个格子流到其相邻的格子且洪水单位时间能从一个格子流到相邻格子)。
给定一个矩阵map表示城市,其中map[i][j]表示坐标为(i,j)的格子,值为1代表该格子有建筑,0代表没有建筑。同时给定矩阵的大小n和m(n和m均小于等于100),请返回流到(n - 1,m - 1)的最早时间。保证洪水一定能流到终点。

注意这不是那个简单的动态规划,而是可以往回走的,可以用队列来实现四个方向的迭代搜索

class Flood {
public:
    int floodFill(vector<vector<int> > map, int n, int m) {
        // write code here
        int direction[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
        queue<int> q;
        q.push(0);
        while(!q.empty()){
            int cur=q.front();
            q.pop();
            int i=cur/m;   //得出x轴坐标
            int j=cur%m;   //得出y轴坐标
            if(i==n-1 && j==m-1){
                return map[n-1][m-1];
            }
            for(int k=0;k<4;k++){
                int next_x=i+direction[k][0];
                int next_y=j+direction[k][1];
                if(next_x>=0 && next_x<n && next_y>=0 && next_y<m && map[next_x][next_y]==0){
                    q.push(next_x*m+next_y);
                    map[next_x][next_y]=map[i][j]+1;
                }
            }
        }
        return 0;
    }
};
class Flood {
public:
    int floodFill(vector<vector<int> > map, int n, int m) {
        // write code here
        int x,y,point;
        int direction[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
        queue<int> q;
        q.push(0);
        int next_x,next_y;

        if(n==0 || m==0 ||map[0][0]) return -1;

        while(!q.empty())
           {
             point = q.front();
             q.pop();
             x = point/m;
             y = point%m;
            if(x==n-1 && y == m-1)
                break;
            for(int k=0;k<4;k++)
               {
                next_x = x + direction[k][0];
                next_y = y + direction[k][1];
                if(next_x >=0 &&next_x <n && next_y >=0 && next_y <m && map[next_x][next_y] == 0)
                    {
                    q.push(next_x *m+next_y);
                    map[next_x][next_y] = map[x][y] +1;
                }
            }

        }
        return map[n-1][m-1];
    }
};
洪水填充算法(Flood Fill)是一种图像处理算法,它通过指定一个起始点和一个目标颜色,将所有与起始点颜色相同的区域都填充成指定的目标颜色。在Python中,可以使用递归来实现洪水填充算法。 根据引用\[1\]中提供的信息,我们可以使用以下代码实现洪水填充算法: ```python def flood_fill(image, start_x, start_y, target_color, replacement_color): if image\[start_x\]\[start_y\] != target_color: return image\[start_x\]\[start_y\] = replacement_color if start_x > 0: flood_fill(image, start_x - 1, start_y, target_color, replacement_color) if start_x < len(image) - 1: flood_fill(image, start_x + 1, start_y, target_color, replacement_color) if start_y > 0: flood_fill(image, start_x, start_y - 1, target_color, replacement_color) if start_y < len(image\[0\]) - 1: flood_fill(image, start_x, start_y + 1, target_color, replacement_color) ``` 在引用\[2\]中提到的问题中,可能是代码中的某些问题导致了错误。可以检查代码中是否正确导入了pygame库,并确保代码中的变量和函数调用正确无误。 总结来说,洪水填充算法是一种用于图像处理的算法,可以通过指定起始点和目标颜色来填充图像中的区域。在Python中,可以使用递归来实现该算法。如果在实际应用中遇到问题,可以检查代码中的错误或者调试环境是否正常。 #### 引用[.reference_title] - *1* *3* [Python实现泛洪填充算法类(附完整源代码)](https://blog.csdn.net/update7/article/details/131496703)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python中的洪水填充算法崩溃](https://blog.csdn.net/weixin_39847887/article/details/114396656)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值