Samsgc @ 2024-08-08 23:41:36
下面是代码部分
#include<bits/stdc++.h>
/*#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>*/
using namespace std;
int n,m,tx,ty;
int ans[309][309];
int move[10][5]={{1,2},{2,1},{1,-2},{2,-1},{-1,-2},{-2,-1},{-1,2},{-2,1}};
struct horse
{
int x,y;
};
queue<horse> Q;
int main()
{
cin>>n>>m>>tx>>ty;
memset(ans,-1,sizeof(ans));
horse tmp={tx,ty};
ans[tx][ty]=0;
Q.push(tmp);
while(!Q.empty())
{
horse nnow=Q.front();
Q.pop();
for(int i=0;i<8;i++)
{
int nx=nnow.x+move[i][0],ny=nnow.y+move[i][1];
if(nx<1||nx>n||ny<1||ny>m||ans[nx][ny]!=-1)continue;
int d=ans[nnow.x][nnow.y];
ans[nx][ny]=d+1;
horse tmp={nx,ny};
Q.push(tmp);
}
}
for(int i=1;i<=n;i++,puts(""))
for(int j=1;j<=m;j++)
printf("%-5d",ans[i][j]);
return 0;
}
by ___LuXun___ @ 2024-08-09 00:12:04
#include<bits/stdc++.h>
using namespace std;
const int N=410;
int n,m;
struct str{
int x,y;
}q[N*N];
int head=0,tail=1;
int v[N][N];
int dx[8]={1,1,2,2,-1,-1,-2,-2};
int dy[8]={2,-2,1,-1,-2,2,1,-1};
int sx,sy;
void bfs(){
v[sx][sy]=0;
q[1].x=sx;
q[1].y=sy;
while(head<tail){
int xx=q[++head].x;
int yy=q[head].y;
int t=v[xx][yy];
for(int i=0;i<8;i++){
int nx=xx+dx[i];
int ny=yy+dy[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&v[nx][ny]==-1){
v[nx][ny]=t+1;
q[++tail].x=nx;
q[tail].y=ny;
}
}
}
}
int main(){
cin>>n>>m>>sx>>sy;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
v[i][j]=-1;
}
}
bfs();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
printf("%-5d",v[i][j]);
}
cout<<endl;
}
return 0;
}
@Samsgc 抄吧
by Samsgc @ 2024-08-09 00:14:42
@LuXun 我有AC的码,我就是好奇这一遍为什么在洛谷上编译失败,但是在本地可以编译
by Samsgc @ 2024-08-09 00:15:54
@LuXun 能帮忙看一下吗
by Lisuyang @ 2024-08-09 00:55:27
把 move 数组的名字改一下应该就可以了
by Lisuyang @ 2024-08-09 00:57:11
万能头里面有 move 了(似乎在move.h里面)
by Lisuyang @ 2024-08-09 01:01:10
名字冲突了
by Samsgc @ 2024-08-09 13:33:04
@Lisuyang 谢谢,过了