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 ...
随机推荐
- jvm最大线程数
摘自:http://sesame.iteye.com/blog/622670 工作中碰到过这个问题好几次了,觉得有必要总结一下,所以有了这篇文章,这篇文章分为三个部分:认识问题.分析问题.解决问题. ...
- 011——VUE中使用object与array控制class
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle
E. Dasha and Puzzle time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- php判断字符串长度 strlen()与mb_strlen()函数
PHP strlen() 函数 定义和用法 strlen() 函数返回字符串的长度. 语法 strlen(string) 参数:string <?php $str=‘中文a字1符‘; echo ...
- Java基础学习-代码块
/*代码块: * 用{}修饰的代码 * 1.局部代码块:控制变量,存在方法中,控制变量的生命周期(作用域) * 2.构造代码块:提取构造方法中的共性,每次创建对象,都会执行,并且在构造方法执行之前执行 ...
- 【SQL查询】查询结果翻译成其他值_decode
decode()函数简介: 主要作用:将查询结果翻译成其他值(即以其他形式表现出来,以下举例说明): 使用方法: Select decode(columnname,值1,翻译值1,值2,翻译值2,.. ...
- win7/8 关闭非正常关机的自动修复功能
win7/8 关闭非正常关机的自动修复功能 1.桌面右键新建一个文档文本,双击打开文件新建文本文档,复制以下命令到文本里面! bcdedit /set {default} bootstatuspoli ...
- 深度学习(六十五)移动端网络MobileNets
- 启动mysql 失败,“Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' ”
一.Mac OS X的升级或其他原因可能会导致MySQL启动或开机自动运行时 在MySQL操作面板上会提示“Warning:The /usr/local/mysql/data directory is ...
- gulp 集成其他基于流的工具
1. 流.缓冲.vinyl 文件对象 gulp 的流是虚拟文件对象 包含的属性有 base 文件名 path 文件路径 content 缓冲.nodejs 流 2. gulp 集成 browserif ...