poj 2312 Battle City(优先队列)
2011-12-01 23:34
435 查看
题意:
坦克大战,
'Y' (you),
'T' (target),
'S' (steel wall),
'B' (brick wall),
'R' (river)
'E' (empty space).
从Y走到T,问最少需要的时间。而不是步数>_<,
分析
一开始用queue做WA了一次。
还是忘记初始化,还有最近总是RE的,悲剧啊,原来是不是B和E||T的也入队了。。。入队函数放进if里吧。
ps:
这个星期数据结构实训,太失败了,偶太弱了!!!!
坦克大战,
'Y' (you),
'T' (target),
'S' (steel wall),
'B' (brick wall),
'R' (river)
'E' (empty space).
从Y走到T,问最少需要的时间。而不是步数>_<,
分析
一开始用queue做WA了一次。
还是忘记初始化,还有最近总是RE的,悲剧啊,原来是不是B和E||T的也入队了。。。入队函数放进if里吧。
ps:
这个星期数据结构实训,太失败了,偶太弱了!!!!
#include<iostream>//得要用优先队列啊,因为存的不是最短步数。而是最短时间,和zoj1649差不多。 #include <cstdio> #include <queue> #include <string.h> using namespace std; typedef struct point{ int x,y; int len; bool operator<(const point &e)const{//优先级队列默认是最大堆,只需在这里加个判断返回...>_< return len>e.len; } }point; char a[315][315]; int flag[315][315]; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int N,M; point end; priority_queue<point>Q; int BFS(point s) { point head,temp; int x,y,len=0; flag[s.x][s.y]=1; Q.push(s); while(!Q.empty()) { head=Q.top (); if(a[head.x][head.y]=='T') return head.len; Q.pop (); for(int i=0;i<4;i++) { x=head.x+dir[i][0]; y=head.y+dir[i][1]; if(x>=0&&x<N&&y>=0&&y<M&&flag[x][y]==0) { if(a[x][y]=='B') { flag[x][y]=1; temp.len=head.len+2; temp.x=x; temp.y=y; Q.push (temp); } if(a[x][y]=='E'||a[x][y]=='T') { flag[x][y]=1; temp.len=head.len+1; temp.x=x; temp.y=y; Q.push (temp); } } } } return -1; } int main() { int i,j,len; point start; while(~scanf("%d%d",&N,&M)) { while(!Q.empty ()) Q.pop (); if(N==0&&M==0) break; memset(flag,0,sizeof(flag)); for(i=0;i<N;i++) { for(j=0;j<M;j++) { cin>>a[i][j]; if(a[i][j]=='Y') { start.x=i; start.y=j; } } } start.len=0; len=BFS(start); printf("%d\n",len); } return 0; }
相关文章推荐
- NYOJ 284 坦克大战 && POJ 2312 Battle City (广搜+优先队列)
- 【POJ】2312-Battle City (bfs,优先队列)
- poj 2312 Battle City(优先队列+bfs)
- POJ 2312 Battle City 优先队列+BFS
- POJ2312 Battle City(优先队列+广搜BFS)
- POJ 2312 Battle City(优先队列+BFS)
- poj 2312 Battle City 优先队列+bfs 或 记忆化广搜
- poj 2312 Battle City(优先队列+bfs)
- Battle City(POJ - 2312)(深搜+优先队列)
- poj 2312 Battle City(优先队列+bfs)
- POJ 2312 Battle City (bfs+优先队列)
- POJ 2312(优先队列+bfs)
- Battle City(POJ 2312)
- poj 2312:Battle City
- 【POJ 2312】Battle City
- POJ 2312 Battle City 优先队列+BFS
- POJ 2312 Battle City 优先队列+BFS
- POJ 2312 Battle City(bfs)
- poj2312 - Battle City (BFS变形)
- poj 2312 Battle City【bfs+优先队列】