hdu 1026 Ignatius and the Princess I (BFS)
来源:互联网 发布:石家庄网络优化 编辑:程序博客网 时间:2024/05/17 10:07
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026
bfs+保存路径 留存
#include<stdio.h>#include<string.h>#include<algorithm>struct{ int x,y,time,pre;}q[110*110];int n,m,vis[110][110],xx[110][110];char s[110][110];int dx[]={1,-1,0,0};int dy[]={0,0,-1,1};void Print(int i){ if(q[i].pre!=-1) { Print(q[i].pre); if(q[q[i].pre].x==q[i].x&&q[q[i].pre].y==q[i].y) printf("%ds:FIGHT AT (%d,%d)\n",q[i].time,q[i].x,q[i].y); else printf("%ds:(%d,%d)->(%d,%d%)\n",q[i].time,q[q[i].pre].x,q[q[i].pre].y,q[i].x,q[i].y); }}void bfs(){ int front=0,rear=1,flag=0; int i; q[front].x=q[front].y=q[front].time=0; q[front].pre=-1; vis[0][0]=1; while(front!=rear) { while(xx[q[front].x][q[front].y]>0) { xx[q[front].x][q[front].y]--; q[rear].x=q[front].x; q[rear].y=q[front].y; q[rear].time=q[front].time+1; q[rear].pre=front; rear++; front++; } int i; for(i=0;i<4;i++) { int a=q[front].x+dx[i]; int b=q[front].y+dy[i]; if(a>=0&&a<n&&b>=0&&b<m&&!vis[a][b]&&s[a][b]!='X') { vis[a][b]=1; q[rear].x=a; q[rear].y=b; q[rear].time=q[front].time+1; q[rear].pre=front; if(a==n-1&&b==m-1) { front=rear; flag=1; while(xx[q[front].x][q[front].y]>0) { rear++; xx[q[front].x][q[front].y]--; q[rear].x=q[front].x; q[rear].y=q[front].y; q[rear].time=q[front].time+1; q[rear].pre=front; front=rear; } printf("It takes %d seconds to reach the target position, let me show you the way.\n",q[rear].time); Print(front); break; } rear++; } } front++; if(flag) break; } if(flag==0) printf("God please help our poor hero.\n"); printf("FINISH\n");}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { int i,j; memset(xx,0,sizeof(xx)); memset(vis,0,sizeof(vis)); for(i=0;i<n;i++) { scanf("%s",s[i]); for(j=0;j<m;j++) { if(s[i][j]>='1'&&s[i][j]<='9') xx[i][j]=s[i][j]-'0'; } } bfs(); } return 0;}
- Ignatius and the Princess I hdu 1026 priority_queue + bfs (vector)
- hdu 1026 Ignatius and the Princess I (BFS)
- hdu 1026 Ignatius and the Princess I(bfs+priority_queue)
- HDU-1026 Ignatius and the Princess I (BFS)
- hdu 1026 Ignatius and the Princess I BFS+剪枝
- HDU 1026 Ignatius and the Princess I && BFS+记录路径
- hdu 1026 Ignatius and the Princess I bfs+优先级队列
- hdu 1026 Ignatius and the Princess I(BFS)
- hdu 1026 Ignatius and the Princess I----bfs+递归
- Hdu 1026 bfs Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I(优先队列+BFS)
- hdu 1026 Ignatius and the Princess I BFS
- hdu 1026 Ignatius and the Princess I 优先队列 + bfs
- HDU 1026 Ignatius and the Princess I - BFS + 优先队列
- hdu 1026 Ignatius and the Princess I(bfs)
- HDU 1026 Ignatius and the Princess I BFS打印路径
- 【Bfs】HDU 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I(BFS)
- PHP详解ASCII码对照表与字符转换
- keytool证书操作
- 自我剖解
- php 环境搭建+ecshop 本机调试
- python 字典排序
- hdu 1026 Ignatius and the Princess I (BFS)
- const详细总结
- java异常
- mysql批量删除或修改
- WEB-Servlet Filter2
- Python在Linux上安装
- 无法写入到注册表,请确定你是否有写的权限
- automake---configure
- MPEG4技术全攻略