社区讨论
全RE,求大佬awa
P1443马的遍历参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mhj07d9q
- 此快照首次捕获于
- 2025/11/03 18:35 4 个月前
- 此快照最后确认于
- 2025/11/03 18:35 4 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
int n,m,xx,yy,a[402][402];
const int x[8]={-1,-2,-2,-1,1,2,2,1};
const int y[8]={-2,-1,1,2,2,1,-1,-2};
struct _horse
{
int sx,sy,step;
}q,p;
queue<_horse> horse;
int bfs()
{
while(!horse.empty())
{
p=horse.front();
horse.pop();
for(int i=0;i<=7;i++)
{
if(p.sx+x[i]>0&&p.sx+x[i]<=n&&p.sy+y[i]>0&&p.sy+y[i]<=m)
{
if(a[p.sx+x[i]][p.sy+y[i]]==-1)
{
q.sx=p.sx+x[i];
q.sy=p.sy+y[i];
q.step=p.step+1;
a[p.sx+x[i]][p.sy+y[i]]=q.step;
horse.push(q);
}
}
}
}
}
int main()
{
scanf("%d %d %d %d",&n,&m,&p.sx,&p.sy);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
a[i][j]=-1;
q.sx=p.sx,q.sy=p.sy,q.step=0;
a[q.sx][q.sy]=0;
horse.push(q);
bfs();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...