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
这个题目就是一道简单的优先队列+BFS能过的题目,但是输出明显墨迹的要死。。。。我们这里先说明为什么要加入优先队列呢?因为我们要干掉卫兵的存在。
我们都知道,BFS有贪心的特性,也可以说BFS有路径优先的特性,但是我们这里要的是时间优先,并不是路径优先,所以呢,我们这里就要应用优先队列改变优先条件,让我们控制的不再是路径优先,变成了时间优先就行了。
那么我们怎么处理这个输出结果呢?
我们这里来谈一下初始化,相信大家就会有相应的思路了:
#include<stdio.h> #include<string.h> #include<queue> using namespace std;//头文件 struct zuobiao { int x,y,output; friend bool operator <(zuobiao a,zuobiao b) { return a.output>b.output; }//优先队列output int zoufa[1000][2];//走法的记录。 }now,nex; int vis[1000][1000];//访问数组 char a[1000][1000];//图 int n,m; int fx[4]={0,0,1,-1};//走法 int fy[4]={1,-1,0,0};
void bfs(int x,int y) { memset(vis,0,sizeof(vis)); priority_queue<zuobiao>s; now.x=x; now.y=y; now.output=0; now.zoufa[0][0]=x; now.zoufa[0][1]=y; vis[x][y]=1; s.push(now); while(!s.empty()) { now=s.top(); if(now.x==n-1&&now.y==m-1)//如果走到了终点,按需求输出,这里边的东西比较好理解,就不啰嗦了。 { int bijiaox=0,bijiaoy=0; printf("It takes %d seconds to reach the target position, let me show you the way.\n",now.output); for(int i=0;i<now.output;i++) { if(bijiaox==now.zoufa[i+1][0]&&bijiaoy==now.zoufa[i+1][1]) { printf("%ds:FIGHT AT (%d,%d)\n",i+1,now.zoufa[i+1][0],now.zoufa[i][1]); bijiaox=now.zoufa[i+1][0]; bijiaoy=now.zoufa[i+1][1]; continue; } printf("%ds:(%d,%d)->(%d,%d)\n",i+1,now.zoufa[i][0],now.zoufa[i][1],now.zoufa[i+1][0],now.zoufa[i+1][1]); bijiaox=now.zoufa[i+1][0]; bijiaoy=now.zoufa[i+1][1]; } printf("FINISH\n"); return ; } s.pop(); for(int i=0;i<4;i++) { nex=now;//关键的代码处理点在这里,如果不在这里copy上一步的内容,我们这里边最终输出的zoufa【】【】是会丢东西的 nex.x=now.x+fx[i]; nex.y=now.y+fy[i]; if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&a[nex.x][nex.y]!='X') { if(a[nex.x][nex.y]=='.')//如果走的是路 { nex.output=now.output+1;//直接都+1就行 int num=nex.output; nex.zoufa[num][0]=nex.x; nex.zoufa[num][1]=nex.y; vis[nex.x][nex.y]=1; s.push(nex); } if(a[nex.x][nex.y]>='1'&&a[nex.x][nex.y]<='9')//如果遇到的是卫兵 { int xunhuan=a[nex.x][nex.y]-'0';//我们这里要先往上走一步 nex.output=now.output+1; int num=nex.output; nex.zoufa[num][0]=nex.x; nex.zoufa[num][1]=nex.y; for(int k=0;k<xunhuan;k++)//再和卫兵大战五百合 { nex.output+=1; int num=nex.output; nex.zoufa[num][0]=nex.x; nex.zoufa[num][1]=nex.y; vis[nex.x][nex.y]=1; } s.push(nex); } } } } printf("God please help our poor hero.\nFINISH\n");//如果没走到终点要对应输出语句。 }然后是完整的AC代码:
#include<stdio.h> #include<string.h> #include<queue> using namespace std; struct zuobiao { int x,y,output; friend bool operator <(zuobiao a,zuobiao b) { return a.output>b.output; } int zoufa[1000][2]; }now,nex; int vis[1000][1000]; char a[1000][1000]; int n,m; int fx[4]={0,0,1,-1}; int fy[4]={1,-1,0,0}; void bfs(int x,int y) { memset(vis,0,sizeof(vis)); priority_queue<zuobiao>s; now.x=x; now.y=y; now.output=0; now.zoufa[0][0]=x; now.zoufa[0][1]=y; vis[x][y]=1; s.push(now); while(!s.empty()) { now=s.top(); if(now.x==n-1&&now.y==m-1) { int bijiaox=0,bijiaoy=0; printf("It takes %d seconds to reach the target position, let me show you the way.\n",now.output); for(int i=0;i<now.output;i++) { if(bijiaox==now.zoufa[i+1][0]&&bijiaoy==now.zoufa[i+1][1]) { printf("%ds:FIGHT AT (%d,%d)\n",i+1,now.zoufa[i+1][0],now.zoufa[i][1]); bijiaox=now.zoufa[i+1][0]; bijiaoy=now.zoufa[i+1][1]; continue; } printf("%ds:(%d,%d)->(%d,%d)\n",i+1,now.zoufa[i][0],now.zoufa[i][1],now.zoufa[i+1][0],now.zoufa[i+1][1]); bijiaox=now.zoufa[i+1][0]; bijiaoy=now.zoufa[i+1][1]; } printf("FINISH\n"); return ; } s.pop(); for(int i=0;i<4;i++) { nex=now; nex.x=now.x+fx[i]; nex.y=now.y+fy[i]; if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&a[nex.x][nex.y]!='X') { if(a[nex.x][nex.y]=='.') { nex.output=now.output+1; int num=nex.output; nex.zoufa[num][0]=nex.x; nex.zoufa[num][1]=nex.y; vis[nex.x][nex.y]=1; s.push(nex); } if(a[nex.x][nex.y]>='1'&&a[nex.x][nex.y]<='9') { int xunhuan=a[nex.x][nex.y]-'0'; nex.output=now.output+1; int num=nex.output; nex.zoufa[num][0]=nex.x; nex.zoufa[num][1]=nex.y; for(int k=0;k<xunhuan;k++) { nex.output+=1; int num=nex.output; nex.zoufa[num][0]=nex.x; nex.zoufa[num][1]=nex.y; vis[nex.x][nex.y]=1; } s.push(nex); } } } } printf("God please help our poor hero.\nFINISH\n"); } int main() { while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++) { scanf("%s",a[i]); } bfs(0,0); } }