HDU 1026 Ignatius and the Princess I
来源:互联网 发布:淘宝做什么产品比较好 编辑:程序博客网 时间:2024/06/05 03:42
杭电 1026 拯救公主
本来刚刚写了一次的,结果最后不知怎么没保存好,唉........直接来代码吧.
#include<stdio.h>#include<string.h>#define M 105#define max 10000000char map[M][M];int mark[M][M];int father[M][M][2];int n, m;struct node{ int x,y,step;} Q[M*M*M],s,p,ye[M*M];int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; void print(){ int i,j,k,t=0,x,y,num,x1,y1; printf("It takes %d seconds to reach the target position, let me show you the way.\n", mark[n - 1][m - 1]); x1=n-1;y1=m-1; while(x1!=0||y1!=0){ s.x=x1;s.y=y1;s.step=mark[s.x][s.y]; ye[++t]=s; x=father[x1][y1][0]; y=father[x1][y1][1]; x1=x;y1=y; } k=1; printf("1s:(0,0)->(%d,%d)\n",ye[t].x,ye[t].y); x=ye[t].x;y=ye[t].y;num=ye[t--].step; k++; while(t>0){ while(k<=num) {printf("%ds:FIGHT AT (%d,%d)\n",k,x,y);k++;} if(x!=n-1||y!=m-1) { printf("%ds:(%d,%d)->(%d,%d)\n",k++,x,y,ye[t].x,ye[t].y); x=ye[t].x;y=ye[t].y;num=ye[t--].step; } } while(k<=num) {printf("%ds:FIGHT AT (%d,%d)\n",k,x,y);k++;}}void bfs(){ int i,j,l,h; s.x=0;s.y=0;s.step=0; l=0;h=0; Q[l++]=s; for(i=0;i<=n;i++) for(j=0;j<=m;j++)mark[i][j]=max; if(map[0][0]=='.')mark[0][0]=0;elsemark[0][0]=(map[0][0]-'0');s.step+=mark[0][0]; while(h<l){ p=Q[h++]; for(i=0;i<4;i++){s = p;s.x+=dx[i];s.y+=dy[i]; if (s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&map[s.x][s.y]!='X') { s.step++; if(map[s.x][s.y]!='.')s.step+=(map[s.x][s.y]-'0'); //加上杀死怪物的时间 if(mark[s.x][s.y]>s.step){ mark[s.x][s.y]=s.step; father[s.x][s.y][0]=p.x;//前一点x坐标 father[s.x][s.y][1]=p.y;//前一点y坐标 Q[l++]=s; } } } } if(mark[n-1][m-1]==max) printf("God please help our poor hero.\n"); else print();}int main(){int i,j;while(scanf("%d%d",&n,&m)!=EOF){for(i=0;i<n;i++){getchar();for(j=0;j<m;j++){scanf("%c",&map[i][j]); //输入矩阵 }}bfs(); printf("FINISH\n"); //scanf("%d%d",&n,&m);}return 0;}
拯救公主,从(0,0)出发,终点(n.m),有一个结构体保存步骤...............
0 0
- hdu 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- hdu - 1026 - Ignatius and the Princess I
- HDU-1026Ignatius and the Princess I
- HDU-1026-Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- hdu-1026-Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I
- HDU - 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- /proc/sys/kernel/printk
- 【P000-009】分析模块——四大类
- ACM steps quicksum
- 写一网络爬虫有感
- Jquery(Ajax)高级UI设计
- HDU 1026 Ignatius and the Princess I
- 数论合集 I
- GPS坐标转百度地图坐标
- 韩信点兵-中国剩余定理(练习)
- C,C++表达式求值顺序
- javascript基础知识梳理-DOM
- JavaSE20_总结之Java变量类型识别的方式
- c++随机数生成
- char、wchar_t、T_char