社区讨论
为什么只是一行代码的问题就会导致90/100pts?
P1443马的遍历参与者 2已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @lo1qnlqx
- 此快照首次捕获于
- 2023/10/23 01:24 2 年前
- 此快照最后确认于
- 2023/11/03 02:03 2 年前
CPP
#include<bits/stdc++.h>
using namespace std;
int n,m,X,Y,tmp;
const int dx[8]={-2,-1,1,2,2,1,-1,-2},
dy[8]={-1,-2,-2,-1,1,2,2,1};
int book[500][500],ans[500][500];
struct node{
int x,y,step;
}s[500*500+10];
void bfs(){
int head=1,tail=2;
while(head<tail){
int nx=s[head].x,ny=s[head].y,ss=s[head].step;
head++;
for(int d=0;d<=7;d++){
int tx=nx+dx[d],ty=ny+dy[d];
if(tx<1 || tx>n || ty<1 || ty>m){
continue;
}
if(book[tx][ty]==0){
s[tail].x=tx;
s[tail].y=ty;
s[tail].step=ss+1;
ans[tx][ty]=ss+1;
book[tx][ty]=1;
tail++;
}
}
}
}
int main(){
scanf("%d%d%d%d",&n,&m,&X,&Y);
memset(ans,-1,sizeof(ans));
s[1].x=X,s[1].y=Y,s[1].step=0;
book[X][Y]=1;
ans[X][Y]=0;
bfs();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
//if(i!=X && j!=Y && ans[i][j]==0)ans[i][j]=-1;
printf("%5d ",ans[i][j]);
}puts("");
}
return 0;
}
看注释
回复
共 5 条回复,欢迎继续交流。
正在加载回复...