流星雨

题目

Q:看到题目可能会疑惑变砸变跑怎么实现呢?
A:所以我们要在bfs之前初始化好,陨石砸下来的位置,然后从起始点进行bfs

Q:怎么进行bfs呢?
A:从起始点开始向四周便利,然后在开始统计的数组中这个时候土地还是完好的那么考虑移动,如果这个点一直不会被砸到,那么就可以输出答案了


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<bits/stdc++.h>
using namespace std;
int a[350][350],m;
int num[350][350],vis[350][350],ans=-1;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
inline int read()
{
    int res=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch)){res=(res<<1)+(res<<3)+(ch&15);ch=getchar();}
    return res*f;
}
struct node
{
    int x;
    int y;
};
queue<node>q;
void bfs()
{
    vis[0][0]=1;
    node st;st.x=0,st.y=0;
    q.push(st);
    while(!q.empty())
    {
        node k=q.front();q.pop();
 
        if(a[k.x][k.y]>1000)
        {
            ans=num[k.x][k.y];
            break;
        }
        for(int i=0;i<4;i++)
        {
           int xx=k.x+dx[i];
           int yy=k.y+dy[i];
           if(xx>=0&&xx<=320&&yy>=0&&yy<=320)
           {
            if(!vis[xx][yy])
            {
                if(a[xx][yy]>num[k.x][k.y]+1)
                {
                    num[xx][yy]=num[k.x][k.y]+1;
                    vis[xx][yy]=1;
                    node ne;ne.x=xx;ne.y=yy;
                    q.push(ne);
                }
               }
           }
        }
    }
}
 
int main()
{
    m=read();
    memset(a,1e6,sizeof(a));
    for(int i=1;i<=m;i++)
    {
        int x,y,z;x=read();y=read();z=read();
        a[x][y]=min(z,a[x][y]);
        for(int k=0;k<4;k++)//向四周初始化
        if(x+dx[k]>=0&&y+dy[k]>=0)
        a[x+dx[k]][y+dy[k]]=min(a[x+dx[k]][y+dy[k]],z);
    }
    bfs();
    printf("%d\n",ans);
    return 0;
}
JavaScript流星雨是一种通过在网页上创建动画效果来模拟流星雨的技术。它通常使用HTML5的Canvas元素和JavaScript编程语言来实现。 实现JavaScript流星雨的基本步骤如下: 1. 创建一个HTML页面,并在其中添加一个Canvas元素,用于绘制动画效果。 2. 使用JavaScript获取Canvas元素的上下文对象,以便在Canvas上进行绘制。 3. 定义一个流星对象,包含流星的起始位置、速度、长度、颜色等属性。 4. 使用定时器或动画循环,在Canvas上不断绘制流星对象,并更新其位置。 5. 当流星对象超出Canvas范围时,重新生成一个新的流星对象,以实现连续不断的流星效果。 以下是一个简单的JavaScript流星雨的示例代码: ```html <!DOCTYPE html> <html> <head> <title>JavaScript流星雨</title> <style> body { margin: 0; overflow: hidden; } canvas { display: block; } </style> </head> <body> <canvas id="canvas"></canvas> <script> // 获取Canvas元素 var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); // 设置Canvas大小 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 定义流星对象 function Meteor(x, y, speed, length, color) { this.x = x; this.y = y; this.speed = speed; this.length = length; this.color = color; } // 绘制流星 Meteor.prototype.draw = function() { ctx.beginPath(); ctx.moveTo(this.x, this.y); ctx.lineTo(this.x + this.length, this.y - this.length); ctx.strokeStyle = this.color; ctx.stroke(); } // 更新流星位置 Meteor.prototype.update = function() { this.x -= this.speed; this.y += this.speed; } // 创建流星对象 function createMeteor() { var x = Math.random() * canvas.width; var y = 0; var speed = Math.random() * 5 + 1; var length = Math.random() * 50 + 10; var color = "white"; return new Meteor(x, y, speed, length, color); } // 存储流星对象的数组 var meteors = []; // 绘制和更新流星 function drawMeteors() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < meteors.length; i++) { var meteor = meteors[i]; meteor.draw(); meteor.update(); if (meteor.x < -meteor.length || meteor.y > canvas.height + meteor.length) { meteors.splice(i, 1); i--; } } } // 添加新的流星到数组中 function addMeteor() { meteors.push(createMeteor()); } // 定时器,每隔一段时间添加新的流星并绘制更新已有的流星 setInterval(function() { addMeteor(); drawMeteors(); }, 100); // 监听窗口大小变化,重新设置Canvas大小 window.addEventListener("resize", function() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }); </script> </body> </html> ``` 这段代码会在网页上创建一个全屏的Canvas元素,并在其中模拟流星雨效果。每隔一段时间,会添加新的流星对象,并绘制更新已有的流星对象,实现流星雨的动画效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值