首页 技术 正文
技术 2022年11月12日
0 收藏 972 点赞 3,007 浏览 1654 个字

Description – 题目描述

Bessie听说有场史无前例的流星雨即将来临;有谶言:陨星将落,徒留灰烬。为保生机,她誓将找寻安全之所(永避星坠之地)。目前她正在平面坐标系的原点放牧,打算在群星断其生路前转移至安全地点。

此次共有M (1 ≤ M ≤ 50,000)颗流星来袭,流星i将在时间点Ti (0 ≤ Ti ≤ 1,000) 袭击点 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300)。每颗流星都将摧毁落点及其相邻四点的区域。

Bessie在0时刻时处于原点,且只能行于第一象限,以平行与坐标轴每秒一个单位长度的速度奔走于未被毁坏的相邻(通常为4)点上。在某点被摧毁的刹那及其往后的时刻,她都无法进入该点。

寻找Bessie到达安全地点所需的最短时间。

Input – 输入

  • 第1行: 一个整数: M
  • 第2..M+1行: 第i+1行包含由空格分隔的三个整数: Xi, Yi, and Ti

    Output – 输出

  • 仅一行: Bessie寻得安全点所花费的最短时间,无解则为-1。

Sample Input – 输入样例

4

0 0 2

2 1 2

1 1 2

0 3 5

Sample Output – 输出样例

5

AC代码:

include

include

include

include

include

using namespace std;

int a[310][310]; //标记每个点的状态,刚开始初始化后上面的数字代表最早在第几秒该点就已不安全

int M,j,plug=0,d[][2]={{0,1},{0,-1},{1,0},{-1,0},{0,0}}; //在初始化a[][]数组与每次移动的情况这两种情况时使用

struct node {

int x,y,t;

}s[50005];

struct noded {

int x1,y1,len;

};

queuequ;

void init(int x,int y,int m);

int cmp(const node &a,const node &b);

int bfs(){

while(!qu.empty()){

struct noded e=qu.front();

qu.pop();

for(int i=0;i<4;i++){

int nx=e.x1+d[i][0],ny=e.y1+d[i][1];

if(nx<0||ny<0) continue;

if(a[nx][ny]==-1) {

printf("%d\n",e.len+1);

return 0;

}

if(a[nx][ny]>e.len+1) {

a[nx][ny]=e.len+1; //这一步很重要,没有则是超时,有的话时间可以控制在100ms左右,这一步的意义就是走过的不再重复的走

struct noded f;

f.x1=nx,f.y1=ny,f.len=e.len+1;

qu.push(f);

}

}
}
printf("-1\n");

}

int main(){

cin>>M;

memset(a,-1,sizeof(a)); //初始化为-1,在输入数据之后若仍未-1,则该点为安全点

for(int i=0;i<M;i++){

scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].t);

init(s[i].x,s[i].y,s[i].t); //五个位置不再是绝对安全

}

sort(s,s+M,cmp); //对其进行排序,方便bfs一步一步执行

struct noded e;

e.x1=0,e.y1=0,e.len=0;

qu.push(e);

bfs();

return 0;

}

void init(int x,int y,int m){ //初始化五个点不再绝对安全

for(int i=0;i<5;i++){

int nx=x+d[i][0],ny=y+d[i][1];

if(nx>=0&&ny>=0&&nx<=300&&ny<=300){

if(a[nx][ny]==-1) a[nx][ny]=m;

else a[nx][ny]=min(m,a[nx][ny]);

}

else continue;

}

}

int cmp(const node &a,const node &b){ //sort()比较函数

return a.t<b.t;

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,468
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,096
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765