POJ 3669 Meteor Shower BFS
2014-07-16 16:08
197 查看
//624K 63MS #include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <algorithm> using namespace std; struct node{ int x, y, t; }; const int INF = 1e9 + 7; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int map[305][305]; bool vis[305][305]; inline bool ok(int x, int y){ if(x >= 0 && y >= 0) return 1; else return 0; } inline void crash(int x, int y, int t){ if(t < map[x][y]) map[x][y] = t; if(t < map[x+1][y]) map[x+1][y] = t; if(t < map[x][y+1]) map[x][y+1] = t; if(x > 0 && t < map[x-1][y]) map[x-1][y] = t; if(y > 0 && t < map[x][y-1]) map[x][y-1] = t; } void bfs(){ memset(vis, 0, sizeof(vis)); queue<node> q; node h, n; h.x=0, h.y=0, h.t=0; //在OJ上不能用{...}赋值,编译错。。 q.push(h); vis[0][0] = 1; while(!q.empty()){ h = q.front(); q.pop(); if(map[h.x][h.y] == INF){ printf("%d\n", h.t); return; } for(int i=0; i<4; i++){ n.x = h.x + dx[i]; n.y = h.y + dy[i]; n.t = h.t + 1; if(ok(n.x, n.y)) if(!vis[n.x][n.y] && map[n.x][n.y] > n.t){ q.push(n); vis[n.x][n.y] = 1; } } } printf("-1\n"); } int main(){ int m; while(scanf("%d", &m) != EOF){ for(int i=0; i<305; i++) for(int j=0; j<305; j++) map[i][j] = INF; int x, y, t; while(m--){ scanf("%d %d %d", &x, &y, &t); crash(x, y, t); } bfs(); } return 0; }
相关文章推荐
- Meteor Shower(POJ-3669)
- poj_3669_Meteor Shower(BFS+预处理)
- poj 3669 Meteor Shower
- 广搜最短路(最短时间到达目的地),POJ(3669)
- Meteor Shower POJ - 3669
- bfs_poj_3669_Meteor Shower
- POJ 3669-Meteor Shower(BFS)
- Meteor Shower POJ - 3669
- POJ 3669 Meteor Shower【BFS】
- POJ 1979 3669 DFS BFS
- 【POJ 3669】Meteor Shower
- Meteor Shower (POJ 3669,广度优先搜索)
- poj 3669
- POJ-3669
- POJ-3669
- POJ 3669 Meteor Shower(BFS)
- poj 3669搜索
- BFS+预处理 POJ - 3669
- poj-3669-Meteor Shower
- poj-3669