HDU 1242 Rescue (BFS+优先队列)
来源:互联网 发布:网络技术发展现状 编辑:程序博客网 时间:2024/05/22 10:51
从天使的位置开始,直到找到r,优先队列每次选择最短时间的路
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<sstream>#include<queue>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int M = 1010;struct node{int x, y, step;bool operator <(const node &b)const{return step > b.step;}};int n, m;priority_queue<node>q;char mp[210][210];int sx, sy;bool vis[210][210];int dir[][2] = { 1,0,-1,0,0,1,0,-1 };int bfs(){node st;st.x = sx;st.y = sy;st.step = 0;vis[sx][sy] = 1;q.push(st);while (!q.empty()){node nw = q.top();q.pop();if (mp[nw.x][nw.y] == 'r'){return nw.step;}for (int i = 0; i < 4; ++i){int xx = nw.x + dir[i][0];int yy = nw.y + dir[i][1];if(xx<0 ||xx>=n ||yy<0||yy>=m ||vis[xx][yy])continue;if(mp[xx][yy]=='#')continue;if (mp[xx][yy] == 'x'){vis[xx][yy] = 1;node nx;nx.x = xx;nx.y = yy;nx.step = nw.step + 2;q.push(nx);}else{vis[xx][yy] = 1;node nx;nx.x = xx;nx.y = yy;nx.step = nw.step + 1;q.push(nx);}}}return -1;}int main(){while (~scanf("%d%d", &n, &m)){for (int i = 0; i < n; ++i){for (int j = 0; j < m; ++j){cin >> mp[i][j];if (mp[i][j] == 'a'){sx = i;sy = j;}}}memset(vis, 0, sizeof(vis));int flag=bfs();if (flag == -1){puts("Poor ANGEL has to stay in the prison all his life.");}else{printf("%d\n", flag);}}return 0;}
0 0
- hdu 1242Rescue(bfs+优先队列)
- hdu 1242 Rescue (优先队列+bfs)
- HDU 1242 Rescue(BFS +优先队列)
- HDU 1242 Rescue(BFS,优先队列)
- hdu 1242 Rescue(BFS优先队列)
- HDU 1242 Rescue(优先队列+bfs)
- hdu 1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(BFS+优先队列)
- hdu 1242 Rescue (BFS+优先队列)
- HDU 1242 Rescue (BFS+优先队列)
- HDU-1242-Rescue(优先队列+BFS)
- HDU 1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(bfs+优先队列)
- HDU:1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(BFS+【优先队列】)
- HDU 1242 Rescue(BFS+优先队列)
- 【HDU】-1242-Rescue(BFS+优先队列)
- HDU 1242 Rescue (BFS+优先队列)
- WEBSHELL姿势之不安全的HTTP方法(渗透实验)
- sprintf函数应用
- 【Space Shoot Project】UI
- 安装LR11时在产品配置时卡住如何解决?
- 由linux命令行下命令参数前的一横(-)和两横(--)的区别而得知的
- HDU 1242 Rescue (BFS+优先队列)
- BNDM字符串匹配算法
- Tomcat的四种web应用部署方式详解
- struts 下载中文名文件
- strings.xml使用方法
- boost/config.hpp文件详解
- Orcale 11g 安装启动测试详解(多图)
- 我使用的Linux命令
- Intent实现简单的值传递