Introduction to POJ 1915 Knight Moves bidirectional BFS

Source: Internet
Author: User
Tags integer numbers

Introduction to POJ 1915 Knight Moves bidirectional BFS

Description

Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.

Input

The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. each scenario consists of three lines containing integer numbers. the first line specifies the length l of a side of the chess board (4 <= l <= 300 ). the entire board has size l * l. the second and third line contain pair of integers {0 ,..., l-1} * {0 ,..., l-1} specifying the starting and ending position of the knight on the board. the integers are separated by a single blank. you can assume that the positions are valid positions on the chess board of that scenario.

Output

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. if starting point and ending point are equal, distance is zero. the distance must be written on a single line.

Sample Input

1

2

3

4

5

6

7

8

9

10

3

8

0 0

7 0

100

0 0

30 50

10

1 1

1 1

Sample Output

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

5

28

0

<Strong> question: give an n * N chessboard and give the minimum number of steps from the start point to the end point of the x y coordinate ball. </Strong>

Bidirectional BFS code:

<Pre class = "brush: java;" >#include <iostream>

# Include <cstdio>

# Include <queue>

# Include <cstring>

# Deprecision qq 330

Using namespace std;

Int vis1 [qq] [qq]; // number of steps that both mark the path and count

Int vis2 [qq] [qq];

Int fx1 [8] = {2,-2,-2, 1, 1,-1,-1 };

Int fx2 [8] = {1,-2,-2 };

Struct node {

Int x, y;

} Start, end; // start point at both ends of Bidirectional BFS

Int sx, sy, ex, ey;

Int m;

Bool inside (int xx, int yy) // you can determine whether an out-of-bounds occurs.

{

If (xx> = 0 & yy> = 0 & xx <m & yy <m) return = "" true; = "" else = "" false; = ""} = "" void = "" dbfs () = "" {= "" int = "" I, tq, tw; = "" queue <node = ""> q, w; // two queues

Start. x = sx; start. y = sy;

End. x = ex; end. y = ey;

Q. push (start );

W. push (end );

Vis1 [sx] [sy] = 0; // The number of subsequent steps starts from 0.

Vis2 [ex] [ey] = 0;

While (! Q. empty ()&&! W. empty ())

{

Node now, next;

Tq = q. size (); // to determine all the queues first

While (tq --)

{

Now = q. front ();

Q. pop ();

If (inside (now. x, now. y) & vis2 [now. x] [now. y]! =-1) // both ends start through this point ..

{

Printf ("% d \ n", vis1 [now. x] [now. y] + vis2 [now. x] [now. y]);

Return;

}

For (I = 0; I <8; I ++)

{

Next. x = now. x + fx1 [I];

Next. y = now. y + fx2 [I];

If (inside (next. x, next. y) & vis2 [next. x] [next. y]! =-1) // important, because the odd step is...

{

Printf ("% d \ n", vis1 [now. x] [now. y] + 1 + vis2 [next. x] [next. y]);

Return;

}

If (inside (next. x, next. y) & vis1 [next. x] [next. y] =-1)

{

Vis1 [next. x] [next. y] = vis1 [now. x] [now. y] + 1;

Q. push (next );

}

}

}

Tw = w. size ();

While (tw --) // same as above

{

Now = w. front ();

W. pop ();

If (inside (now. x, now. y) & vis1 [now. x] [now. y]! =-1)

{

Printf ("% d \ n", vis1 [now. x] [now. y] + vis2 [now. x] [now. y]);

Return;

}

For (I = 0; I <8; I ++)

{

Next. x = now. x + fx1 [I];

Next. y = now. y + fx2 [I];

If (inside (next. x, next. y) & vis1 [next. x] [next. y]! =-1)

{

Printf ("% d \ n", vis2 [now. x] [now. y] + 1 + vis1 [next. x] [next. y]);

Return;

}

If (inside (next. x, next. y) & vis2 [next. x] [next. y] =-1)

{

Vis2 [next. x] [next. y] = vis2 [now. x] [now. y] + 1;

W. push (next );

}

}

}

}

}

Int main ()

{

Int t;

Scanf ("% d", & t );

While (t --)

{

Scanf ("% d", & m );

Scanf ("% d", & sx, & sy, & ex, & ey );

Memset (vis1,-1, sizeof (vis1); // mark as not passed

Memset (vis2,-1, sizeof (vis2 ));

Dbfs ();

}

Return 0;

}

</M & yy <m)> </cstring> </queue> </cstdio> </iostream> </pre> <br> the essence of Bidirectional BFS is to start searching from the start point and end point at the same time. find the shortest path .. <Br>

 


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.