T1330 minimum number of steps (#ⅱ-8) (Breadth First search)

Source: Internet
Author: User

"Title description"

In all kinds of chess, chess pieces of the way is always certain, such as Chinese chess horse walking "Day". There is a pupil thought that if the horse can have two ways to increase its interest, therefore, he stipulated that the horse can go according to "Day", but also can elephant the same "Tian" word. His deskmate usually like to go, know this is very interesting after the event, want to try, in a (100x100) of the board selected two points A, B,a point put on the sunspot, B Point put on white son, on behalf of two horses. Chess pieces can follow the word "Day", you can also press the word "Tian" walk, two people a dark horse, a walk white horse. Who wins with a minimum number of steps to the upper-left corner of the coordinates (at the top) of the point. Now he asked you to help, give you a, b two coordinates, want to know the two positions to (the) point the minimum possible number of steps.

Input

Coordinates of A and b two points.

Output

Minimum number of steps.

"Input Sample" 12 1618 10 "Output sample" 89
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<string>#include<cstdlib>#include<queue>#include<vector>#defineINF 0x3f3f3f3f#definePI ACOs (-1.0)#defineN 101#defineMOD 2520#defineE 1e-12using namespacestd;intA[n][n];BOOLVis[n][n];intdir[][2]={{-2,1},{-2,-1},{-2,2},{-2,-2},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1},{2,-2},{2,2}};structnode{intx; inty; intStep;} Q[n* -];voidBFsintX0,inty0) {    intHead=1, tail=1; memset (Vis,0,sizeof(VIS)); q[tail].x=x0; Q[tail].y=y0; Q[tail].step=0; Tail++; VIS[X0][Y0]=1;  while(head<tail) {        intx=q[head].x; inty=q[head].y; intstep=Q[head].step; if(x==1&&y==1) {printf ("%d\n", step);  Break; }         for(intI=0;i< A; i++)        {            intnx=x+dir[i][0]; intny=y+dir[i][1]; if(nx>=1&&nx<= -&&ny>=1&&ny<= -&&vis[nx][ny]==0) {Vis[nx][ny]=1; q[tail].x=NX; Q[tail].y=NY; Q[tail].step=step+1; Tail++; }} head++; }}intMain () {intXa,ya,xb,yb; scanf ("%d%d%d%d",&xa,&ya,&xb,&YB);    BFS (Xa,ya);    BFS (XB,YB); return 0;}

T1330 minimum number of steps (#ⅱ-8) (Breadth First search)

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.