06-图1 列出连通集 (25分)

BFS中非STL实现

#include<cstdio>

#define N 15

int G[N][N],Nv,Ne;
bool Visited[N];

void InitVisit()
{
    for(int i=0;i<N;i++)
        Visited[i] = false;
}

void DFS(int V)
{
    Visited[V] = true;
    printf("%d ",V);
    for(int i=0;i<Nv;i++)
    {
        if(!Visited[i]&&G[V][i])
            DFS(i);
    }
}

void ListComponentsWithDFS()
{
    for(int i=0;i<Nv;i++)
    {
        if(!Visited[i])
        {
            printf("{ ");
            DFS(i);
            printf("}\n");
        }
    }
}

void BFS(int V)
{
    const int MAX_SIZE = 100;
    int Queue[MAX_SIZE];
    int first = -1,last = -1;

    Queue[++last] = V;
    Visited[V] = true;
    while(first<last)
    {
        int F = Queue[++first];
        printf("%d ",F);
        for(int i=0;i<Nv;i++)
        {
            if(G[F][i]&&!Visited[i])
            {
                Queue[++last] = i;
                Visited[i] = true;
            }
        }
    }
}

void ListComponentsWithBFS()
{
    for(int i=0;i<Nv;i++)
    {
        if(!Visited[i])
        {
            printf("{ ");
            BFS(i);
            printf("}\n");
        }
    }
}

void CreateGraph()
{
    int v1,v2;

    scanf("%d %d",&Nv,&Ne);
    for(int i=0;i<Nv;i++)
    {
        for(int j=0;j<Nv;j++)
        {
            G[i][j] = 0;
        }
    }
    for(int i=0;i<Ne;i++)
    {
        scanf("%d %d",&v1,&v2);
        G[v1][v2] = G[v2][v1] = 1;
    }
}

int main()
{
    CreateGraph();
    InitVisit();
    ListComponentsWithDFS();
    InitVisit();
    ListComponentsWithBFS();
}









对于BFS用STL实现

#include<cstdio>
#include<queue>

using namespace std;

#define N 15

int G[N][N],Nv,Ne;
bool Visited[N];

void InitVisit()
{
    for(int i=0;i<N;i++)
        Visited[i] = false;
}

void DFS(int V)
{
    Visited[V] = true;
    printf("%d ",V);
    for(int i=0;i<Nv;i++)
    {
        if(!Visited[i]&&G[V][i])
            DFS(i);
    }
}

void ListComponentsWithDFS()
{
    for(int i=0;i<Nv;i++)
    {
        if(!Visited[i])
        {
            printf("{ ");
            DFS(i);
            printf("}\n");
        }
    }
}

void BFS(int V)
{
    queue<int> Q;
    Q.push(V);
    Visited[V] = true;
    while(!Q.empty()){
        int h = Q.front();
        printf("%d ",h);
        Q.pop();
        for(int i=0;i<Nv;i++){
            if(!Visited[i]&&G[h][i]){
                Q.push(i);
                Visited[i] = true;
            }
        }
    }
}

void ListComponentsWithBFS()
{
    for(int i=0;i<Nv;i++)
    {
        if(!Visited[i])
        {
            printf("{ ");
            BFS(i);
            printf("}\n");
        }
    }
}

void CreateGraph()
{
    int v1,v2;

    scanf("%d %d",&Nv,&Ne);
    for(int i=0;i<Nv;i++)
    {
        for(int j=0;j<Nv;j++)
        {
            G[i][j] = 0;
        }
    }
    for(int i=0;i<Ne;i++)
    {
        scanf("%d %d",&v1,&v2);
        G[v1][v2] = G[v2][v1] = 1;
    }
}

int main()
{
    CreateGraph();
    InitVisit();
    ListComponentsWithDFS();
    InitVisit();
    ListComponentsWithBFS();
}









### 实现Python流星雨特效 #### 使用Turtle库创建简单流星雨效果 对于初学者来说,`turtle` 库提供了一种直观的方式来进行图形编程。下面展示了一个利用 `turtle` 创建基本流星雨动画的例子: ```python import turtle import random def draw_star(x, y): star = turtle.Turtle() star.speed(0) star.penup() star.goto(x, y) star.pendown() size = random.randint(5, 15) for _ in range(5): star.forward(size) star.backward(size) star.left(72) def create_meteor_shower(): screen = turtle.Screen() screen.bgcolor("black") while True: x = random.randint(-screen.window_width()//2, screen.window_width()//2) y = screen.window_height()//2 color = (random.random(), random.random(), random.random()) meteor = turtle.Turtle(shape="circle", visible=False) meteor.color(color) meteor.penup() meteor.setpos(x,y) meteor.showturtle() while meteor.ycor()>-(screen.window_height())//2 : meteor.right(random.uniform(-10, 10)) meteor.forward(random.uniform(8, 15)) meteor.clearstamps() if random.choice([True,False]): draw_star(meteor.xcor()+random.randint(-10,10),meteor.ycor()-random.randint(0,30)) meteor.hideturtle() meteor.reset() create_meteor_shower() ``` 这段程序定义了两个主要部分:一个是用于画星星的小函数 `draw_star()`;另一个是主循环 `create_meteor_shower()` 来模拟流星划过夜空的效果[^2]。 #### 利用Pygame构建更复杂的流星雨场景 如果想要制作更加复杂和互动性强的流星雨游戏,则可以考虑采用 `pygame` 这样的多媒体模块。这里给出一段简化版的代码片段作为起点: ```python import pygame import sys from math import sin, cos, radians import random class Meteor(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface((10, 10)) # 可替换为实际图片资源加载 self.rect = self.image.get_rect(center=(random.randrange(WIDTH), -20)) self.angle = random.uniform(radians(-45), radians(45)) self.velocity_x = int(cos(self.angle)*random.uniform(3, 9)) self.velocity_y = int(sin(self.angle)*random.uniform(3, 9)+gravity) def update(self): global score self.rect.centery += self.velocity_y self.rect.centerx += self.velocity_x if not (-20 < self.rect.top < HEIGHT or WIDTH+20 > self.rect.left > -20): meteors.remove(self) pygame.init() size = WIDTH, HEIGHT = 640, 480 screen = pygame.display.set_mode(size) clock = pygame.time.Clock() meteors = pygame.sprite.Group() gravity = 0.5 score = 0 while True: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() new_meteors_count = min(int(score / 10) + 1, 5) for i in range(new_meteors_count): meteors.add(Meteor()) meteors.update() screen.fill((0, 0, 0)) meteors.draw(screen) pygame.display.flip() ``` 此段代码展示了如何使用面向对象的方法来管理多个流星实例,并让它们按照一定规律移动并最终消失于屏幕之外[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值