【LeetCode】731. 影象渲染
阿新 • • 發佈:2020-08-16
題目連結
733. 影象渲染
題目描述
有一幅以二維整數陣列表示的圖畫,每一個整數表示該圖畫的畫素值大小,數值在 0 到 65535 之間。
給你一個座標 (sr, sc) 表示影象渲染開始的畫素值(行 ,列)和一個新的顏色值 newColor,讓你重新上色這幅影象。
為了完成上色工作,從初始座標開始,記錄初始座標的上下左右四個方向上畫素值與初始座標相同的相連畫素點,接著再記錄這四個方向上符合條件的畫素點與他們對應四個方向上畫素值與初始座標相同的相連畫素點,……,重複該過程。將所有有記錄的畫素點的顏色值改為新的顏色值。
最後返回經過上色渲染後的影象。
示例 1: 輸入: image = [[1,1,1],[1,1,0],[1,0,1]] sr = 1, sc = 1, newColor = 2 輸出: [[2,2,2],[2,2,0],[2,0,1]] 解析: 在影象的正中間,(座標(sr,sc)=(1,1)), 在路徑上所有符合條件的畫素點的顏色都被更改成2。 注意,右下角的畫素沒有更改為2, 因為它不是在上下左右四個方向上與初始點相連的畫素點。 注意: image 和 image[0] 的長度在範圍 [1, 50] 內。 給出的初始點將滿足 0 <= sr < image.length 和 0 <= sc < image[0].length。 image[i][j] 和 newColor 表示的顏色值在範圍 [0, 65535]內。
解題思路
典型DFS+BFS,
DFS:遞迴完成,
BFS:利用佇列,
雖然這題真的很簡單,但是在動手實現的過程中,我還是踩了非常多的坑,所以想記錄一下!
AC程式碼
(1)DFS
class Solution { int dir[][]={{0,1},{0,-1},{1,0},{-1,0}}; void dfs(int x,int y,int[][] image,int newColor,int color,int[][] viewr){ if(x < 0 || x >= image.length || y < 0 || y >= image[0].length) return; if(image[x][y] != color) return; if(viewr[x][y] == -1) return; image[x][y] = newColor; viewr[x][y] = -1; // dfs(x+1,y,image,newColor,color); // dfs(x-1,y,image,newColor,color); // dfs(x,y+1,image,newColor,color); // dfs(x,y-1,image,newColor,color); for(int i = 0; i < 4; i++){ int xx = x + dir[i][0]; int yy = y + dir[i][1]; dfs(xx,yy,image,newColor,color,viewr); } } public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { int color = image[sr][sc]; int viewr[][] = new int[image.length][image[0].length]; dfs(sr,sc,image,newColor,color,viewr); return image; } }
(2)DFS(空間優化)
在(1)解法中,我開了一個和image陣列一樣的viewr陣列用於記錄點(x,y)是否被訪問過,其實可以利用回溯思想省去這部分的開銷。
class Solution { int dir[][]={{0,1},{0,-1},{1,0},{-1,0}}; void dfs(int x,int y,int[][] image,int newColor,int color){ if(x < 0 || x >= image.length || y < 0 || y >= image[0].length) return; if(image[x][y] != color) return; if(image[x][y] == -1) return; image[x][y] = -1; // dfs(x+1,y,image,newColor,color); // dfs(x-1,y,image,newColor,color); // dfs(x,y+1,image,newColor,color); // dfs(x,y-1,image,newColor,color); for(int i = 0; i < 4; i++){ int xx = x + dir[i][0]; int yy = y + dir[i][1]; dfs(xx,yy,image,newColor,color); } ------------------------------------------------------------------------------ image[x][y] = newColor; //有點回溯的思想,通過遞迴樹和棧就能夠理解了!!相當於使用一個特殊值 -1 代替 visited 陣列的作用,達到不走回頭路的效果 --------------------------------------------------------------------------------- } public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { int color = image[sr][sc]; int viewr[][] = new int[image.length][image[0].length]; dfs(sr,sc,image,newColor,color); return image; } }
(3)BFS
class Solution {
Queue<int[]> q = new LinkedList<>();//利用陣列來存座標x,y是我一開始沒想到的。
int dir[][] = {{0,1},{0,-1},{-1,0},{1,0}};
void bfs(int[][] image, int x, int y, int newColor,int oldcolor,int[][] viewer){
int[] buf = {x,y};
q.offer(buf);
while(!q.isEmpty()){
int[] arr = q.peek();
for(int i = 0; i < 4; i++){
int xx = arr[0] + dir[i][0];
int yy = arr[1] + dir[i][1];
if(xx>=0&&xx<image.length&&yy>=0&&yy<image[0].length&&viewer[xx][yy]==0&&image[xx][yy]==oldcolor){
int[] temp = {xx,yy};
q.offer(temp);
viewer[xx][yy] = 1;
}
}
viewer[arr[0]][arr[1]] = 1;
image[arr[0]][arr[1]] = newColor;
q.poll();
}
}
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int[][] viewer = new int[image.length][image[0].length];//viewer用來記錄點x,y是否訪問過。
int oldcolor = image[sr][sc];
bfs(image,sr,sc,newColor,oldcolor,viewer);
return image;
}
}