标签:
5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX. 5 6 .XX.1. ..X.2. 2...X. ...XX. XXXXX1 5 6 .XX... ..XX1. 2...X. ...XX. XXXXX.
It takes 13 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) FINISH It takes 14 seconds to reach the target position, let me show you the way. 1s:(0,0)->(1,0) 2s:(1,0)->(1,1) 3s:(1,1)->(2,1) 4s:(2,1)->(2,2) 5s:(2,2)->(2,3) 6s:(2,3)->(1,3) 7s:(1,3)->(1,4) 8s:FIGHT AT (1,4) 9s:FIGHT AT (1,4) 10s:(1,4)->(1,5) 11s:(1,5)->(2,5) 12s:(2,5)->(3,5) 13s:(3,5)->(4,5) 14s:FIGHT AT (4,5) FINISH God please help our poor hero. FINISH
题意:从(0,0)到(n-1,m-1)‘.‘代表路,‘X‘代表障碍,‘n’带表怪物血量,打过就可以过了,消耗时间n
题目思想:
广搜(bfs)
这是我第一次接触到广搜,就大致查询了一下广搜,再结合以前的深搜,总结如下:
深搜是与栈有关:
拿树来做例子,深搜是把树的某个分支进行到底再查找另一个分支,直到满足条件结束或者分支找完结束
再说说与栈的关系:栈,先进后出,即把树的根放在栈底上面的栈内元素就可以依次换元,就可以遍历完整个结构
再说说在图中:即从起始点走到终点再返回一步再往下走,直到满足条件或者遍历完成
广搜是与队有关:#include<iostream> #include<queue> #include<stack> #include<stdlib.h> #include<iomanip> using namespace std; int n,m; char map[101][101]; typedef struct point { int x,y,step,time;//step用于记录到此点的方向,step=1,2,3,4 ->上下左右 }*prt;//因为队中元素需要修改,用指针 prt t[100][100]; bool cheak(int x,int y)//判断是否可达 { if(x<0||y<0||x>=n||y>=m||map[x][y]=='X') return false; return true; } //广搜遍历所有可能 void bfs() { queue<prt> Q;//因为队中元素需要修改,用指针 prt corrent; int x,y,k,i; bool flag; t[0][0]->time=0;//出发点时间置0 Q.push(t[0][0]);//出发点进队 while(!Q.empty()) { corrent=Q.front(); x=corrent->x; y=corrent->y; Q.pop(); //对于上下左右四个点进行查看: // 如果可以达到,并且从当前点走过去比以前方式到达他的时间小 // 就将这个点进队 // 这里既有初始情况到达,也有事后修改,并且修改需要修改以后所有点,所以也需要进队(即最后样例所给) if(cheak(x-1,y)&&corrent->time+map[x-1][y]-'0'+1<t[x-1][y]->time) { t[x-1][y]->time=corrent->time+map[x-1][y]-'0'+1; Q.push(t[x-1][y]); t[x-1][y]->step=4; } if(cheak(x+1,y)&&corrent->time+map[x+1][y]-'0'+1<t[x+1][y]->time) { t[x+1][y]->time=corrent->time+map[x+1][y]-'0'+1; Q.push(t[x+1][y]); t[x+1][y]->step=3; } if(cheak(x,y-1)&&corrent->time+map[x][y-1]-'0'+1<t[x][y-1]->time) { t[x][y-1]->time=corrent->time+map[x][y-1]-'0'+1; Q.push(t[x][y-1]); t[x][y-1]->step=2; } if(cheak(x,y+1)&&corrent->time+map[x][y+1]-'0'+1<t[x][y+1]->time) { t[x][y+1]->time=corrent->time+map[x][y+1]-'0'+1; Q.push(t[x][y+1]); t[x][y+1]->step=1; } } } int main() { int k,i,order,time; while(cin>>n>>m) { for(k=0;k<n;k++) for(i=0;i<m;i++) { t[k][i]=new point; cin>>map[k][i]; if(map[k][i]=='.') map[k][i]='0'; t[k][i]->x=k; t[k][i]->y=i; t[k][i]->time=10000000; t[k][i]->step=-1; } t[0][0]->step=1; bfs(); if(t[n-1][m-1]->time<10000000)//如果可以到达目的地 { cout<<"It takes "<<t[n-1][m-1]->time<<" seconds to reach the target position, let me show you the way."<<endl; stack<point> S; point p,last; k=n-1; i=m-1; while(k>=0&&i>=0)//由最后点按照每一点的step找到路线 ,找到的上一点都入栈 { p.x=k; p.y=i; S.push(p); order=t[k][i]->step; if(order==4) k+=1; if(order==3) k-=1; if(order==2) i+=1; if(order==1) i-=1; } p=S.top(); S.pop(); time=1; while(!S.empty())//输出栈 { last=p; p=S.top(); cout<<(time++)<<"s:("<<last.x<<","<<last.y<<")->("<<p.x<<","<<p.y<<")"<<endl; for(k=0;k<map[p.x][p.y]-'0';k++)//输出在某点打怪消耗时间 cout<<(time++)<<"s:FIGHT AT ("<<p.x<<","<<p.y<<")"<<endl; S.pop(); } } else cout<<"God please help our poor hero."<<endl; cout<<"FINISH"<<endl; for(k=0;k<n;k++)//删除申请空间 for(i=0;i<m;i++) delete t[k][i]; } return 0; }
测试样例:
5 6
.XX.1.
..X.9.
2...X.
...XX.
XXXXX.
hdu 1026 Ignatius and the Princess I(bfs)
标签:
原文地址:http://blog.csdn.net/lym77777/article/details/51346342