leetcode773
Posted AsenYang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } }
以上是关于leetcode773的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] 773. Sliding Puzzle 滑动拼图
LeetCode 773 滑动谜题[BFS] HERODING的LeetCode之路
LeetCode 752. 打开转盘锁 / 127. 单词接龙 / 773. 滑动谜题(学习双向BFS,A*算法)
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段