HDU1026 (BFS)

题意:从起点到终点,输出路径长度,时间。

bfs+记录路径

View Code
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<queue>
  5 const int N = 105;
  6 const int inf = 9999999;
  7 using namespace std;
  8 
  9 struct node{
 10     int x,y;
 11 };
 12 node p,pp,path[N][N];
 13 struct node2{
 14     int x1,y1,x2,y2;
 15 }out[1000005];
 16 
 17 char map[N][N];
 18 int time[N][N];
 19 int n,m,endflag,ans;
 20 queue<node>q;
 21 
 22 const int dx[]={1,-1,0,0};
 23 const int dy[]={0,0,1,-1};
 24 
 25 void init(){
 26     for(int i=0;i<N;i++)
 27         for(int j=0;j<N;j++){
 28             path[i][j].x=-1;
 29             path[i][j].y=-1;
 30             time[i][j]=inf;
 31         }
 32 }
 33 
 34 bool judge(int x,int y){
 35     if(map[x][y]>='1'&&map[x][y]<='9')
 36         return true;
 37     return false;
 38 }
 39 
 40 void output(){
 41     int nowx,nowy,tpx,tpy,cnt=1;
 42     nowx=n-1;
 43     nowy=m-1;
 44     while(nowx!=0||nowy!=0){
 45         out[cnt].x1=path[nowx][nowy].x;
 46         out[cnt].y1=path[nowx][nowy].y;
 47         out[cnt].x2=nowx;
 48         out[cnt++].y2=nowy;
 49         tpx=nowx;
 50         tpy=nowy;
 51         nowx=path[tpx][tpy].x;
 52         nowy=path[tpx][tpy].y;
 53     }
 54 
 55     int cnt2=1;
 56     for(int i=cnt-1;i>=1;i--)
 57     {
 58         if(judge(out[i].x1,out[i].y1)==false)
 59             printf("%ds:(%d,%d)->(%d,%d)\n",cnt2++,out[i].x1,out[i].y1,out[i].x2,out[i].y2);
 60         else
 61         {
 62             int pp;
 63             pp=map[out[i].x1][out[i].y1]-'0';
 64             for(int j=1;j<=pp;j++)
 65                 printf("%ds:FIGHT AT (%d,%d)\n",cnt2++,out[i].x1,out[i].y1);
 66             printf("%ds:(%d,%d)->(%d,%d)\n",cnt2++,out[i].x1,out[i].y1,out[i].x2,out[i].y2);
 67         }
 68     }
 69     if(endflag!=-1){
 70     int pp;
 71     pp=endflag;
 72         for(int j=1;j<=pp;j++)
 73             printf("%ds:FIGHT AT (%d,%d)\n",cnt2++,n-1,m-1);
 74     }
 75     printf("FINISH\n");
 76 }
 77 
 78 void bfs(){
 79     int i,j,k;
 80     ans=inf;
 81     endflag=-1;
 82     while(!q.empty())q.pop();
 83     p.x=0;
 84     p.y=0;
 85     time[p.x][p.y]=0;
 86     if(judge(0,0)==true)
 87         time[0][0]=map[0][0]-'0';
 88     q.push(p);
 89     while(!q.empty()){
 90         p=q.front();
 91         q.pop();
 92         
 93         if(p.x==n-1&&p.y==m-1)
 94             if(time[n-1][m-1]<ans)
 95                 ans=time[n-1][m-1];
 96                 
 97         for(i=0;i<4;i++){
 98             pp.x=p.x+dx[i];
 99             pp.y=p.y+dy[i];
100             if(pp.x<0||pp.x>=n||pp.y<0||pp.y>=m)
101                 continue;
102             if(map[pp.x][pp.y]=='X')
103                 continue;
104             if(map[pp.x][pp.y]=='.'&&time[pp.x][pp.y]>(time[p.x][p.y]+1)){
105                 time[pp.x][pp.y]=time[p.x][p.y]+1;
106                 path[pp.x][pp.y].x=p.x;
107                 path[pp.x][pp.y].y=p.y;
108                 q.push(pp);
109             }
110             if(map[pp.x][pp.y]>='1'&&map[pp.x][pp.y]<='9'){
111                 if(time[pp.x][pp.y]>(time[p.x][p.y]+map[pp.x][pp.y]-'0'+1)){
112                     time[pp.x][pp.y]=time[p.x][p.y]+map[pp.x][pp.y]-'0'+1;
113                     path[pp.x][pp.y].x=p.x;
114                     path[pp.x][pp.y].y=p.y;
115                     if(pp.x==n-1&&pp.y==m-1){
116                         endflag=map[pp.x][pp.y]-'0';
117                     }
118                     q.push(pp);
119                 }
120             }
121         }
122     }
123     
124     if(ans!=inf){
125         printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
126         output();
127     }
128     else
129         printf("God please help our poor hero.\nFINISH\n");
130     return ;
131 }
132 
133 int main(){
134     int i;
135     while( scanf("%d%d",&n,&m)!=EOF ){
136         memset(map,0,sizeof(map));
137         for(i=0;i<n;i++)
138             scanf("%s",map[i]);
139         init();
140         bfs();
141     }
142     return 0;
143 }            

 

posted @ 2013-01-29 09:28  xxx0624  阅读(504)  评论(0编辑  收藏  举报