2 条题解
-
0huangguangrun LV 6 @ 2023-12-3 20:34:29
#include<bits/stdc++.h> using namespace std; struct st{int x,y,step;}f[90005]; const int dx[8]={-2,-1,1,2,2,1,-1,-2}; const int dy[8]={1,2,2,1,-1,-2,-2,-1}; bool mapp[305][305]; int sx,sy,ex,ey,n,ll; bool onboard(int x,int y){return ((x>=0)&&(x<ll)&&(y>=0)&&(y<ll)&&mapp[x][y]0);} bool isans(int x,int y){return (xex&&y==ey);} void bfs(){ int x,y,tail,head; memset(mapp,0,sizeof(mapp)); head=tail=1; f[head].x=sx; f[head].y=sy; f[head].step=0; mapp[sx][sy]=1; while (head<=tail){ for (int i=0;i<8;i++){ int tx=f[head].x+dx[i]; int ty=f[head].y+dy[i];
if (onboard(tx,ty)) { tail++;f[tail].x=tx;f[tail].y=ty; f[tail].step=f[head].step+1;mapp[f[tail].x][f[tail].y]=1; if(isans(f[tail].x,f[tail].y)){ printf("%d\n",f[tail].step); return; } } } head++; }
} int main(){ cin>>n; for (int i=0;i<n;i++){ cin>>ll; cin>>sx>>sy;cin>>ex>>ey; if (isans(sx,sy)){ cout<<0<<endl; continue; } bfs(); } }
-
02023-8-2 10:41:57@
#include<bits/stdc++.h> using namespace std; struct st{int x,y,step;}f[90005]; const int dx[8]={-2,-1,1,2,2,1,-1,-2}; const int dy[8]={1,2,2,1,-1,-2,-2,-1}; bool mapp[305][305]; int sx,sy,ex,ey,n,ll; bool onboard(int x,int y){return ((x>=0)&&(x<ll)&&(y>=0)&&(y<ll)&&mapp[x][y]==0);} bool isans(int x,int y){return (x==ex&&y==ey);} void bfs(){ int x,y,tail,head; memset(mapp,0,sizeof(mapp)); head=tail=1;f[head].x=sx;f[head].y=sy; f[head].step=0;mapp[sx][sy]=1; while (head<=tail){ for (int i=0;i<8;i++){ int tx=f[head].x+dx[i]; int ty=f[head].y+dy[i]; if (onboard(tx,ty)) { tail++;f[tail].x=tx;f[tail].y=ty; f[tail].step=f[head].step+1;mapp[f[tail].x][f[tail].y]=1; if(isans(f[tail].x,f[tail].y)){ printf("%d\n",f[tail].step); return; } } } head++; } } int main(){ cin>>n; for (int i=0;i<n;i++){ cin>>ll; cin>>sx>>sy;cin>>ex>>ey; if (isans(sx,sy)){ cout<<0<<endl; continue; } bfs(); } }
- 1
信息
- ID
- 371
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 7
- 标签
- 递交数
- 276
- 已通过
- 60
- 上传者