信息学奥赛初赛天天练-30CSP-J2022完善程序-结构体构造函数初始化、auto关键字、连通块、洪水填充算法实战

PDF文档公众号回复关键字:20240620

在这里插入图片描述

2022 CSP-J 阅读程序2

完善程序 (单选题 ,每小题3分,共30分)

2 (洪水填充) 现有用字符标记像素颜色的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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值