hdu 1026 Ignatius and the Princess I
来源:互联网 发布:数据分析报告范文 编辑:程序博客网 时间:2024/06/03 23:56
/*
题意:给一个n*m的图,图中有X,有.有数字,x代表墙,.代表路,数字代表当走到这个地方的时候,还要停留多少
秒,问你从左上角到右下角的最少时间是多少。当能走通的时候,要把路径打印出来。
除了Map数组和标记数组之外,还应该有一个结构体数组存放当前点是由那个点广搜过来的
*/
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
char Map[105][105];//存图
int Map1[105][105]/*标记数组*/,bu[4][2]={0,1,0,-1,1,0,-1,0},n,m;
struct node
{
int x,y,temp;
friend bool operator <(node p,node q)
{
return p.temp>q.temp;
}
}S,D;
node MM[105][105];//结构体数组存放该点是由那个点广搜过来的
int bfs()
{
memset(Map1,0,sizeof(Map1));
priority_queue<node> v;
while(!v.empty())
v.pop();
node l,r;
int i;
v.push(S);
Map1[S.x][S.y]=1;
while(!v.empty())
{
l=v.top();
//printf("++++%d %d %d\n",l.x,l.y,l.temp);
v.pop();
if(l.x==D.x&&l.y==D.y)
return l.temp;
for(i=0;i<4;i++)
{
int aa=l.x+bu[i][0];
int bb=l.y+bu[i][1];
if(aa>=0&&aa<n&&bb>=0&&bb<m&&Map1[aa][bb]==0&&Map[aa][bb]!='X')
{
Map1[aa][bb]=1;
MM[aa][bb].x=l.x;MM[aa][bb].y=l.y;
r.x=aa;r.y=bb;
if(Map[aa][bb]=='.')
r.temp=l.temp+1;
else
r.temp=l.temp+Map[aa][bb]-'0'+1;
//printf("%d %d %d\n",r.x,r.y,r.temp);
v.push(r);
}
}
}
return -1;
}
void Printf(int aa,int nn,int mm)//通过递归输出图
{
if(aa==1)//当aa是1秒的时候开始输出
{
printf("%ds:(%d,%d)->(%d,%d)\n",aa,MM[nn][mm].x,MM[nn][mm].y,nn,mm);
return ;
}
else
{
if(Map[nn][mm]>'0'&&Map[nn][mm]<='9')//当时数字的时候输出这么多次
{
Map[nn][mm]-=1;//图中的数字减1
Printf(aa-1,nn,mm);//先递归,当递归回来时在输出图
printf("%ds:FIGHT AT (%d,%d)\n",aa,nn,mm);//输出图
}
else
{
Printf(aa-1,MM[nn][mm].x,MM[nn][mm].y);//当图中是点的时候先递归后输出图
printf("%ds:(%d,%d)->(%d,%d)\n",aa,MM[nn][mm].x,MM[nn][mm].y,nn,mm);//输出图
}
}
}
int main()
{
int i;
S.x=0;S.y=0;
while(scanf("%d%d",&n,&m)!=EOF)
{
D.x=n-1;D.y=m-1;
for(i=0;i<n;i++)
scanf("%s",Map[i]);
int aa=bfs();
if(aa==-1)
{
printf("God please help our poor hero.\nFINISH\n");
}
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",aa);
Printf(aa,n-1,m-1);//输出图的函数
printf("FINISH\n");
}
}
return 0;
}
题意:给一个n*m的图,图中有X,有.有数字,x代表墙,.代表路,数字代表当走到这个地方的时候,还要停留多少
秒,问你从左上角到右下角的最少时间是多少。当能走通的时候,要把路径打印出来。
除了Map数组和标记数组之外,还应该有一个结构体数组存放当前点是由那个点广搜过来的
*/
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
char Map[105][105];//存图
int Map1[105][105]/*标记数组*/,bu[4][2]={0,1,0,-1,1,0,-1,0},n,m;
struct node
{
int x,y,temp;
friend bool operator <(node p,node q)
{
return p.temp>q.temp;
}
}S,D;
node MM[105][105];//结构体数组存放该点是由那个点广搜过来的
int bfs()
{
memset(Map1,0,sizeof(Map1));
priority_queue<node> v;
while(!v.empty())
v.pop();
node l,r;
int i;
v.push(S);
Map1[S.x][S.y]=1;
while(!v.empty())
{
l=v.top();
//printf("++++%d %d %d\n",l.x,l.y,l.temp);
v.pop();
if(l.x==D.x&&l.y==D.y)
return l.temp;
for(i=0;i<4;i++)
{
int aa=l.x+bu[i][0];
int bb=l.y+bu[i][1];
if(aa>=0&&aa<n&&bb>=0&&bb<m&&Map1[aa][bb]==0&&Map[aa][bb]!='X')
{
Map1[aa][bb]=1;
MM[aa][bb].x=l.x;MM[aa][bb].y=l.y;
r.x=aa;r.y=bb;
if(Map[aa][bb]=='.')
r.temp=l.temp+1;
else
r.temp=l.temp+Map[aa][bb]-'0'+1;
//printf("%d %d %d\n",r.x,r.y,r.temp);
v.push(r);
}
}
}
return -1;
}
void Printf(int aa,int nn,int mm)//通过递归输出图
{
if(aa==1)//当aa是1秒的时候开始输出
{
printf("%ds:(%d,%d)->(%d,%d)\n",aa,MM[nn][mm].x,MM[nn][mm].y,nn,mm);
return ;
}
else
{
if(Map[nn][mm]>'0'&&Map[nn][mm]<='9')//当时数字的时候输出这么多次
{
Map[nn][mm]-=1;//图中的数字减1
Printf(aa-1,nn,mm);//先递归,当递归回来时在输出图
printf("%ds:FIGHT AT (%d,%d)\n",aa,nn,mm);//输出图
}
else
{
Printf(aa-1,MM[nn][mm].x,MM[nn][mm].y);//当图中是点的时候先递归后输出图
printf("%ds:(%d,%d)->(%d,%d)\n",aa,MM[nn][mm].x,MM[nn][mm].y,nn,mm);//输出图
}
}
}
int main()
{
int i;
S.x=0;S.y=0;
while(scanf("%d%d",&n,&m)!=EOF)
{
D.x=n-1;D.y=m-1;
for(i=0;i<n;i++)
scanf("%s",Map[i]);
int aa=bfs();
if(aa==-1)
{
printf("God please help our poor hero.\nFINISH\n");
}
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",aa);
Printf(aa,n-1,m-1);//输出图的函数
printf("FINISH\n");
}
}
return 0;
}
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
- 第九周 结果预测(二)
- Oracle数据库,resultSet.getObject获取时间类型问题的解析
- 新生选拔赛一
- 多人即时战斗游戏服务端系列[3]--日志类及Config类以及Excel生成类
- 系统分析与设计(技术专家)讲师
- hdu 1026 Ignatius and the Princess I
- [LeetCode] 3Sum Closest
- [LeetCode] 3Sum Solution
- C++ 虚函数与纯虚函数
- 第12周项目1.5
- 禁止Html5在手机上屏幕页面缩放
- 求一个字符串的最长连续回文串(模板)
- 手把手教你做iOS推送
- sql server 2012 或 2014 连接不上服务器