Meteor Shower(POJ 3669)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12816 | Accepted: 3451 |
Description
Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.
The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.
Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).
Determine the minimum time it takes Bessie to get to a safe place.
Input
* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
Output
* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
Sample Input
4
0 0 2
2 1 2
1 1 2
0 3 5
Sample Output
5
题意思路:m个陨石将砸向第一象限和正坐标抽,每块陨石砸的时间分别为Ti,从0,0出发,如何走才能达到安全位置;
用一个T[][]数组记录每个点的最早爆炸时间,vis标记该点是否被砸,如果该点不可能被砸,那么为安全位置,否则向四个方向扩展,改点可走的条件为改点没有走过以及该点爆炸的时间大于当前时间,对于走过的点标记T[][]为0,那么改点在以后就不可能再走。 反正我的思路好麻烦,每次都要写个函数对所有陨石进行遍历判断是否为安全位置。
AC:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (50000+1)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Node{
int x, y, step;
};
bool judge(int x, int y){
return x >= && y >= ;
}
bool vis[][];
int Move[][] = {,, ,-, ,, -,};
int T[][];
int BFS(int x, int y)
{
if(!vis[x][y])
return ;
queue<Node> Q;
Node now, next;
now.x = x; now.y = y; now.step = ;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(!vis[now.x][now.y])
return now.step;
for(int k = ; k < ; k++)
{
next.x = now.x + Move[k][];
next.y = now.y + Move[k][];
next.step = now.step + ;
if(!judge(next.x, next.y)) continue;
if(vis[next.x][next.y])
{
if(T[next.x][next.y] != INF && next.step < T[next.x][next.y])
{
T[next.x][next.y] = ;
Q.push(next);
}
}
else
return next.step;
}
}
return -;
}
int main()
{
int n;
while(Ri(n) != EOF)
{
CLR(vis, false); CLR(T, INF);
for(int i = ; i < n; i++)
{
int x, y, t;
Ri(x); Ri(y); Ri(t);
T[x][y] = min(T[x][y], t);
vis[x][y] = true;
for(int k = ; k < ; k++)
{
int xx = x + Move[k][];
int yy = y + Move[k][];
if(!judge(xx, yy)) continue;
vis[xx][yy] = true;
T[xx][yy] = min(T[xx][yy], t);
}
}
Pi(BFS(, ));
}
return ;
}
WA:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int vis[][],b[][];
struct node {int x,y,t;}m[];
bool cmp(node a,node b) {return a.t<=b.t;}
int n,Mint;
int dx[]={,,,,-},dy[]={,,-,,};
bool safe(node p)
{
int l=-,r=n,mid;
while(r-l>)
{
mid=(l+r)/;
if(m[mid].t>=p.t)
r=mid;
else
l=mid;
}
for(int i=r;i<n;i++)
{
for(int j=;j<;j++)
{
if(p.x==(m[i].x+dx[j])&&p.y==(m[i].y+dy[j]))
return ;
}
}
return ;
}
int bfs()
{
int i,j;
Mint=-;
queue<node> s;
node temp,next;
temp.x=temp.y=temp.t=;
s.push(temp);
while(!s.empty())
{
temp=s.front();
s.pop();
if(safe(temp))
{
Mint=temp.t;
break;
}
temp.t++;
int l=-,r=n,mid;
while(r-l>)
{
mid=(l+r)/;
if(m[mid].t>=temp.t)
r=mid;
else
l=mid;
}
while(m[r].t==temp.t)
{ for(int i=;i<=;i++)
b[m[r].x+dx[i]][m[r].y+dy[i]]=;
r++;
}
for(i=;i<=;i++)
{
next.x=temp.x+dx[i];
next.y=temp.y+dy[i];
next.t=temp.t;
if(next.x>=&&next.y>=&&vis[next.x][next.y]==&&b[next.x][next.y]==)
{
vis[next.x][next.y]=;
s.push(next);
}
//cout<<"ads";
}
}
return Mint;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
memset(vis,,sizeof(vis));
memset(b,,sizeof(vis));
for(i=;i<n;i++)
scanf("%d%d%d",&m[i].x,&m[i].y,&m[i].t);
sort(m,m+n,cmp);
//for(i=0;i<n;i++)
// cout<<m[i].x<<" "<<m[i y<<" "<<m[i].t<<endl;
i=;
bool flag=;
while(m[i].t==)
{
if((m[i].x==&&m[i].y==)||(m[i].x==&&m[i].y==)||(m[i].x==&&m[i].y==))
{
cout<<-<<endl;
flag=;
break;
}
i++;
}
if(flag)
continue;
bfs();
printf("%d\n",Mint);
}
}
Meteor Shower(POJ 3669)的更多相关文章
- POJ 3669 Meteor Shower (BFS+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- poj3669 Meteor Shower(预处理+bfs)
https://vjudge.net/problem/POJ-3669 先给地图a[][]预处理每个位置被砸的最小时间.然后再bfs. 纯bfs,还被cin卡了下时间.. #include<io ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 【POJ - 3669】Meteor Shower(bfs)
-->Meteor Shower Descriptions: Bessie听说有场史无前例的流星雨即将来临:有谶言:陨星将落,徒留灰烬.为保生机,她誓将找寻安全之所(永避星坠之地).目前她正在平 ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
- 广大暑假训练1(poj 2488) A Knight's Journey 解题报告
题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A (A - Children of the Candy Corn) ht ...
随机推荐
- Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(一)
在数据库有外键的时候,使用 select_related() 和 prefetch_related() 可以很好的减少数据库请求的次数,从而提高性能.本文通过一个简单的例子详解这两个函数的作用.虽然Q ...
- DataTables获取表单输入框数据
$(document).ready(function() { var table = $('#example').DataTable(); $('button').click(function() { ...
- Google机器学习笔记(七)TF.Learn 手写文字识别
转载请注明作者:梦里风林 Google Machine Learning Recipes 7 官方中文博客 - 视频地址 Github工程地址 https://github.com/ahangchen ...
- UIWebView与JavaScript(JS) 回调交互 -备
很多关于objc 与 js 交互的文章都比较适用于 mac开发,iOS的webview 还是有所不一样, 参考:http://blog.sina.com.cn/s/blog_693de6100102v ...
- DJANGO用户名认证一例
现在实例了用户登陆,就自带的功能.. urls.py ~~~~~~~~~~ (r'^login/$', login), (r'^logout/$',logout,{'next_page':'/logi ...
- yii第一个应用blog
1. 连接到数据库 大多数 Web 应用由数据库驱动,我们的测试应用也不例外.要使用数据库,我们首先需要告诉应用如何连接它.修改应用的配置文件 WebRoot/testdrive/protected/ ...
- 【转】UltraISO制作U盘启动盘安装Win7/9/10系统攻略
U盘安装好处就是不用使用笨拙的光盘,光盘还容易出现问题,无法读取的问题.U盘体积小,携带方便,随时都可以制作系统启动盘. U盘建议选择8G及以上大小的. 下面一步一步讲解如果制作U盘安装盘: 1.首先 ...
- Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum
这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...
- IOS开发错误提示原因集合-----长期更新
"[__NSCFConstantString size]: unrecognized selector sent to instance." =>将NSString类型的参数 ...
- bwlabel函数的c++实现
实验中需要用到区域联通的算法,就是类似于matlab中bwlabel的函数.网上找了找c++源码未果,bwlabel-python版用python描述了matlab中的实现方法,但是最后对标签的处理部 ...