poj 2312 Battle City(优先队列+bfs)
2014-08-14 16:51
399 查看
题目链接:http://poj.org/problem?id=2312
题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟。最后求解Y--->T的最短时间!!
看到这题首先想到广搜来找最短时间,但是这里可以对B和E进行处理,方便计算~
View Code
题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟。最后求解Y--->T的最短时间!!
看到这题首先想到广搜来找最短时间,但是这里可以对B和E进行处理,方便计算~
#include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std; int dir[4][2]= {1,0,-1,0,0,1,0,-1}; bool vis[310][310]; char map[310][310]; int m,n,sx,sy; struct node { int x,y,time; }; int bfs() { node s,ss,sss; //priority_queue<node>q; queue<node>q; s.x=sx; s.y=sy; s.time=0; q.push(s); vis[sx][sy]=1; while (!q.empty()) { ss=q.front(); q.pop(); for (int i=0; i<4; i++) { sss.x=ss.x+dir[i][0]; sss.y=ss.y+dir[i][1]; if (sss.x<0||sss.y<0||sss.x>=n||sss.y>=m||map[sss.x][sss.y]=='R' || map[sss.x][sss.y]=='S'||vis[sss.x][sss.y]) continue; if (map[sss.x][sss.y]=='B') sss.time=ss.time+2; else sss.time=ss.time+1; //sss.time=ss.time+1; if (map[sss.x][sss.y]=='T') return sss.time; vis[sss.x][sss.y]=1; q.push(sss); } } return -1; } int main () { while (~scanf("%d%d",&n,&m)) { if (n==0&&m==0) break; memset(vis,0,sizeof(vis)); for (int i=0; i<n; i++) { getchar(); for (int j=0; j<m; j++) { scanf("%c",&map[i][j]); if (map[i][j]=='Y') { sx=i; sy=j; } } } printf ("%d\n",bfs()); } return 0; }
View Code
相关文章推荐
- 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,优先队列)
- POJ2312 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(优先队列)
- poj2312 - Battle City (BFS变形)
- Battle City(POJ - 2312)(深搜+优先队列)
- 【BFS+优先队列】Battle City(poj-2312)
- POJ 2312 Battle City【Bfs+优先队列】
- POJ 2312Battle City(BFS-priority_queue 或者是建图spfa)
- POJ 2312:Battle City【BFS & 优先队列】
- POJ 2312 Battle City(优先队列 + BFS)
- POJ_2312_BFS:priority_queue -- Battle City