多题目

(2 )(洪水填充) 现有用字符标记像素颜色的 8x8 图像。颜色填充的操作描述如下:给定起始像素的位置和待填充的颜色,将起始像素和所有可达的像素(可达的定义:经过 一次或多次的向上、下、左、右四个方向移动所能到达且终点和路径上所有像素的颜色都与起始像素颜色相同),替换为给定的颜色。

试补全程序。

01 #include <bits/stdc++.h>

02 using namespace std;

03

04 const int ROWS = 8;

05 const int COLS = 8;

06

07 struct Point {

08     int r, c;

09     Point(int r, int c) : r(r), c(c) {}

10 };

11

12 bool is_valid(char image[ROWS][COLS], Point pt,

13     int prev_color, int new_color) {

14     int r = pt.r;

15     int c = pt.c;

16     return (0 <= r && r < ROWS && 0 <= c && c < COLS &&

17     ① && image[r][c] != new_color);

18 }

19

20 void flood_fill(char image[ROWS][COLS], Point cur, int new_color) {

21     queue<Point> queue;

22     queue.push(cur);

23

24     int prev_color = image[cur.r][cur.c];

25     ② ;

26

27     while (!queue.empty()) {

28         Point pt = queue.front();

29         queue.pop();

30

31         Point points[4] = { ③ , Point(pt.r - 1, pt.c),

32         Point(pt.r, pt.c + 1), Point(pt.r, pt.c - 1)};

33         for (auto p : points) {

34             if (is_valid(image, p, prev_color, new_color)) {

35                 ④ ;

36                 ⑤ ;

37             }

38         }

39     }

40 }

41

42

第1题 单选

①处应填( )

A.

image[r][c] == prev_color

B.

image[r][c] != prev_color

C.

image[r][c] == new_color

D.

image[r][c] != new_color

第2题 单选

②处应填( )

A.

image[cur.r+1][cur.c] = new_color

B.

image[cur.r][cur.c] = new_color

C.

image[cur.r][cur.c+1] = new_color

D.

image[cur.r][cur.c] = prev_color

第3题 单选

③处应填( )

A.

Point(pt.r, pt.c)

B.

Point(pt.r, pt.c+1)

C.

Point(pt.r+1, pt.c)

D.

Point(pt.r+1, pt.c+1)

第4题 单选

④处应填( )

A.

prev_color = image[p.r][p.c]

B.

new_color = image[p.r][p.c]

C.

image[p.r][p.c] = prev_color

D.

image[p.r][p.c] = new_color

第5题 单选

⑤处应填( )

A.

queue.push( p )

B.

queue.push(pt)

C.

queue.push(cur)

D.

queue.push(Point(ROWS,COLS))

发表评论

登录 后再回复