标签:
http://poj.org/problem?id=3669
/*思路:BFS+预处理 先预处理会爆炸的区域,BFS,遇到-1则结束*/ #include <iostream> #include <queue> #include <algorithm> 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<point> 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) << "\n"; } return 0; }
poj_3669_Meteor Shower(BFS+预处理)
标签:
原文地址:http://blog.csdn.net/lyn12030706/article/details/43649579