IAKIOI66666 @ 2024-08-21 10:51:59
不知道为啥,全RE,麻烦各路大佬帮忙看一下为什么。
ans记录目前到达目的地时最少的跳跃次数,num记录目前跳了几次
#include<iostream>
#include<queue>
using namespace std;
struct node{
int x,y;
int num;
};
queue<node> q;
int n,m,x1,y1,x2,y2,c,d,ans=1e9,a[100][100];
int main()
{
cin>>n>>m>>c>>d;
int x[8]={c,c,-1*c,-1*c,d,d,-1*d,-1*d};
int y[8]={-1*d,d,-1*d,d,c,-1*c,c,-1*c};
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]==3)x1=i,y1=j;
if(a[i][j]==4)x2=i,y2=j;
}
}
q.push({x1,y1,0});
while(!q.empty())
{
node qq=q.front();
q.pop();a[qq.x][qq.y]=2;
if(qq.x==x2&&qq.y==y2)
{
ans=min(ans,qq.num);continue;
}
else if(qq.num>=ans)
{
continue;
}
for(int i=0;i<=8;i++)
{
int xx=x[i]+qq.x,yy=y[i]+qq.y;
if(xx<1||yy<1||yy>m||xx>n||a[xx][yy]==0||a[xx][yy]==2)continue;
q.push({xx,yy,qq.num+1});
}
}
cout<<ans<<endl;
return 0;
}
by lovely_codecat @ 2024-08-21 11:01:29
注意到
for(int i=0;i<=8;i++)
应为:
for(int i=0;i<8;i++)
by lovely_codecat @ 2024-08-21 11:02:43
@IAKIOI66666
by IAKIOI66666 @ 2024-08-21 11:07:15
@lovely_codecat 谢谢大佬
by lovely_codecat @ 2024-08-21 11:08:21
@IAKIOI66666 求关