From b2f46d0a3fe1f479a805b96961ac449b96ac8630 Mon Sep 17 00:00:00 2001 From: Celia_Wu <530081999@qq.com> Date: Tue, 28 May 2019 22:42:32 +0800 Subject: [PATCH] 2019-05-28 --- ...76\345\203\217\346\270\262\346\237\223.py" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "0733.\345\233\276\345\203\217\346\270\262\346\237\223/0733-\345\233\276\345\203\217\346\270\262\346\237\223.py" diff --git "a/0733.\345\233\276\345\203\217\346\270\262\346\237\223/0733-\345\233\276\345\203\217\346\270\262\346\237\223.py" "b/0733.\345\233\276\345\203\217\346\270\262\346\237\223/0733-\345\233\276\345\203\217\346\270\262\346\237\223.py" new file mode 100644 index 0000000..429ca08 --- /dev/null +++ "b/0733.\345\233\276\345\203\217\346\270\262\346\237\223/0733-\345\233\276\345\203\217\346\270\262\346\237\223.py" @@ -0,0 +1,32 @@ +from collections import deque +class Solution(object): + def floodFill(self, image, sr, sc, newColor): + """ + :type image: List[List[int]] + :type sr: int + :type sc: int + :type newColor: int + :rtype: List[List[int]] + """ + m, n = len(image), len(image[0]) + color = image[sr][sc] + image[sr][sc] = newColor + + visited = [[0 for _ in range(n + 1)] for _ in range(m + 1)] + dx = [1, -1, 0, 0] + dy = [0, 0, 1, -1] + + q = deque() + q.append([sr,sc]) + while q: + x0, y0 = q.popleft() + for k in range(4): + x = x0 + dx[k] + y = y0 + dy[k] + + if 0 <= x < m and 0 <= y < n and image[x][y] == color and visited[x][y] == 0: + image[x][y] = newColor + visited[x][y] = 1 + q.append([x, y]) + + return image \ No newline at end of file