poj 2312 Battle City(优先队列+bfs)
来源:互联网 发布:淘宝背景音乐推荐 编辑:程序博客网 时间:2024/05/29 14:49
题目链接:http://poj.org/problem?id=2312View CodeView Code
题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟。最后求解Y--->T的最短时间!!
看到这题首先想到广搜来找最短时间,但是这里可以对B和E进行处理,方便计算~
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 int dir[4][2]= {1,0,-1,0,0,1,0,-1}; 7 bool vis[310][310]; 8 char map[310][310]; 9 int m,n,sx,sy;10 struct node11 {12 int x,y,time;13 friend bool operator<(node a,node b)14 {15 return a.time>b.time;16 }17 };18 19 int bfs()20 {21 node s,ss,sss;22 priority_queue<node>q;23 s.x=sx;24 s.y=sy;25 s.time=0;26 q.push(s);27 vis[sx][sy]=1;28 while (!q.empty())29 {30 ss=q.top();31 q.pop();32 for (int i=0; i<4; i++)33 {34 sss.x=ss.x+dir[i][0];35 sss.y=ss.y+dir[i][1];36 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])37 continue;38 if (map[sss.x][sss.y]=='B')39 sss.time=ss.time+2;40 else41 sss.time=ss.time+1;42 //sss.time=ss.time+1;43 if (map[sss.x][sss.y]=='T')44 return sss.time;45 vis[sss.x][sss.y]=1;46 q.push(sss);47 }48 }49 return -1;50 }51 52 int main ()53 {54 while (~scanf("%d%d",&n,&m))55 {56 if (n==0&&m==0)57 break;58 memset(vis,0,sizeof(vis));59 for (int i=0; i<n; i++)60 {61 getchar();62 for (int j=0; j<m; j++)63 {64 scanf("%c",&map[i][j]);65 if (map[i][j]=='Y')66 {67 sx=i;68 sy=j;69 }70 }71 }72 printf ("%d\n",bfs());73 }74 return 0;75 }
还有一种,单纯的广搜也是可以的。
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 int dir[4][2]= {1,0,-1,0,0,1,0,-1}; 7 bool vis[310][310]; 8 char map[310][310]; 9 int m,n,sx,sy;10 struct node11 {12 int x,y,time;13 };14 15 int bfs()16 {17 node s,ss,sss;18 //priority_queue<node>q;19 queue<node>q;20 s.x=sx;21 s.y=sy;22 s.time=0;23 q.push(s);24 vis[sx][sy]=1;25 while (!q.empty())26 {27 ss=q.front();28 q.pop();29 for (int i=0; i<4; i++)30 {31 sss.x=ss.x+dir[i][0];32 sss.y=ss.y+dir[i][1];33 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])34 continue;35 if (map[sss.x][sss.y]=='B')36 sss.time=ss.time+2;37 else38 sss.time=ss.time+1;39 //sss.time=ss.time+1;40 if (map[sss.x][sss.y]=='T')41 return sss.time;42 vis[sss.x][sss.y]=1;43 q.push(sss);44 }45 }46 return -1;47 }48 49 int main ()50 {51 while (~scanf("%d%d",&n,&m))52 {53 if (n==0&&m==0)54 break;55 memset(vis,0,sizeof(vis));56 for (int i=0; i<n; i++)57 {58 getchar();59 for (int j=0; j<m; j++)60 {61 scanf("%c",&map[i][j]);62 if (map[i][j]=='Y')63 {64 sx=i;65 sy=j;66 }67 }68 }69 printf ("%d\n",bfs());70 }71 return 0;72 }
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(优先队列)
- poj 2312 Battle City 优先队列
- hdu 2544 最短路 (dijkstra,floyd)
- 成员变量、实例变量、类变量、成员方法、实例方法、类方法的区别
- hdu 1874 畅通工程续
- spring aop中的propagation的7种配置的意思
- hdu 2112 HDU Today (最短路)
- poj 2312 Battle City(优先队列+bfs)
- hdu 1548 A strange lift (dijkstra算法)
- hdu 3790 最短路径问题(双重权值,dijkstra算法)
- hdu 1217 Arbitrage (spfa算法)
- poj 2387 Til the Cows Come Home(dijkstra算法)
- hdu 2066 一个人的旅行
- poj 1062 昂贵的聘礼 (dijkstra最短路)
- poj 2253 Frogger (dijkstra最短路)
- CxImage的编译及简单使用举例