题解HDU1026
来源:互联网 发布:看图编辑软件 编辑:程序博客网 时间:2024/05/22 04:53
//求迷宫最短路问题;
//在上面加了一些东西;
//最短路 的话我们首先想到的一般都是BFS
//那么问题主要记录路径的问题
#include<iostream>#include<string.h>#include<queue>using namespace std;const int MAX=999999;struct dian{ int x; int y; int quan; bool operator < (const dian &a) const { return quan>a.quan ;//最小值优先 }};int n,m;int lu[101][101];int visit[101][101];char maze[101][101];int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};int bfs() { int i,j; priority_queue<dian>p; for(i=0;i<n;i++) { for(j=0;j<m;j++) { lu[i][j]=MAX; } } lu[0][0]=0; dian arry; arry.x=0; arry.y=0; arry.quan=0; p.push(arry); while(p.size()) { dian j1=p.top(); p.pop(); for(i=0;i<4;i++) { int nx=j1.x+dx[i]; int ny=j1.y+dy[i]; if(nx>=0&&nx<n&&ny>=0&&ny<m&&maze[nx][ny]!='X'&&lu[nx][ny]==MAX) { dian wu; wu.x=nx; wu.y=ny; visit[nx][ny]=i+1; if(maze[nx][ny]=='.') { if(lu[nx][ny]>lu[j1.x][j1.y]+1) { lu[nx][ny]=lu[j1.x][j1.y]+1; wu.quan=lu[nx][ny]; p.push(wu); } } else { if(lu[nx][ny]>(lu[j1.x][j1.y]+maze[nx][ny]-'0'+1)) { lu[nx][ny]=lu[j1.x][j1.y]+maze[nx][ny]-'0'+1; wu.quan=lu[nx][ny]; p.push(wu); } } } } } return lu[n-1][m-1];}int temp;void lujing(int x,int y){ if(visit[x][y]==0) return ; int nx=x-dx[visit[x][y]-1]; int ny=y-dy[visit[x][y]-1]; lujing(nx,ny); cout<<temp++<<'s'<<":"<<'('<<nx<<','<<ny<<')'<<"->"<<'('<<x<<','<<y<<')'<<endl; if(maze[x][y]!='X'&&maze[x][y]!='.') { int a=maze[x][y]-'0'; while(a--) cout<<temp++<<'s'<<":"<<"FIGHT AT "<<'('<<x<<','<<y<<')'<<endl; }}int main(){ while(scanf("%d%d",&n,&m)!=EOF) { int i,j; temp=1; for(i=0;i<n;i++) { for(j=0;j<m;j++) cin>>maze[i][j]; } memset(visit,0,sizeof(visit)); int ans=bfs(); if(ans==MAX) cout<<"God please help our poor hero."<<endl; else { cout<<"It takes "<<ans<<" seconds to reach the target position, let me show you the way."<<endl; lujing(n-1,m-1); }cout<<"FINISH"<<endl; }}
0 0
- hdu1026题解
- 题解HDU1026
- hdu1026
- hdu1026
- hdu1026
- HDU1026
- hdu1026
- hdu1026
- HDU1026
- HDU1026
- hdu1026
- HDU1026
- HDU1026
- hdu1026
- hdu1026 bfs
- hdu1026和hdu1242
- hdu1026还是搜索
- hdu1026 BFS+记录路径
- win7环境下visualsvn-server的搭建与使用(Eclipse)整合版
- cf476D构造
- 程序语言的底层描述(5)——数组、指针的汇编实现以及C程序嵌入汇编
- gggggggggggg
- HDU 1729
- 题解HDU1026
- so库的性能注意事项
- AdaBoost的人脸识别—Haar特征与积分图
- NYOJ 821 简单求值【简单题】
- 《精通struts2实用教程》学习笔记-01
- Linux修改用户的操作权限
- js数组操作
- HDOJ题目3038 How Many Answers Are Wrong(带权值并查集)
- 《高可用MySQL》2 – 单机版MySQL主从配置