#include <iostream> #include <string.h> using namespace std;struct node{int x;//point coordinates int y; int s;//Record the number of steps};int main () {int startx,starty; int Endx,endy; int tx,ty; int n,m; int k; Node que[300000]; int book[500][500]; int head,tail; int next[8][2]={{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1}}; Direction while (Cin>>startx>>starty>>endx>>endy) {if (Startx==endx&&starty==endy) {cout<< "0" <<endl; } else {int flag=0; memset (book,0,sizeof (book));//The initial all is not marked head=1; tail=1; Starting and ending points into the queue que[tail].x=startx; Que[tail].y=starty; que[tail].s=0; Book[startx][starty]=1; tail++; BFS while (Head<tail) {for (k=0;k<8;k++)//traverse 8 Directions { TX=QUE[HEAD].X+NEXT[K][0]; TY=QUE[HEAD].Y+NEXT[K][1]; is out of range if (tx<0| | tx>300| | ty<0| | ty>300) {continue; }//has not been accessed if (book[tx][ty]==0) {book[tx][t Y]=1; into the queue que[tail].x=tx; Que[tail].y=ty; que[tail].s=que[head].s+1; tail++; if (Tx==endx&&ty==endy)//arrives at the end of the access point {flag=1; Break }}} if (flag==1) {break;//end loop } head++; } cout<<que[tail-1].s<<endl; } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Harbin Polytechnic oj1181