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[].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的更多相关文章
- [Swift]LeetCode773. 滑动谜题 | Sliding Puzzle
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...
随机推荐
- 亲测安装nginx1.8.1 日期2016年3月16日
1.安装nginx tar zxvf nginx-1.8.1.tar.gz cd nginx-1.8.1 ./configure make make install /usr/local/nginx/ ...
- day13作业
#作业1猜年龄 , 可以让用户最多猜三次! # Auther:bing #!/usr/bin/env python age = 24 print("猜年龄") for i in r ...
- UI- UIView控件知识点回顾
// 通过一个frame来初始化一个UI控件 - (id)initWithFrame:(CGRect)frame; // YES:能够跟用户进行交互 @property(nonatomic,gett ...
- L133
The U.S. Food and Drug Administration is considering a ban on flavorede-cigarettes in response to an ...
- Java基本数据对应的封装类
Java基本数据对应的封装类 在java中一切都是以类为基础的,并且java对基本的数据类型也进行了封装,如下所示,将介绍几个基本的数据类型及其封装类: 1 Boolean VS boolean pu ...
- tf随笔-6
import tensorflow as tfx=tf.constant([-0.2,0.5,43.98,-23.1,26.58])y=tf.clip_by_value(x,1e-10,1.0)ses ...
- lwip 使用记录(1)
原子F429的lwip实验:网络实验8 NETCONN_TCP客户端实验 代码 //tcp客户端任务函数 static void tcp_client_thread(void *arg) { OS_C ...
- Django之HttpRequest和HttpReponse
当一个web请求链接进来时,django会创建一个HttpRequest对象来封装和保存所有请求相关的信息,并且会根据请求路由载入匹配的试图函数,每个请求的试图函数都会返回一个HttpResponse ...
- bzoj 5369 最大前缀和
Written with StackEdit. Description 小\(C\)是一个算法竞赛爱好者,有一天小\(C\)遇到了一个非常难的问题:求一个序列的最大子段和. 但是小\(C\)并不会做这 ...
- iOS UI调试工具 -- UIDebuggingInformationOverlay
英文原文: http://ryanipete.com/blog/ios/swift/objective-c/uidebugginginformationoverlay/ 无意中看到iOS自带调试工具 ...