最短步数

gwh2022 2022-10-14 20:00:39 29 返回题目

#include<bits/stdc++.h> using namespace std; struct Site{ int r,c,s; }; queueq; int qx[12]={-2,-2,-1,1,2,2,2,2,1,-1,-2,-2}; int qy[12]={-1,-2,-2,-2,-2,-1,1,2,2,2,2,1}; int main(){ int m[101][101],x1,y1,x2,y2; Site s,news; memset(m,0xff,sizeof(m)); s.r=1,s.c=1,s.s=0; q.push(s); cin>>x1>>y1>>x2>>y2; while(!q.empty()){ s=q.front(); q.pop(); for(int d=0;d<=11;d++){ int x,y,st; x=s.r+qx[d]; y=s.c+qy[d]; if(x>0&&y>0&&x<=100&&y<=100) if(m[x][y]==-1){ m[x][y]=s.s+1; news.r=x; news.c=y; news.s=m[x][y]; q.push(news); if(m[x1][y1]>0&&m[x2][y2]>0){ cout<<m[x1][y1]<<endl; cout<<m[x2][y2]<<endl; return 0; } } } } }

{{ vote && vote.total.up }}