zoukankan      html  css  js  c++  java
  • leetcode773

    使用两种语言实现,先贴C++的

    class Solution {
    public:
        vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
            int row = image.size();
            int col = image[0].size();
    
            int oldColor = image[sr][sc];
            if (oldColor == newColor)
            {
                return image;
            }
            queue<pair<int, int>> Q;
            Q.push(make_pair(sr, sc));
    
            const int N = 51;
    
            int Visited[N][N];
    
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    Visited[i][j] = false;
                }
            }
    
            while (!Q.empty())
            {
                pair<int, int> Node = Q.front();
                Q.pop();
                int x = Node.first;
                int y = Node.second;
                if (Visited[x][y])
                {
                    continue;//略过此点
                }
                else
                {
                    Visited[x][y] = true;
                    image[x][y] = newColor;
                }
                pair<int, int> Up_Node = make_pair(x - 1, y);
                pair<int, int> Down_Node = make_pair(x + 1, y);
                pair<int, int> Left_Node = make_pair(x, y - 1);
                pair<int, int> Right_Node = make_pair(x, y + 1);
    
                if (Up_Node.first >= 0 && !Visited[Up_Node.first][Up_Node.second] && image[Up_Node.first][Up_Node.second] == oldColor)
                {
                    Q.push(Up_Node);
                }
    
                if (Down_Node.first <= row - 1 && !Visited[Down_Node.first][Down_Node.second] && image[Down_Node.first][Down_Node.second] == oldColor)
                {
                    Q.push(Down_Node);
                }
    
                if (Left_Node.second >= 0 && !Visited[Left_Node.first][Left_Node.second] && image[Left_Node.first][Left_Node.second] == oldColor)
                {
                    Q.push(Left_Node);
                }
    
                if (Right_Node.second <= col - 1 && !Visited[Right_Node.first][Right_Node.second] && image[Right_Node.first][Right_Node.second] == oldColor)
                {
                    Q.push(Right_Node);
                }
            }
    
            return image;
        }
    };

    下面贴出C#的

    public class Solution
        {
            public int[,] FloodFill(int[,] image, int sr, int sc, int newColor)
            {
                int row = image.GetLength(0);
                int col = image.GetLength(1);
    
                int oldColor = image[sr, sc];
                if (oldColor == newColor)
                {
                    return image;
                }
                Queue<KeyValuePair<int, int>> Q = new Queue<KeyValuePair<int, int>>();
                Q.Enqueue(new KeyValuePair<int, int>(sr, sc));
    
                bool[,] Visited = new bool[row, col];
    
                while (Q.Any())
                {
                    var Node = Q.Dequeue();
                    int x = Node.Key;
                    int y = Node.Value;
                    if (Visited[x, y])
                    {
                        continue;//略过此点
                    }
                    else
                    {
                        Visited[x, y] = true;
                        image[x, y] = newColor;
                    }
                    var Up_Node = new KeyValuePair<int, int>(x - 1, y);
                    var Down_Node = new KeyValuePair<int, int>(x + 1, y);
                    var Left_Node = new KeyValuePair<int, int>(x, y - 1);
                    var Right_Node = new KeyValuePair<int, int>(x, y + 1);
    
                    if (Up_Node.Key >= 0 && !Visited[Up_Node.Key, Up_Node.Value] && image[Up_Node.Key, Up_Node.Value] == oldColor)
                    {
                        Q.Enqueue(Up_Node);
                    }
    
                    if (Down_Node.Key <= row - 1 && !Visited[Down_Node.Key, Down_Node.Value] && image[Down_Node.Key, Down_Node.Value] == oldColor)
                    {
                        Q.Enqueue(Down_Node);
                    }
    
                    if (Left_Node.Value >= 0 && !Visited[Left_Node.Key, Left_Node.Value] && image[Left_Node.Key, Left_Node.Value] == oldColor)
                    {
                        Q.Enqueue(Left_Node);
                    }
    
                    if (Right_Node.Value <= col - 1 && !Visited[Right_Node.Key, Right_Node.Value] && image[Right_Node.Key, Right_Node.Value] == oldColor)
                    {
                        Q.Enqueue(Right_Node);
                    }
                }
    
                return image;
            }
        }
  • 相关阅读:
    在Windows10上安装Linux子系统
    《ln--软连接》--linux命令
    【转】最详细的Log4J使用教程
    7 str字符串的内置函数
    6.1 range创建数值序列
    6 int数值类型的内置函数
    linux shell 字符串操作(长度,查找,截取,替换)详解
    awk 字符串处理函数
    Shell字符串截取
    5 python基本数据类型
  • 原文地址:https://www.cnblogs.com/asenyang/p/9732895.html
Copyright © 2011-2022 走看看