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;

}

最新文章

  1. Redis链表实现
  2. JqueryDataTable的使用(.Net平台)
  3. ps还能用脚本切片?
  4. 单元测试(junit使用)
  5. Oracle 查看表空间的大小
  6. css3 --- 翻页动画 --- javascript --- 3d --- 准备
  7. Swift - Property &#39;&#39;not initialized at super.init call
  8. ☀【canvas】直线 / 三角形 / 矩形 / 曲线 / 控制点 / 变换
  9. MVC项目初次发布到IIS可能会遇到的问题
  10. haproxy nginx 多路径
  11. java 判断字符串编码
  12. HDU 1171 Big Event in HDU (多重背包)
  13. hdu 2049 别easy列(4)——测试新郎
  14. Spring新下载地址
  15. Supported method argument types Spring MVC
  16. Zabbix4.0.3解决中文乱码
  17. Go Rand小结
  18. linux下fallocate快速创建大文件
  19. etcd-v2第四集
  20. Spring Boot + Spring Cloud 构建微服务系统(七):API服务网关(Zuul)

热门文章

  1. [BZOJ1074] [luogu 4036] [JSOI 2008] 火星人 (二分答案+哈希+fhq treap)
  2. python学习shutil模块的文件压缩和解压用法
  3. H5白屏问题
  4. 2018团队项目beta阶段成果汇总
  5. 如何在SVN服务器上创建项目
  6. Vue 学习之 vue-router2
  7. tensor与数组转化
  8. 转发一个robotframework的循环
  9. bzoj4448 [Scoi2015]情报传递 主席树+树上差分
  10. django之mysql数据库的配置和orm交互