5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
1.一定要注意细节 经常有字母打错 找了好久BUG
2.理清思路
3.第二种方法中不明白为什么用ckeck代替if条件语句就错误,求广大网友指正
以下给出两个路径输出的方法
第一种是DFS递归调用
第二种方法是stack的使用
/*
优先队列
BUG
1.
在BFS里面可以输出step
return 返回到主函数后 输出乱码
2.
终点如果还有数字 不会加上终点的数字
3.
n.y<m打成n.y<n
4.注意
0 0点如果有怪物的话 忽略这个怪物
*/
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
int x,y,step;
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
};
char map[105][105];
int hp[105][105];
int vis[105][105];
int n,m;
int stepp[4][2]= {1,0,-1,0,0,1,0,-1}; //下上右左
int minn;
int time;
int flag;
int pre[105][105];
bool check(int x,int y)
{
if(x<0||y<0||x>=n||y>=m)//越界
return false;
if(map[x][y]=='X')//墙
return false;
if(vis[x][y]==true)//走过了
return false;
return true;
}
void BFS()
{
int bx,by;
priority_queue<node> q;
node a,b;
a.x=0;
a.y=0;
a.step=0;
memset(vis,0,sizeof(vis));
vis[0][0]=1;
q.push(a);
while(!q.empty())
{
a=q.top();
q.pop();
//printf("%d %d %d\n",a.x,a.y,a.step);
if(a.x == n-1 && a.y == m-1)
{
flag=1;
minn=a.step;
return ;
}
for(int i=0; i<4; i++)
{
b.x=a.x+stepp[i][0];
b.y=a.y+stepp[i][1];
//if(check(bx,by))//如果能走
if(check(b.x,b.y))
{
//b.y<m写成b.y<n 妈的
b.step=a.step+hp[b.x][b.y]+1;
pre[b.x][b.y]=i+1;//0 0 点 的pre[0][0]是0
vis[b.x][b.y]=1;
q.push(b);
}
}
}
//return ;
}
void print(int x,int y)
{
int nx,ny;
if(!pre[x][y]) //当找到0 0 点 返回 0 0点如果有怪物的话 忽略这个怪物
return ;
nx=x-stepp[pre[x][y]-1][0];
ny=y-stepp[pre[x][y]-1][1];
print(nx,ny);
printf("%ds:(%d,%d)->(%d,%d)\n",time++,nx,ny,x,y);
while(hp[x][y]--)
{
printf("%ds:FIGHT AT (%d,%d)\n",time++,x,y);
}
}
int main(void)
{
while(~scanf("%d%d",&n,&m))
{
memset(pre,0,sizeof(pre));
for(int i=0; i<n; i++)
{
scanf("%s",map[i]);
for(int ii=0; ii<m; ii++)
{
if(map[i][ii]=='.')
hp[i][ii]=0;
else if(map[i][ii]=='X')
hp[i][ii]=-1;
else
hp[i][ii]=map[i][ii]-'0';
}
}
flag=0;
BFS();//printf("111");
if(flag)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",minn);
time=1;
print(n-1,m-1);
}
else
printf("God please help our poor hero.\n");
printf("FINISH\n");
}
return 0;
}
第二种:
#include<iostream>
#include<queue>
#include<stack>
#include<cstdio>
#include<string.h>
using namespace std;
struct node
{
int x;
int y;
int step;
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
} a,b;
struct node1
{
int x;
int y;
int num;
} pre[105][105];
int hp[105][105];
int step[4][2]= {1,0,-1,0,0,1,0,-1};
int vis[105][105];
int m,n;
int minn;
char map[105][105];
int check(int x,int y)//用check就错误 不知道为什么
{
if(x<n&&x>=0&&y>=0&&y<m&&!vis[x][y]&&map[x][y]!='X')
return 1;
return 0;
}
void BFS(int x,int y)
{
priority_queue<node> q;
int bx,by;
int vis[105][105];
memset(vis,0,sizeof(vis));
vis[0][0]=1;
a.x=0;
a.y=0;
a.step=0;
minn=0;
q.push(a);
while(!q.empty())
{
a=q.top();
// printf("%d %d %d \n",a.x,a.y,a.step);
q.pop();
if(a.x==x&&a.y==y)
{
minn=a.step;
return ;
}
for(int i=0; i<4; i++)
{
bx=a.x+step[i][0];
by=a.y+step[i][1];
if(bx>=0 && bx<n && by>=0 && by<m && !vis[bx][by] && map[bx][by]!='X')
//if(check(bx,by))
{
vis[bx][by]=1;
b.step=a.step+hp[bx][by];
b.x=bx;
b.y=by;
q.push(b);
pre[bx][by].x=a.x;//pre的当前节点 记录上一个点的坐标 和 血量
pre[bx][by].y=a.y;
pre[bx][by].num=hp[a.x][a.y];
}
}
}
return ;
}
void print()
{
int time=1;
int x1,x2,y1,y2;
stack<node1> Q;
node1 aa,bb;
bb.x=n-1;
bb.y=m-1;
bb.num=hp[n-1][m-1];//之前输入 bb.num=pre[n-1][m-1].pre;如果终点(n-1,m-1)的HP是1 bb.num是1, 而不是2 应该把 2传给bb.num(走到time+1 杀怪time+1)
Q.push(bb);
for(x1=n-1, y1=m-1;;)
{
x2=x1;
y2=y1;
Q.push(pre[x2][y2]);
x1=pre[x2][y2].x;
y1=pre[x2][y2].y;
if(x1==0&&y1==0)break;
}
while(!Q.empty())
{
if(Q.size()-1==0&&Q.top().num==1)//最后一个跳出条件
break;
aa=Q.top();
Q.pop();
if(Q.size()>0)
bb=Q.top();
if(aa.num==1)
printf("%ds:(%d,%d)->(%d,%d)\n",time++,aa.x,aa.y,bb.x,bb.y);
else
{
for(int i=1; i<aa.num; i++)
{
printf("%ds:FIGHT AT (%d,%d)\n",time++,aa.x,aa.y);
}
if(Q.size()>0)
printf("%ds:(%d,%d)->(%d,%d)\n",time++,aa.x,aa.y,bb.x,bb.y);
}
}
}
int main (void)
{
int x1,y1;
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
{
scanf("%s",map[i]);
for(int ii=0; ii<m; ii++)
{
if(map[i][ii]=='.')
hp[i][ii]=1;
else if(map[i][ii]=='X')
hp[i][ii]=-1;
else
hp[i][ii]=map[i][ii]-'0'+1;
}
}
BFS(n-1,m-1);
if(minn)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",minn);
print();
}
else
printf("God please help our poor hero.\n");
printf("FINISH\n");
}
return 0;
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。