poj 3669 BFS
2015-04-01 15:21
176 查看
思路还是不明朗,其实这题和迷宫的最短路径思路差不多,但是这题没有给出明显的边界条件,导致半天没有想明白,写的程序还是半天调试不过去,基础不扎实......keep moving .....
粗略的想法:
预处理,把每个会被陨石雨破坏的坐标都用破坏时间表示出来,类似迷宫的最短路径的障碍,方便接下来的bfs判断,比如,在原点不需要动的情况或往回走的情况。
注意:
在oj上 cin流输入 比scanf输入效率差几倍,用cin就会TLE。
参考引用:http://www.cnblogs.com/justforgl/archive/2012/12/19/2825422.html
粗略的想法:
预处理,把每个会被陨石雨破坏的坐标都用破坏时间表示出来,类似迷宫的最短路径的障碍,方便接下来的bfs判断,比如,在原点不需要动的情况或往回走的情况。
注意:
在oj上 cin流输入 比scanf输入效率差几倍,用cin就会TLE。
/* * ===================================================================================== * * Filename: poj_3669.cpp * * Description: poj 3669 Meteor shower * * Version: 1.0 * Created: 03/29/2015 03:38:35 AM * Revision: none * Compiler: gcc * * Author: Kart, 786900722@qq.com * Blog: http://blog.csdn.net/linux_kernel_fan/ * * ===================================================================================== */ #include<cstdio> #include<iostream> #include<queue> #include<utility> #include<cstring> using namespace std; const int maxsize=405; int map[maxsize][maxsize]; int M; pair<int, int> dir[5] = {{0, 0}, {-1, 0}, {0, 1}, {1, 0}, {0, -1}}; typedef struct { int x; int y; int time; }Node; int solve() { if(map[0][0] == 0) return -1; if(map[0][0] == -1) return 0; queue<Node> que; Node now, tmp; tmp.x = tmp.y = tmp.time = 0; que.push(tmp); while(que.size()) { now = que.front(); que.pop(); for(int i = 1; i < 5; i++) { tmp.x = now.x + dir[i].first; tmp.y = now.y + dir[i].second; tmp.time = now.time + 1; if(tmp.x < 0 || tmp.x >= maxsize || tmp.y < 0 || tmp.y >= maxsize) continue; if(map[tmp.x][tmp.y] == -1) return tmp.time; if(tmp.time >= map[tmp.x][tmp.y]) continue; map[tmp.x][tmp.y] = tmp.time; que.push(tmp); } } return -1; } int main() { while(scanf("%d", &M) != EOF) { memset(map, -1, sizeof(map)); for(int i = 0; i < M; i++) { int x, y,t; scanf("%d%d%d", &x, &y, &t); for(int k = 0; k < 5; k++) { int nx = x + dir[k].first; int ny = y + dir[k].second; if(nx >= 0 && nx < maxsize && ny >= 0 && ny < maxsize) { if(map[nx][ny] == -1) map[nx][ny] = t; else map[nx][ny] = min(t, map[nx][ny]); } } } printf("%d\n", solve()); } return 0; }
参考引用:http://www.cnblogs.com/justforgl/archive/2012/12/19/2825422.html
相关文章推荐
- POJ 3669 BFS
- POJ 3669-Meteor Shower(BFS)
- Meteor Shower (poj 3669 bfs)
- POJ 3669 Meteor Shower(BFS)
- POJ 3669 Meteor Shower (带时间轴的bfs)
- Meteor Shower POJ - 3669-BFS
- POJ 3669Meteor Shower(传说中的流星雨 BFS)
- POJ 3669 Meteor Shower(bfs)
- BFS——Poj 3669 Meteor Shower
- 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]
- BFS:Meteor Shower(POJ 3669)
- [水+bfs] poj 3669 Meteor Shower
- POJ 3669-Meteor Shower(BFS)
- POJ 3669 Meteor Shower(BFS)