社区讨论
求助,为什么输出一堆-1
P1443马的遍历参与者 4已保存回复 15
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 15 条
- 当前快照
- 1 份
- 快照标识符
- @locorrdj
- 此快照首次捕获于
- 2023/10/30 17:17 2 年前
- 此快照最后确认于
- 2023/11/05 04:11 2 年前
CPP
#include<bits/stdc++.h>
using namespace std;
int mp[505][505],p[505][505]={0};
int dx[8]={-2,-2,-1,-1,1,1,2,2};
int dy[8]={-1,1,-2,2,-2,2,-1,1};
int n,m,x,y;
struct node{
int nx,ny,step;
};
node t;
queue<node> q;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void bfs(){
while(!q.empty()){
t.nx=q.front().nx;
t.ny=q.front().ny;
t.step=q.front().step;
if(mp[t.nx][t.ny]>t.step&&t.nx>1&&t.ny>1&&t.nx<=n&&t.ny<=m&&!p[t.nx][t.ny]){
for(int i=0;i<8;i++){
t.nx=q.front().nx+dx[i];
t.ny=q.front().ny+dy[i];
t.step=q.front().step+1;
q.push(t);
}
}
p[t.nx][t.ny]=1;
q.pop();
}
}
int main(){
n=read(),m=read(),x=read(),y=read();
n++,m++,x++,y++;
memset(mp,0x7f7f7f7f,sizeof(mp));
t.nx=x,t.ny=y,t.step=0;
q.push(t);
bfs();
for(int i=2;i<=n;i++){
for(int j=2;j<=m;j++){
if(mp[i][j]==0x7f7f7f7f)printf("%-5d",-1);
else printf("%-5d",mp[i][j]);
}
putchar('\n');
}
}
RT /dk
回复
共 15 条回复,欢迎继续交流。
正在加载回复...