POJ 2312 Battle City(BFS+优先队列)
来源:互联网 发布:阿里云服务器怎样使用 编辑:程序博客网 时间:2024/06/04 08:28
题意:坦克要从起点(Y),到目的地(T),坦克不能通过钢墙(S),河(R),可以在空地在行走(E),射击破坏砖墙(B),射击砖墙时不行走且花费一个单位的时间,在空地上行走时也花费一个单位的时间。求坦克从起点到目的地最少花多少时间,不可达输出-1;
思路:等于穿过B花了两个单时间,用优先队列即可
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;#define inf 0x3f3f3f3fint r,c;int sx,sy;int ans;char mapp[305][305];bool book[305][305];int next[4][2]={0,1,0,-1,1,0,-1,0};typedef pair<int,pair<int,int> > pii;priority_queue<pii,vector<pii>,greater<pii> >que;void bfs(){ while(!que.empty()) que.pop(); pair<int ,int > st=make_pair(sx,sy); que.push(make_pair(0,st)); book[sx][sy]=1; while(!que.empty()) { pii nxt; pii cur=que.top(); que.pop(); int x=cur.second.first; int y=cur.second.second; if(mapp[x][y]=='T') { ans=cur.first; return ; } for(int i=0;i<4;i++) { int nx=x+next[i][0]; int ny=y+next[i][1]; if(book[nx][ny]) continue; if(mapp[nx][ny]=='E'||mapp[nx][ny]=='T' ) { nxt.first=cur.first+1; pair<int ,int > nxp=make_pair(nx,ny); nxt.second=nxp; que.push(nxt); book[nx][ny]=1; } else if(mapp[nx][ny]=='B') { nxt.first=cur.first+2; pair<int ,int > nxp=make_pair(nx,ny); nxt.second=nxp; que.push(nxt); book[nx][ny]=1; } } } return ;}void ini(){ ans=-1; memset(mapp,0,sizeof(mapp)); memset(book,false,sizeof(book));}int main(){ while(scanf("%d%d",&r,&c),(r||c)) { ini(); for(int i=1;i<=r;i++) scanf("%s",mapp[i]+1); for(int i=1;i<=r;i++) for(int j=1;mapp[i][j];j++) if(mapp[i][j]=='Y') { sx=i; sy=j; } bfs(); printf("%d\n",ans); } return 0;}
0 0
- POJ 2312 Battle City 优先队列+BFS
- 【BFS+优先队列】Battle City(poj-2312)
- poj 2312 Battle City bfs +优先队列
- POJ 2312 Battle City 优先队列+BFS
- POJ 2312 Battle City(优先队列 + BFS)
- POJ 2312 Battle City(优先队列+BFS)
- POJ 2312 Battle City BFS+优先队列
- poj 2312 Battle City(优先队列+bfs)
- POJ 2312:Battle City【BFS & 优先队列】
- poj 2312 Battle City(优先队列+bfs)
- POJ 2312 Battle City【Bfs+优先队列】
- POJ 2312 Battle City 优先队列+bfs
- POJ 2312 Battle City(BFS+优先队列)
- 【POJ】2312-Battle City (bfs,优先队列)
- POJ 2312 Battle City (优先队列 + BFS)
- (POJ 2312)Battle City 优先队列 & 简单BFS
- poj 2312 Battle City(优先队列)
- Battle City (BFS+优先队列)
- 将自己的图片插入QRcode中
- 编写第一个ice程序
- 最简单 NDK 例子
- Access中批量替换数据库内容
- Android中的不同drawable文件夹所带来的思考
- POJ 2312 Battle City(BFS+优先队列)
- Please check that values for params "default.fs.name" and "hive.metastore.warehouse.dir" do not conf
- 第168章 大战!88真人娱乐城
- HDU 3001 Travelling(状压dp)
- 高通8x12平台 mipi屏 调试 .
- Protocol Buffers 语法指南
- hdu 1753 大明A+B
- zepto 研究
- 高通 8x12 添加 TP和按键 .