您的位置:首页 > 其它

hdu 1026 Ignatius and the Princess I

2016-07-19 18:15 417 查看
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=110;
int book

;
char a

;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  广搜