hdu 1026 Ignatius and the Princess I
来源:互联网 发布:优优部屋 淘宝 编辑:程序博客网 时间:2024/05/15 07:08
优先队列。。。
#include <iostream>#include<queue>using namespace std;struct node{int time; //每个节点的时间int num;//每个节点的位置 (num/m,num%m)};bool operator<(node a,node b) //优先队列的排序{return a.time>b.time;}int n,m;priority_queue<node> pq;int vis[105*105]; //是否进队列int map[105][105];int parent[105*105]; //每个节点的父亲节点int t;int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int Can(int r,int c){if(map[r][c]==-1)return 0;if(vis[r*m+c]||r<0||c<0||r>=n||c>=m)return 0;return 1;}void put_path(int pos){if(parent[pos]>=0) put_path(parent[pos]); //递归找出一条最短路径else return;int pr=parent[pos]/m, //该节点的父亲节点位置pc=parent[pos]%m;int cr=pos/m, //该节点的位置cc=pos%m;if(map[pr][pc]>=1)for(int i=0;i<map[pr][pc];t++,i++)printf("%ds:FIGHT AT (%d,%d)\n",t,pr,pc);printf("%ds:(%d,%d)->(%d,%d)\n",t++,pr,pc,cr,cc);}void bfs(){vis[0]=1;node pos;pos.time=0;parent[0]=-1;pos.num=0;pq.push(pos);int nextr,nextc;int curr,curc;node cur;while(!pq.empty()){pos=pq.top();pq.pop();if(pos.num==(n-1)*m+m-1)break;curr=pos.num/m;curc=pos.num%m;for(int i=0;i<4;i++) //四个方向{nextr=curr+dir[i][0];nextc=curc+dir[i][1];if(Can(nextr,nextc)){cur.time=pos.time+1+map[nextr][nextc]; //此节点的时间是中间路程1加上该节点的时间parent[nextr*m+nextc]=pos.num;cur.num=nextr*m+nextc;vis[nextr*m+nextc]=1;pq.push(cur); }}}if(pos.num!=(n-1)*m+m-1) printf("God please help our poor hero.\n");else{printf("It takes %d seconds to reach the target position, let me show you the way.\n",pos.time);t=1;put_path(pos.num); //递归输出路径for(int i=0;i<map[pos.num/m][pos.num%m];t++,i++) //最后一个节点printf("%ds:FIGHT AT (%d,%d)\n",t,pos.num/m,pos.num%m);}printf("FINISH\n");}int main(){//freopen("in.txt","r",stdin);while(scanf("%d %d",&n,&m)==2){while(!pq.empty()) pq.pop();char ch;for(int i=0;i<n;i++){getchar();for(int j=0;j<m;j++){scanf("%c",&ch);if(ch=='.')map[i][j]=0;else if(ch=='X')map[i][j]=-1;else map[i][j]=ch-'0';}}memset(vis,0,sizeof(vis));bfs();}return 0;}
- hdu 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- hdu - 1026 - Ignatius and the Princess I
- HDU-1026Ignatius and the Princess I
- HDU-1026-Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- hdu-1026-Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- HDU - 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- 任意多边形求重心
- Partitioning
- IOS 常用宏定义(二)
- Makefile经典教程
- 求解协方差矩阵算法 转载地址:http://www.16kan.com/post/231601.html
- hdu 1026 Ignatius and the Princess I
- 《数据结构》第一章之绪论相关代码
- Android 基于xmpp的即时通信Demo
- 转载一篇关于POWERPC的中断的文章
- C# 枚举类型(enum)学习
- jQuery插件开发全解析
- Xcode 中设置部分文件ARC支持
- 【OpenStack】Quantum(G版)中的安全组
- 网页爬虫