使用两种语言实现,先贴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[].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 = ; int Visited[N][N]; for (int i = ; i < row; i++)
{
for (int j = ; 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 - , y);
pair<int, int> Down_Node = make_pair(x + , y);
pair<int, int> Left_Node = make_pair(x, y - );
pair<int, int> Right_Node = make_pair(x, y + ); if (Up_Node.first >= && !Visited[Up_Node.first][Up_Node.second] && image[Up_Node.first][Up_Node.second] == oldColor)
{
Q.push(Up_Node);
} if (Down_Node.first <= row - && !Visited[Down_Node.first][Down_Node.second] && image[Down_Node.first][Down_Node.second] == oldColor)
{
Q.push(Down_Node);
} if (Left_Node.second >= && !Visited[Left_Node.first][Left_Node.second] && image[Left_Node.first][Left_Node.second] == oldColor)
{
Q.push(Left_Node);
} if (Right_Node.second <= col - && !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();
int col = image.GetLength(); 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 - , y);
var Down_Node = new KeyValuePair<int, int>(x + , y);
var Left_Node = new KeyValuePair<int, int>(x, y - );
var Right_Node = new KeyValuePair<int, int>(x, y + ); if (Up_Node.Key >= && !Visited[Up_Node.Key, Up_Node.Value] && image[Up_Node.Key, Up_Node.Value] == oldColor)
{
Q.Enqueue(Up_Node);
} if (Down_Node.Key <= row - && !Visited[Down_Node.Key, Down_Node.Value] && image[Down_Node.Key, Down_Node.Value] == oldColor)
{
Q.Enqueue(Down_Node);
} if (Left_Node.Value >= && !Visited[Left_Node.Key, Left_Node.Value] && image[Left_Node.Key, Left_Node.Value] == oldColor)
{
Q.Enqueue(Left_Node);
} if (Right_Node.Value <= col - && !Visited[Right_Node.Key, Right_Node.Value] && image[Right_Node.Key, Right_Node.Value] == oldColor)
{
Q.Enqueue(Right_Node);
}
} return image;
}
}

leetcode773的更多相关文章

  1. [Swift]LeetCode773. 滑动谜题 | Sliding Puzzle

    On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...

随机推荐

  1. shell编程-数组

    #!bin/bash/ A=(a b c hello) echo ${A[*]} echo ${A[@]} ]} ]} ]} ]} ]}echo ${#A[*]} #测数组长度方法1echo ${#A ...

  2. idea远程debug调试设置

    1.idea设置 1.1 加入Tomcat Server选择Remote 1.2:设置对应的參数 xxx.xxx.152.67:8080为远程Tomcatserver的IP地址和port,这里能够设置 ...

  3. springmvc跨域(转)

    跨域资源共享 CORS 详解  原文链接:http://www.ruanyifeng.com/blog/2016/04/cors.html   作者: 阮一峰 日期: 2016年4月12日 CORS是 ...

  4. java String 转 Long 两种方法区别

    Long.ValueOf("String")返回Long包装类型 包装类型: Byte,Integer,Short,Long,Boolean,Character,Float,Dou ...

  5. keras中自定义Layer

    最近在学习SSD的源码,其中有两个自定的层,特此学习一下并记录. import keras.backend as K from keras.engine.topology import InputSp ...

  6. Zookeeper在Dubbo中的作用及Zk集群的选举原理

    转自 : https://blog.csdn.net/zh15732621679/article/details/80723358

  7. asp.net viewstate 数据大导致错误

    当在ViewState中放入dataSet的数据量比较大的时候,当再点页面上的控件时,不会返回到后台,并且会出现如下错误: 或者是上面的12030改成500的错误. --解决方法:Viewstate绑 ...

  8. BaseCommand

    import java.io.Serializable; import android.util.Log; public class BaseCommand implements Serializab ...

  9. Python标准库之time和datetime

    注:博客转载自:https://www.cnblogs.com/zhangxinqi/p/7687862.html 1.python3日期和时间 Python 程序能用很多方式处理日期和时间,转换日期 ...

  10. Eclipse环境搭建配置操作

    1.选择window 2.设置字体 3.设置编码格式:国际编码:UTF-8 第一个地方设置编码格式 第二个地方设置编码格式:这个更重要些 4.配置26个英文小写字母. 作用:能够在开发时提示你,快速开 ...