Harbin Polytechnic oj1181

Source: Internet
Author: User

 #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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.