hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
2015-05-28 14:49
483 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1240
开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向。
第一维代表在第几层。后两维代表行和列。
http://acm.hdu.edu.cn/showproblem.php?pid=1253
这题用优先队列是超时的,普通队列可以过。
开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向。
第一维代表在第几层。后两维代表行和列。
#include <cstdio> #include <cstring> #include <queue> using namespace std; struct point { int x,y,z,step; bool operator < (const point a) const { return step>a.step; } }; point t,e; int n; char maze[15][15][15]; int used[15][15][15]; int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}}; void bfs() { memset(used,0,sizeof(used)); priority_queue<point>que; que.push(t); used[t.z][t.x][t.y]=1; while(!que.empty()) { point s=que.top(); que.pop(); // printf("%d %d %d %d\n",s.z,s.x,s.y,s.step); if(s.x==e.x&&s.y==e.y&&s.z==e.z) {printf("%d %d\n",n,s.step);return;} for(int i=0;i<6;i++) { t.x=s.x+dir[i][0],t.y=s.y+dir[i][1],t.z=s.z+dir[i][2]; if(t.x>=0&&t.x<n&&t.y>=0&&t.y<n&&t.z>=0&&t.z<n&&maze[t.z][t.x][t.y]!='X'&&!used[t.z][t.x][t.y]) { t.step=s.step+1; used[t.z][t.x][t.y]=1; que.push(t); } } } printf("NO ROUTE\n"); } int main() { // freopen("a.txt","r",stdin); char s[10]; while(~scanf("%s",s)) { scanf("%d",&n); for(int i=0;i<n;i++) for(int j=0;j<n;j++) scanf("%s",maze[i][j]); scanf("%d%d%d",&t.y,&t.x,&t.z); t.step=0; scanf("%d%d%d",&e.y,&e.x,&e.z); scanf("%s",s); bfs(); } return 0; }
http://acm.hdu.edu.cn/showproblem.php?pid=1253
这题用优先队列是超时的,普通队列可以过。
#include <cstdio> #include <cstring> #include <queue> using namespace std; struct point { int x,y,z,time; }s,e; int maze[55][55][55]; int dir[6][3]= {{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}}; int main() { //freopen("a.txt","r",stdin); int d,flag; int a,b,c,t; queue<point>que; scanf("%d",&d); while(d--) { scanf("%d%d%d%d",&a,&b,&c,&t); for(int i=0; i<a; i++) for(int j=0; j<b; j++) for(int k=0; k<c; k++) scanf("%d",&maze[i][j][k]); while(!que.empty()) que.pop(); s.x=0,s.y=0,s.z=0,s.time=0; flag=0; que.push(s); maze[s.z][s.x][s.y]=1; while(!que.empty()) { e=que.front(); que.pop(); //printf("%d %d %d %d\n",e.z,e.x,e.y,e.time); if(e.z==a-1&&e.x==b-1&&e.y==c-1&&e.time<=t) { printf("%d\n",e.time); flag=1; break; } for(int i=0; i<6; i++) { s.x=e.x+dir[i][0]; s.y=e.y+dir[i][1]; s.z=e.z+dir[i][2]; if(s.z>=0&&s.z<a&&s.x>=0&&s.x<b&&s.y>=0&&s.y<c&&maze[s.z][s.x][s.y]!=1) { s.time=e.time+1; if(s.time<=t) { maze[s.z][s.x][s.y]=1; que.push(s); } } } } if(!flag) printf("-1\n"); } return 0; }
相关文章推荐
- 胜利大逃亡 hdu 1253 && 亡命逃窜 (bfs)nyoj 523
- HDU 1429--胜利大逃亡(续)【BFS && 状态压缩】
- HDU 1253 胜利大逃亡(BFS)
- hdu-1253-胜利大逃亡(bfs)
- Hdu 1253 胜利大逃亡 解题报告(bfs)
- HDU-1253 胜利大逃亡(简单三维广搜BFS)
- HDU1253 胜利大逃亡 (3维 BFS)
- HDU----1253胜利大逃亡 BFS
- hdu 题目1253 胜利大逃亡(BFS)
- hdu1240 Asteroids!--DFS & BFS
- hdu 1253 胜利大逃亡 BFS
- HDU 1253 胜利大逃亡(三维BFS)
- HDU 1253 胜利大逃亡(简单三维BFS)
- HDU-1253 胜利大逃亡 (BFS)
- HDU 胜利大逃亡 1253 BFS
- HDU-1253 胜利大逃亡 (BFS)
- hdu 1253 胜利大逃亡(BFS)
- HDU 1253 - 胜利大逃亡(BFS)
- hdu 1253 胜利大逃亡(BFS,三维)
- HDU 1253:胜利大逃亡(简单三维BFS)