设为首页 加入收藏

TOP

poj_3669_Meteor Shower(BFS+预处理)
2015-07-20 17:20:01 来源: 作者: 【 】 浏览:2
Tags:poj_3669_Meteor Shower BFS 处理
 
 
 
/*思路:BFS+预处理
先预处理会爆炸的区域,BFS,遇到-1则结束*/
#include 
  
   
#include 
   
     #include 
    
      using namespace std; int visit[1001][1001]; int dx[5] = {0,0,1,0,-1}; int dy[5] = {0,1,0,-1,0}; typedef struct { int x,y; int step; }point; queue
     
       que; int bfs(int x,int y) { int cnt; if(visit[0][0] == 0) return -1;//无法逃离 if(visit[0][0] == -1) return 0; //安全,无需逃离 point s,cur,next; s.x = x; s.y = y; s.step = 0; que.push(s); while(!que.empty()) { cur = que.front(); que.pop(); for(int i = 0;i < 5;i++) { next.x = cur.x + dx[i]; next.y = cur.y + dy[i]; next.step = cur.step + 1; if(next.x >= 0 && next.y >= 0 && next.y < 1001 && next.x < 1001) { if(visit[next.x][next.y] == -1) { return next.step; } if(next.step < visit[next.x][next.y]) { visit[next.x][next.y] = next.step;//记录最小的步数 que.push(next); } } } } return -1; } int main() { int m; while(cin >> m) { int x,y,t,xx,yy; memset(visit,-1,sizeof(visit)); for(int i = 0;i < m;i++) { cin >> x >> y >> t; for(int i = 0;i <= 4;i++) { xx = x + dx[i]; yy = y + dy[i]; if(xx >= 0 && yy >= 0 && xx < 1001 && yy < 1001)//保证在有限区域内 { if(visit[xx][yy] == -1) visit[xx][yy] = t; else visit[xx][yy] = min(visit[xx][yy],t); } } } cout << bfs(0,0) << ; } return 0; } 
     
    
   
  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj2948--Martian Mining(dp) 下一篇POJ 3468 A Simple Problem with ..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python 数据分析与可 (2025-12-26 21:51:20)
·从零开始学Python之 (2025-12-26 21:51:17)
·超长干货:Python实 (2025-12-26 21:51:14)
·为什么 Java 社区至 (2025-12-26 21:19:10)
·Java多线程阻塞队列 (2025-12-26 21:19:07)