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; }
相关文章推荐
- 搜索(HDU 1026)
- 八数码(hdu 1043)
- 一道hash题
- NOIP2010 饮水入城
- hdu1241Oil Deposits 深广搜
- poj1753 Flip Game 广搜哈希
- zoj 1148 The Game 一个晚上终于AC!
- Dungeon Master (广搜,bfs)
- 迷宫问题(广搜 bfs)
- hdoj 2612 Find a way简单广搜
- LeetCode之Word Ladder
- HDU2612,简单广搜题
- FZU2150,暴力广搜
- Gym100187E,Two Labyrinths,广搜
- USACO2011Open Silver Unlocking Blocks 题解
- hdoj1072,Nightmare
- hdoj1044,Collect More Jewels
- hdoj1043,eight
- Hdoj1206,Ignatius and the Princess I
- 广搜状态压缩 OpenJudge 百炼4105