CSP初赛复习-29-洪水填充-FloodFill-练习题

CSP初赛复习-29-洪水填充-FloodFill-练习题
PDF及答案公众号回复关键字:CSPC29

完善程序

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

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             --1-- && image[r][c]!=new_colr);                
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     --2--;
26     
27     while(!queue.empty()){
28         Point pt=queue.front();
29         queue.pop();
30         
31         Point point[4]={--3--,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                 --4--;
36                 --5--;
37             }
38         }
39     }    
40 }
41 
42 int main(){
43     char image[ROW][COLS]={{'g g g g g g g g'},
44                            {'g g g g g g r r'},
45                            {'g r r g g r g g'},
46                            {'g b b b b r g r'},
47                            {'g g g b b r g r'},
48                            {'g g g b b b b r'},
49                            {'g g g g g b g g'},
50                            {'g g g g g b b g'}};
51     
52     Point cur(4,4);
53     char new_color ='y';
54     
55     flood_fill(image,cur,new_color);
56     
57     for(int r=0;r<ROWS;r++){
58         for(int c=0;c<COLS;c++){
59             cout<<image[r][c]<<" ";
60         }
61         cout<<endl;
62     }
63     // 输出
64     // g g g g g g g g
65     // g g g g g g r r
66     // g r r g g r g g
67     // g b b b b r g r
68     // g g g b b r g r
69     // g g g b b b b r
70     // g g g g g b g g
71     // g g g g g b b g
72     
73     return 0;
74 }

1.①处应填( )

A. image[r] [c] == prew_color

B. image[r] [c] != prew_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] == prew_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. Prew_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))

posted @ 2023-08-21 22:50  new-code  阅读(186)  评论(0)    收藏  举报