hdu 1026 Ignatius and the Princess I

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=110;
int book[N][N];
char a[N][N];
struct f{
int x,y,step;
int early;//储存来源下标;
int xx;//打怪次数
}b[N*N*4];
bool cmp(f p,f q){
return p.step<q.step;
}
int c[N*N];//中间数组
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        for(int i=0;i<n;i++)
          scanf("%s",a[i]);
          int head=0,tail=1,flag=0,l=0;
          memset(book,0,sizeof(book));
          memset(b,0,sizeof(b));
          book[0][0]=1;
          b[head].x=0,b[head].y=0,b[head].step=0,b[head].early=-1,b[head].xx=0;
          while(head<tail){
                sort(b+head,b+tail,cmp);//最小步优先
            int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//全局变量hdu竟编译错误
              for(int i=0;i<4;i++)
              {
                  int tx=b[head].x+next[i][0],ty=b[head].y+next[i][1];
                      if(tx<0||tx>=n||ty<0||ty>=m) continue;
                  if(!book[tx][ty]&&a[tx][ty]!='X'){
                    book[tx][ty]=1;
                    b[tail].x=tx,b[tail].y=ty;
                    if(a[tx][ty]=='.') {
                            b[tail].step=b[head].step+1;
                            b[tail].xx=0;
                            }
                    else {
                            b[tail].step=b[head].step+(a[tx][ty]-'0')+1;
                            b[tail].xx=(a[tx][ty]-'0');
                    }
                    b[tail++].early=head;
                    if(tx==n-1&&ty==m-1){flag=1;break;}
                  }
              }
              if(flag) break;
            head++;
          }
          int k=0,p=tail-1,ti=1;
           c[k++]=p;
          while(1){
               p=b[p].early;
               if(p==-1) break;
               c[k++]=p;
          }
          if(flag){
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",b[tail-1].step);
            for(int i=k-1;i>=1;i--)
            { 
             while(b[c[i]].xx--)
                    printf("%ds:FIGHT AT (%d,%d)\n",ti++,b[c[i]].x,b[c[i]].y);
             printf("%ds:(%d,%d)->(%d,%d)\n",ti++,b[c[i]].x,b[c[i]].y,b[c[i-1]].x,b[c[i-1]].y);
            }
             while(b[c[0]].xx--)
                 printf("%ds:FIGHT AT (%d,%d)\n",ti++,b[c[0]].x,b[c[0]].y);
          }
         else{printf("God please help our poor hero.\n");}
         printf("FINISH\n");
    }
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值