POJ 3669 Meteor Shower(BFS)
来源:互联网 发布:知乎 教育培训 编辑:程序博客网 时间:2024/05/17 00:04
题目链接:POJ3669
一道BFS算法题,不过有很多坑
1. 第一象限(然而却是包括了两条坐标轴上的点
2. 可以留在原地不动,但不能走回头路(没必要)
3. 虽然很多人都说范围有问题,但题面上给出的是流星砸落的范围,所以出现302并没有问题(emmm
4. 一个坐标可以被砸很多次2333
5. 原点可能在time=0的时候就被砸了(哭
关于算法
首先处理坐标,记录每一个坐标被摧毁的(最早)时间
从原点开始使用bfs,入队条件就是当前要前往的点的被摧毁时间-1>当前时间
bfs结束条件即到达了一个不可能被摧毁的点
否则到队列为空时输出-1
AC代码如下
#include <iostream>#include <cstring>#include <queue>using namespace std;#define INF 1005#define MAXN 310int mapp[MAXN][MAXN];int visited[MAXN][MAXN];struct position{ int x, y; position operator+(const position& o) { position p; p.x = x + o.x; p.y = y + o.y; return p; } position(int a=0,int b=0):x(a), y(b) {}};position cur;position direction[5];int bfs(){ int cnt = 0; if(mapp[0][0]==0) return -1; queue<position> que; que.push(position(0,0)); while(!que.empty()) { int s = que.size(); for(int t=1; t<=s; ++t) { position p = que.front(); que.pop(); if(mapp[p.x][p.y]==INF) return cnt; position temp; for(int i=0; i<=4; ++i) { temp = p + direction[i]; if(temp.x>=0&&temp.y>=0&&!visited[temp.x][temp.y]&&mapp[temp.x][temp.y]>cnt+1) { visited[temp.x][temp.y] = 1; que.push(temp); } } } cnt++; } return -1;}int main(){ ios::sync_with_stdio(0); cin.tie(0); int M; direction[2].x = direction[3].y = 1; direction[1].x = direction[4].y = -1; while(cin>>M) { memset(visited,0,sizeof(visited)); for(int i=0; i<MAXN; ++i) fill(mapp[i],mapp[i]+MAXN,INF); for(int i=1; i<=M; ++i) { int x, y, t; cin>>x>>y>>t; mapp[x][y] = min(t,mapp[x][y]); if(x>0) mapp[x-1][y] = min(t,mapp[x-1][y]); mapp[x+1][y] = min(t,mapp[x+1][y]); if(y>0) mapp[x][y-1] = min(t,mapp[x][y-1]); mapp[x][y+1] = min(t,mapp[x][y+1]); } cout<<bfs()<<endl; } return 0;}
阅读全文
0 0
- POJ 3669 Meteor Shower (BFS)
- POJ 3669 Meteor Shower (BFS + 预处理)
- POJ 3669 Meteor Shower (bfs)
- POJ 3669 Meteor Shower(bfs)
- poj 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669-Meteor Shower(BFS)
- POJ - 3669 - Meteor Shower(bfs)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower(bfs)
- poj 3669-Meteor Shower(简单bfs)
- POJ 3669 Meteor Shower(BFS)
- poj 3669 Meteor Shower (bfs)
- POJ 3669-Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower BFS
- Inno Setup安装时判断并关闭已经运行的应用exe
- Gogland use goimports
- hdu1255 覆盖的面积(线段树+扫描线+离散化,求矩形面积并)
- Gradle离线配置、.android、.AndroidStudio、.gradle、.m2缓存文件夹路径修改
- (HDU
- POJ 3669 Meteor Shower(BFS)
- MySQL中information_schema
- presentViewController 是否有系统导航条
- js笔记:Number()、parseInt() 和 parseFloat() 的区别
- web worker+highcharts动态实现可视化数据展示
- TBF(Trajectory Based Forwarding)路由协议
- linux下安装zookeeper
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是 100 的程序,并输出所有的可能性
- 黑科技-------咖啡装逼利器