当前位置:网站首页>Leetcode:733. image rendering [BFS]
Leetcode:733. image rendering [BFS]
2022-07-25 04:01:00 【Starry sky and bright moon】
Title Description
There's a picture m x n A picture represented by a two-dimensional array of integers image , among image[i][j] Indicates the pixel value size of the picture .
You are also given three integers sr , sc and newColor . You should start with pixels image[sr][sc] Start editing the image Color fill .
In order to complete the Coloring work , Start with the initial pixel , Record the initial coordinates Up, down, left and right Connected pixels with the same pixel value as the initial coordinates , Then record the pixels that meet the conditions in these four directions and correspond to them In four directions Connected pixels with the same pixel value as the initial coordinates ,……, Repeat the process . Change the color value of all recorded pixels to newColor .
Finally back to The color rendered image .
Input and output
Input : image = [[1,1,1],[1,1,0],[1,0,1]],sr = 1, sc = 1, newColor = 2
Output : [[2,2,2],[2,2,0],[2,0,1]]
Tips
- m == image.length
- n == image[i].length
- 1 <= m, n <= 50
- 0 <= image[i][j], newColor < 216
- 0 <= sr < m
- 0 <= sc < n
Ideas
The meaning of the title is to find [sr][sc] Connected block as the starting point , Change its value to newColor. use bfs Just do it .
AC Code
class Solution {
public:
int dir[4][2] = {
{
1, 0}, {
0, 1}, {
0, -1}, {
-1, 0}};
int vis[50][50];
typedef pair<int, int> pii;
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
queue<pii> q;
int val = image[sr][sc];
int n = image.size();
int m = image[0].size();
q.push(pii(sr, sc));
while(!q.empty()) {
pii cur = q.front(); q.pop();
int cx = cur.first, cy = cur.second;
image[cx][cy] = color;
vis[cx][cy] = 1;
for (int i = 0; i < 4; ++i) {
int dx = cx + dir[i][0];
int dy = cy + dir[i][1];
if (dx < 0 || dx >= n || dy < 0 || dy >= m || image[dx][dy] != val || vis[dx][dy] == 1) continue;
q.push(pii(dx, dy));
}
}
return image;
}
};
边栏推荐
- MySQL
- Force deduction problem 238. product of arrays other than itself
- Debezium series: Show slave status view the possible different situations of master-slave delay
- Jenkins continues to integrate entry to mastery
- Moveit2 - 8. Motion planning API
- MySQL eight shares
- Performance test indicators using JMeter
- To clarify the tax arrears: there is no tax arrears, and will continue to operate in compliance, rooted in China
- An article takes you quickly to learn flex layout
- 292. Nim game
猜你喜欢

Network security - comprehensive penetration test -cve-2018-10933-libssh maintain access

ADS1256 debugging notes based on stm32hal Library

Chapter 3 business function development (modify the remarks of market activities)

Implementing DDD based on ABP -- domain logic and application logic

基于SSM实现后勤报修系统

Force the resumption of game 302 of the week

Fiddler grabs packets and displays err_ TUNNEL_ CONNECTION_ FAILED

C language: string processing function

C language file operation
![[understanding of opportunity-47]: Guiguzi - Chapter 11 - decision makers, moderation, and rational distribution of interests](/img/f1/e95f230e618d4c4b67b2e0a8c8507b.jpg)
[understanding of opportunity-47]: Guiguzi - Chapter 11 - decision makers, moderation, and rational distribution of interests
随机推荐
.net6 miniapi (V): Options
基于SSH婴幼儿产品销售系统
[kaggle] how to effectively avoid oom and the long process of alchemy
Force deduction question 3
Multithreading advanced Step2
[Flink] protocol operator reduce
[Flink] submit the jar package to the Flink cluster and run it
[understanding of opportunity-47]: Guiguzi - Chapter 11 - decision makers, moderation, and rational distribution of interests
ADS1256 debugging notes based on stm32hal Library
GDAL image resampling
[matlab] solve the mex error there was a problem creating the mex file for real time execution, please ensure y
NC | progress has been made in the study of the ecological network relationship between dissolved organic carbon and microorganisms in the context of global change
Unity: text input box for numerical judgment
Moveit2 - 6. Planning scene monitor
Implementing DDD based on ABP -- domain logic and application logic
DIY can decorate the mall system, you can also have!
场景之在线人数或者粉丝查询实现
How to cancel and exit revision mode for word
Creativity: presentation of AI oil paintings with high imitation mineral pigments
Tiktok live broadcast brings goods to help the industry go to sea, and Fastdata observation industry boutique salon helps the ecological development