hdu-1242-Rescue
来源:互联网 发布:开发板与单片机 编辑:程序博客网 时间:2024/05/17 22:58
题目链接
题意:
x代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙
路花费一秒,卫兵花费两秒
问到达终点的最少时间
思路:BFS+优先队列的水题
优先队列的基本使用 :http://blog.csdn.net/forever_kirito/article/details/72909692
#include<cstring>#include<cstdio>#include<queue>using namespace std;char a[210][210];int to[4][2]={1,0,-1,0,0,1,0,-1};bool vis[210][210];int n,m;struct node{ int x,y,time; friend bool operator <(const node &a,const node &b){ return a.time>b.time; }};bool Isok (int x,int y){ if(x>=0&&y>=0&&x<n&&y<m&&a[x][y]!='#') return true; return false;}int bfs(int x,int y){ node pre,next; memset(vis,0,sizeof(vis)); priority_queue<node>Q; pre.x=x;pre.y=y; pre.time=0; vis[pre.x][pre.y]=true; Q.push(pre); while(!Q.empty()){ pre=Q.top(); Q.pop(); if(a[pre.x][pre.y]=='r') return pre.time; for(int i=0;i<4;i++){ next.x=pre.x+to[i][0]; next.y=pre.y+to[i][1]; if(Isok(next.x,next.y)&&!vis[next.x][next.y]){ vis[next.x][next.y]=true; if(a[next.x][next.y]=='x'){ next.time=pre.time+2; } else next.time=pre.time+1; Q.push(next); } } } return -1;}int main(){ int x=0,y=0; while(~scanf("%d %d",&n,&m)){ for(int i=0;i<n;i++) scanf("%s",a[i]); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(a[i][j]=='a'){ x=i;y=j;break; } } } // printf("%d %d\n",x,y); int ans=bfs(x,y); if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",ans); } return 0;}
阅读全文
2 0
- hdu 1242 Rescue
- HDU-1242-Rescue
- HDU 1242 Rescue
- HDU-1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue<java>
- hdu 1242 rescue
- HDU-1242:Rescue
- HDU 1242 Rescue
- HDU 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- Rescue hdu 1242
- hdu-1242-Rescue
- Hdu 1242 Rescue
- sparkSQL1.1入门
- 习题1.5
- Tomcat服务器使用详解
- 微信小程序如何通过微信绑定的手机号进行快速登录
- 通过关联条件将A表的某个字段值和B表的某个对应字段值设成一样
- hdu-1242-Rescue
- 你真的会使用XMLHttpRequest吗?
- Git的学习
- 技术博客,技术社区,学术机构(汇总)
- Linux-4.9.2内核在mini2440上的移植(三)——编译环境测试
- python 高级特性笔记
- js事件(Event)之阻止默认操作
- SMO算法的极简数学推导
- ceil和floor用法