宽度优先搜索(BFS):POJ3669--Meteor Shower
来源:互联网 发布:java中重载的定义 编辑:程序博客网 时间:2024/05/10 13:24
本题题意为:陨石落下会砸坏第一象限方块,周围的四个方块也会损坏。你只能在当前完好的方块上逃离,问你能不能逃脱,最短时间是多少?显然这题类似于迷宫问题,求最短路径,一道基本广搜题。应该注意,不可随意的以为到达每个点的时间为x+y,因为像迷宫一样,路可以绕着走。所以时间必须要记录下来。此外,方块何时被损坏是一个要不断更新的过程。原题链接
#include <iostream>#include <cstring>#include <queue>#define MAX_X 302#define MAX_Y 302#define INF 0x4f4f4f4f ///int类型时4个字节,所以用4个4f,以便使用memset函数(用字节填充)using namespace std;typedef pair<int,int> P;const int xi[]={1,0,-1,0};const int yi[]={0,1,0,-1};int data[MAX_X][MAX_Y]; ///记录损坏的最先时间int used[MAX_X][MAX_Y]; ///第一次被使用的时间(记录是第几步到达的)void bfs(){ queue<P> que; used[0][0]=0; ///设原点为第0步 que.push(P(0,0)); ///加入队列第1点 while(!que.empty()){ P tmp=que.front(); int x=tmp.first,y=tmp.second; if(data[x][y]==INF){ ///找到安全点 cout<<used[x][y]<<endl; return ; } que.pop(); for(int i=0;i<4;i++){ int xx=x+xi[i],yy=y+yi[i]; if(data[xx][yy]>used[x][y]+1&&used[xx][yy]>used[x][y]+1&&xx>=0&&yy>=0){ used[xx][yy]=used[x][y]+1; ///步数加1 que.push(P(xx,yy)); } } } cout<<-1<<endl; ///已被坏的方块包围!!!}int main(){ int n; cin>>n; memset(data,0x4f,sizeof(data)); ///初始化填充数据 memset(used,0x4f,sizeof(used)); for(int i=0;i<n;i++){ ///输入数据 int x,y,t; cin>>x>>y>>t; data[x][y]=min(data[x][y],t); ///如果在data[x][y]之前已经损坏,更新自身 for(int i=0;i<4;i++) if(x+xi[i]>=0&&y+yi[i]>=0) data[x+xi[i]][y+yi[i]]=min(data[x+xi[i]][y+yi[i]],t); ///更新相邻节点 } if (data[0][0]==0) cout<<-1<<endl; else bfs(); return 0;}
0 0
- 宽度优先搜索(BFS):POJ3669--Meteor Shower
- POJ3669 Meteor Shower 广度优先搜索
- POJ3669 Meteor Shower BFS
- poj3669 Meteor Shower BFS
- POJ3669 Meteor Shower(BFS)
- POJ3669 Meteor Shower 【BFS】
- POJ3669-Meteor Shower-BFS
- poj3669 Meteor Shower bfs
- Poj3669 meteor shower bfs
- POJ3669 Meteor Shower (BFS)
- 【poj3669】Meteor Shower 搜索
- POJ3669 Meteor Shower(bfs)
- POJ3669-Meteor Shower(BFS)
- poj3669 Meteor Shower(bfs java)
- POJ3669——Meteor Shower(BFS)
- POJ3669-Meteor Shower
- POJ3669 Meteor Shower
- poj3669-Meteor Shower
- vs2010 安装mvc3
- Android开发之路(四)--AndroidStudio的快捷键
- final关键字的使用
- Java运行时内存数据区域分布
- iOS夯实:ARC时代的内存管理
- 宽度优先搜索(BFS):POJ3669--Meteor Shower
- android关机时Framework层对存储设备的操作分析
- 基于Spring session data redis的session共享
- iOS中不同形式的图文混排技术的实现总结
- Google Java编程风格规范
- c# [app.config] 找不到 "ConfigurationManager",即使添加了命名空间 using System.Configuration;
- Android加载图片导致内存溢出(Out of Memory异常)
- H265(HEVC) nal 单元头介绍及rtp发送中的fu分组发送详解
- spring-day2