使用两种语言实现,先贴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. New Concept English three (26)

    34w/m 54words No one can avoid being influenced by advertisements. Much as we may pride ourselves on ...

  2. jsp中的session

    浏览器和服务器的异常通话 常用方法 setAttribute(String key,Object value);//设置值 getAttribute(String key); //取值 Invalid ...

  3. js中的函参(arguments)

    函参,顾名思义,就是函数的参数,一般我们的js函数这么写: function sum(a,b){ console.log(a+b); } 不难看出,这实现了两个数的相加,比如sum(1,2),打印结果 ...

  4. lua基础---函数

    Lua的函数功能很强大,保留了C语言的一些基本的特性,但是也有C语言没有的特性,比如,lua可以在一个函数返回多个值,我们来看看下面这个案例: 解释运行: lua test5.lua --定义一个函数 ...

  5. Python itertools模块中的product函数

    product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in A for y ...

  6. flex 弹性布局的大坑!!

    如果父元素设置 display:flex,那么其中的子元素会被当成行内元素对待,即会忽略其宽度 这在设置背景图时要特别特别注意!!!!

  7. 跟我学Delphi Xe4 开发 IOS 一 , 重读Delphi Xe4 自带文档.

    安装了 Delphi Xe4 之后打开这个地址就是完整的官方的文档了. 虽然不是立刻能解决你的问题. 但也是必须要看一遍的. 最基础的都在这里了. ms-help://embarcadero.rs_x ...

  8. vue动态 设置类名

    <div class="tab"> <navigator :class="currentTab=='mzfw'?'nav active': 'nav'& ...

  9. linux【基础命令】

    最近在学linux,避免一些命令忘记,所以在此记录一下: linux文件列表遍历 ls -a 列出所有的文件及文件夹 包括隐藏的ls -l 列出文件目录的详细信息 history 查看历史命令ctrl ...

  10. [转载] ffmpeg 基本数据结构和对象: AVPacket、AVPicture、AVFrame

    一.AVPacket /** * AVPacket 作为解码器的输入 或 编码器的输出. * 当作为解码器的输入时,它由demuxer生成,然后传递给解码器 * 当作为编码器的输出时,由编码器生成,然 ...