poj3669
来源:互联网 发布:大尺度网络电影 编辑:程序博客网 时间:2024/04/27 19:41
#include <iostream>
#include <stdio.h>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
#define MAX 400
#define INF 0x3f3f3f3f
struct St //结构体
{
int x,y;
St(){}
St(int _x,int _y) : x(_x),y(_y){}
};
int n,time[MAX][MAX],danger[MAX][MAX],
dx[5]={-1,0,1,0,0}, //把原地不动的坐标放在最后
dy[5]={0,-1,0,1,0};
void bfs()
{
queue<St> que;
if (danger[0][0]==0) {cout<<"-1";return;}
memset(time,0x3f,sizeof(time)); //初始每个方块要很久才会走到
time[0][0]=0; //对起点初始化
que.push(St(0,0)); //起点入队
while (!que.empty()) //队列不为空时
{
St now;
now=que.front(); //取出队首
if (danger[now.x][now.y]==INF) break; //到达目标则break,ans在队首
que.pop(); //弹出队首
for (int j=0;j<4;j++) //穷举各个方向,不包括原地不动的情况
{
int x1,y1;
x1=now.x+dx[j];y1=now.y+dy[j];
if (x1>=0&&y1>=0&& //判断条件:不出界,之前没走到过,没有被破坏
time[x1][y1]>time[now.x][now.y]+1&&
danger[x1][y1]>time[now.x][now.y]+1)
{
time[x1][y1]=time[now.x][now.y]+1; //标记
que.push(St(x1,y1)); //入队
}
}
}
if (que.empty()) cout<<"-1";
else
{
St now;
now=que.front();
cout<<time[now.x][now.y];
}
}
int main()
{
freopen("in.txt","r",stdin);
cin>>n;
memset(danger,0x3f,sizeof(danger)); //初始每个方块会在很久之后被砸
for (int i=1;i<=n;i++)
{
int x,y,t;
cin>>x>>y>>t;
for (int j=0;j<5;j++)
{
int x1,y1;
x1=x+dx[j];y1=y+dy[j];
if (x1>=0&&y1>=0)
if (danger[x1][y1]>t) danger[x1][y1]=t; //记录每个方块第一次被砸的时间
}
}
bfs();
return 0;
}
/*题目大意:
一个人在地上躲陨石。用坐标轴的第一象限表示他的位置。
初始时刻他位于坐标轴原点。单位时间内他只能移动一格。
有很多块陨石在不同时刻砸下来,陨石砸的时候会把上下左右和中间的格子砸坏。
格子一旦砸坏了就不能再经过了。
问要怎么走才能在最短时间内走到一个安全地点---陨石不会落下的地方。
*/
#include <stdio.h>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
#define MAX 400
#define INF 0x3f3f3f3f
struct St //结构体
{
int x,y;
St(){}
St(int _x,int _y) : x(_x),y(_y){}
};
int n,time[MAX][MAX],danger[MAX][MAX],
dx[5]={-1,0,1,0,0}, //把原地不动的坐标放在最后
dy[5]={0,-1,0,1,0};
void bfs()
{
queue<St> que;
if (danger[0][0]==0) {cout<<"-1";return;}
memset(time,0x3f,sizeof(time)); //初始每个方块要很久才会走到
time[0][0]=0; //对起点初始化
que.push(St(0,0)); //起点入队
while (!que.empty()) //队列不为空时
{
St now;
now=que.front(); //取出队首
if (danger[now.x][now.y]==INF) break; //到达目标则break,ans在队首
que.pop(); //弹出队首
for (int j=0;j<4;j++) //穷举各个方向,不包括原地不动的情况
{
int x1,y1;
x1=now.x+dx[j];y1=now.y+dy[j];
if (x1>=0&&y1>=0&& //判断条件:不出界,之前没走到过,没有被破坏
time[x1][y1]>time[now.x][now.y]+1&&
danger[x1][y1]>time[now.x][now.y]+1)
{
time[x1][y1]=time[now.x][now.y]+1; //标记
que.push(St(x1,y1)); //入队
}
}
}
if (que.empty()) cout<<"-1";
else
{
St now;
now=que.front();
cout<<time[now.x][now.y];
}
}
int main()
{
freopen("in.txt","r",stdin);
cin>>n;
memset(danger,0x3f,sizeof(danger)); //初始每个方块会在很久之后被砸
for (int i=1;i<=n;i++)
{
int x,y,t;
cin>>x>>y>>t;
for (int j=0;j<5;j++)
{
int x1,y1;
x1=x+dx[j];y1=y+dy[j];
if (x1>=0&&y1>=0)
if (danger[x1][y1]>t) danger[x1][y1]=t; //记录每个方块第一次被砸的时间
}
}
bfs();
return 0;
}
/*题目大意:
一个人在地上躲陨石。用坐标轴的第一象限表示他的位置。
初始时刻他位于坐标轴原点。单位时间内他只能移动一格。
有很多块陨石在不同时刻砸下来,陨石砸的时候会把上下左右和中间的格子砸坏。
格子一旦砸坏了就不能再经过了。
问要怎么走才能在最短时间内走到一个安全地点---陨石不会落下的地方。
*/
0 0
- POJ3669
- poj3669
- poj3669
- poj3669
- poj3669
- poj3669
- poj3669
- POJ3669
- poj3669
- bfs--poj3669
- BFS POJ3669
- poj3669 BFS
- poj3669 流星雨
- poj3669【bfs】
- poj3669 bfs
- poj3669(bfs)
- POJ3669 Meteor Shower BFS
- poj3669 Meteor Shower BFS
- poj3009
- Base64
- arguments实参集合与局部变量,参数关系
- Ubuntu Linux中开启MySQL远程访问功能
- Android 模拟圆形水杯倒水的效果
- poj3669
- 将Rsyslog的日志输出到Kafka消息队列
- BZOJ 1452 [JSOI2009]Count 二维树状数组
- poj3126
- Android开发使用LogCat,LogCat常见问题
- linux的3个查找命令
- 于庆玲2014134029
- uva 10163 - Storage Keeperst
- 数据库增删改查