hdu 1026 Ignatius and the Princess I BFS
来源:互联网 发布:c链接mysql数据库 编辑:程序博客网 时间:2024/05/18 09:06
题意:给定一个n*m的迷宫,起点在(0,0),终点在(n-1,m-1);规则如下:
1)每次移动只能上下左右方向移动一格,耗时1秒,不能到迷宫外。
2)迷宫上的各个字符意义为:
‘.’,表示可以走的路。
‘X’,表示不可以走的陷阱。
‘n’,n为数字,(1<=n<=9),表示这个区域有一格HP值为n的怪物,需要耗时n秒时间打败,才能继续前进。
题解:
典型的BFS题目,只是加了怪物的设定以及需要输出路径。
路径用pre[i]记录前驱,由于n<=100,m<=100所以一格点(i,j)可以表示为i*100+j,最后递归输出路径即可。
而路过标志vis[i][j],不像以前那样只是表示是否路过,因为可能绕远路的时间更短。所以vis[i][j]表示的是到达(i,j)最少需要的时间,比vis[i][j]大的路径都可以省去。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <ctime>#include <algorithm>#include <cmath>#include <queue>#include <vector>using namespace std;const int maxn=101;const int INF=1e8;int pre[maxn*maxn];//记录路径int vis[maxn][maxn];//vis[i][j]记录到(i,j)的所需要的最短时间char e[maxn][maxn];int ans,MinDis;int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//转移方向struct node{ int x,y,num;};void show(int i)//结果输出{ if(pre[i]==-1) { if(e[i/100][i%100]!='.') { for(int k=0;k<e[i/100][i%100]-'0';k++) printf("%ds:FIGHT AT (%d,%d)\n",++ans,i/100,i%100); } return ; } show(pre[i]); printf("%ds:(%d,%d)->(%d,%d)\n",++ans,pre[i]/100,pre[i]%100,i/100,i%100); if(e[i/100][i%100]!='.') { for(int k=0;k<e[i/100][i%100]-'0';k++) printf("%ds:FIGHT AT (%d,%d)\n",++ans,i/100,i%100); }}void bfs(int n,int m){ int i,j,k,xx,yy,num; node f,g; memset(pre,-1,sizeof(vis)); for(i=0;i<n;i++) for(j=0;j<m;j++) vis[i][j]=INF; vis[0][0]=0; queue<node>q; f.x=0;f.y=0;f.num=0; MinDis=INF; q.push(f); while(!q.empty()) { f=q.front(); q.pop(); if(f.x==n-1&&f.y==m-1) MinDis=min(MinDis,f.num); for(i=0;i<4;i++) { xx=f.x+dir[i][0]; yy=f.y+dir[i][1]; if(e[f.x][f.y]=='.')num=f.num+1; else num=f.num+1+(e[f.x][f.y]-'0'); if(xx<0||yy<0||xx>=n||yy>=m||e[xx][yy]=='X')continue; if(num>=vis[xx][yy])continue; vis[xx][yy]=num; pre[xx*100+yy]=f.x*100+f.y; g.x=xx; g.y=yy; g.num=num; q.push(g); } } if(pre[(n-1)*100+m-1]==-1)printf("God please help our poor hero.\nFINISH\n"); else { if(e[n-1][m-1]!='.') MinDis+=(e[n-1][m-1]-'0'); printf("It takes %d seconds to reach the target position, let me show you the way.\n",MinDis); ans=0; show((n-1)*100+(m-1)); printf("FINISH\n"); }}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { int i,j,k; for(i=0;i<n;i++) scanf("%s",e[i]); if(e[0][0]=='X'||e[n-1][m-1]=='X') { printf("God please help our poor hero.\nFINISH\n"); continue; } bfs(n,m); } return 0;}
0 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)
- 华丽的HTML5/jQuery动画和应用 前端必备
- 二分图的最大匹配、完美匹配和匈牙利算法
- LCS
- ava.net.BindException: Problem binding to [ubuntu-master:9000] java.net.BindException: 地址已在使用; For m
- 【学生管理系统】EOF和BOF
- hdu 1026 Ignatius and the Princess I BFS
- (36)JS运动之使物体向右运动
- 手机管理应用研究【5】——应用杂篇
- HDU 4814 Golden Radio Base 小模拟
- poj 3255 Roadblocks Dijkstra求次短路
- 快速幂模
- BI(商务智能)系统简介
- 学习笔记_毕向东 Java_集合框架_LinkedList 2014.7.22
- 常用入口