• 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;
            }
        }
  • 相关阅读:
    基于HttpListener的web服务器
    基于TcpListener的web服务器
    一个简单的web服务器
    c# 6.0新特性(二)
    c# 6.0新特性(一)
    c#之Redis实践list,hashtable
    html5摇一摇[转]
    在Microsoft-IIS/10.0上面部署mvc站点的时候,出现404的错误
    [实战]MVC5+EF6+MySql企业网盘实战(28)——其他列表
    让DELPHI自带的richedit控件显示图片
  • 原文地址:https://www.cnblogs.com/asenyang/p/9732895.html
Copyright © 2020-2023  润新知