POJ-3669 Meteor Shower(bfs)
http://poj.org/problem?id=3669
注意理解题意:有m颗行星将会落在方格中(第一象限),第i颗行星在ti时间会摧毁(xi,yi)这个点和四周相邻的点,一个人开始在原点,然后只能在第一象限内行走,每单位时间移动一格,只能移动到当前未摧毁的点,问多少时间能到达安全地方。
开始题意理解错了,没有明白每一颗行星最多摧毁5个点,我们可以预处理出被行星摧毁的点,然后用bfs搜索。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
//#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define maxn 1000000000
#define N 1010
using namespace std; int dir[][]={-,,,,,,,-,,};
int vis[][];
struct node
{
int x,y,time;
}; int bfs()
{
if(vis[][]==-) return ;
node p;
p.x=p.y=p.time=;
queue<node>que;
que.push(p);
while(que.size())
{
node q=que.front();
que.pop();
for(int i=;i<;i++)
{
p=q;
p.x=q.x+dir[i][];
p.y=q.y+dir[i][];
p.time++;
if(p.x<||p.y<) continue; //出界了
if(vis[p.x][p.y]==-) return p.time; //到达安全地方
if(p.time<vis[p.x][p.y]) //当前这个点还未摧毁
{
vis[p.x][p.y]=p.time; //必须赋值,不然会超时,这样可以防止往回走的情况,节省不必要的搜索
que.push(p);
}
}
}
return -;
}
int main()
{
//freopen("a.txt","r",stdin);
int m,x,y,t;
scanf("%d",&m);
memset(vis,-,sizeof(vis));
while(m--)
{
scanf("%d%d%d",&x,&y,&t);
for(int i=;i<;i++)
{
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx>=&&yy>=) //预处理所有被摧毁的点,并赋值为被摧毁的最小时间。
{
if(vis[xx][yy]==-) vis[xx][yy]=t;
else vis[xx][yy]=min(t,vis[xx][yy]);
}
}
}
if(vis[][]==) printf("-1\n");
else printf("%d\n",bfs());
return ;
}
POJ-3669 Meteor Shower(bfs)的更多相关文章
- POJ 3669 Meteor Shower BFS求最小时间
Meteor Shower Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31358 Accepted: 8064 De ...
- POJ 3669 Meteor Shower BFS 水~
http://poj.org/problem?id=3669 题目大意: 一个人从(0,0)出发,这个地方会落下陨石,当陨石落在(x,y)时,会把(x,y)这个地方和相邻的的四个地方破坏掉,求该人到达 ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- 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+预处理)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- poj 3669 Meteor Shower
Me ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- poj3669 Meteor Shower(BFS)
题目链接:poj3669 Meteor Shower 我只想说这题WA了后去看讨论才发现的坑点,除了要注意原点外,流星范围题目给的是[0,300],到302的位置就绝对安全了... #include& ...
随机推荐
- 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏
#include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...
- CSS3:线上编辑工具及实用资料整理
an I Use 个人最常用的,资料比较全,桌面和移动浏览器支持HTML5,CSS3,SVG和兼容性表. 官网地址:http://caniuse.com/ CSS3 Click Chart CSS3 ...
- Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- acdream1116 Gao the string!(hash二分 or 后缀数组)
问题套了一个斐波那契数,归根结底就是要求对于所有后缀s[i...n-1],所有前缀在其中出现的总次数.我一开始做的时候想了好久,后来看了别人的解法才恍然大悟.对于一个后缀来说 s[i...n-1]来说 ...
- JSONObject 包的依赖
commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json- ...
- Linux网络编程7——使用TCP实现双方聊天
思路 主线程负责发送消息,另一线程负责接收消息.服务端和客户端均是如此. 注意 当A方close掉用于通信的socket端口后,该端口是不会立即关闭的.因为此时可能B方的信息还没send完.因此,此时 ...
- 使用secureCRT远程Linux,出现远程主机拒绝连接
1.查看是否开启远程连接, 控制面板->程序和功能->打开或关闭windows功能->勾选telnet服务器和telnet客户端2.cmd命令行输入telnet ip地址 端口号(比 ...
- http://my.oschina.net/u/719192/blog/506062?p={{page}}
http://my.oschina.net/u/719192/blog/506062?p={{page}}
- Spring REST for DELETE Request Method Not Supoorted
http://stackoverflow.com/questions/22055251/sending-data-with-angularjs-http-delete-request I have a ...
- Android核心分析之二十七Android GDI 之SurfaceFlinger之动态结构示
SurfaceFlinger对象建立过程示意 1 SurfaceSession的建立 客户端请求建立Surface时,首先在要与SurfaceFlinger建立一个Session,然后再 ...