当前位置:网站首页>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;
}
};
边栏推荐
- 144. Preorder traversal of binary tree
- Homologous strategy, surface longitude
- Execution flow control of shell
- Moveit2 - 8. Motion planning API
- Chapter 3 business function development (modify the remarks of market activities)
- Wechat applet authorized login (including obtaining basic information and binding mobile number)
- Advantages and disadvantages of zero trust security
- Server status code
- The difference between apply, call and bind
- Installation and tutorial of MATLAB curling simulation game
猜你喜欢

Cluster clock synchronization configuration

Fiddler grabs packets and displays err_ TUNNEL_ CONNECTION_ FAILED
![[Flink] submit the jar package to the Flink cluster and run it](/img/9b/076949a4b9fe75ee248c400ee21474.png)
[Flink] submit the jar package to the Flink cluster and run it

High temperature in Britain: two airport runways were burnt out, and several railways were restricted to ensure safety

Student thinking vs workplace thinking

Traditional architecture of enterprise digital transformation to medium platform micro service architecture

292. Nim game

Servlet个人实操笔记(一)

Skywalking distributed link tracking, related graphics, DLJD, cat

Machine learning exercise 8 - anomaly detection and recommendation system (collaborative filtering)
随机推荐
Homologous strategy, surface longitude
[kaggle] how to effectively avoid oom and the long process of alchemy
Force button brushing question 61. rotating linked list
Debezium series: Show slave status view the possible different situations of master-slave delay
ES (8.1) certification topic
Xrrunner, a domestic performance testing tool for palm smart, officially unveiled qecon
[Flink] transform operator filter
Niuke interview high frequency list (group 1) difficulty: simple & medium
Hw2021 attack and defense drill experience - Insights
Huawei od maximum consumption JS
ADS1256 debugging notes based on stm32hal Library
Advantages and disadvantages of zero trust security
Optimize bubble sorting
01_ Education 1
Unity word document click button to download
Max matrix and JS
Yuntu says digital asset chain: your God of digital asset property protection
Advanced numbers | [calculus of multivariate function] chain derivation method, implicit function derivation method, total differential form invariance, multivariate function extremum
ECCV 2022 | rethinking image blending for data enhancement in vision transformers
How should enterprise users choose aiops or APM?