Knight moves hdu1372 BFS

Source: Internet
Author: User

/*
I didn't understand the question at the beginning. It turned out to be a question about horse jumping in chess. Didn't you like playing chess? But why... Tangle
This is my first self-written BFs. After debugging, I finally changed my low-level problem (marked)
*/
# Include <iostream> // 2374758 2010-04-23 14:41:15 accepted 1372 46 Ms 292 K 1189 B C ++ regret
# Include <queue>
# Include <cstdio>
Using namespace STD;

Int map [10] [10];
Bool hash [10] [10];
Int Si, SJ;
Int EI, EJ;
Char str1 [5];
Char str2 [5];
Int dir [8] [2] = {-1,-2}, {-2,-1}, {-2, 1}, {-1, 2 }, {1, 2}, {2, 1}, {2,-1}, {1,-2 }};
Struct Node
{
Int X;
Int y;
Int T;
};
Int BFS (int I, Int J)
{
Node P, N;
Int K;
N. x = I; N. Y = J; N. T = 0;
Queue <node> q;
Q. Push (N );
Hash [I] [J] = false;
While (! Q. Empty ())
{
N = Q. Front ();
Q. Pop ();
If (N. x = EI & N. Y = EJ)
{
Cout <"to get from" <str1 <"to" <str2 <"takes" <N. T <"knight moves." <Endl;
// Cout <N. T <Endl;
Break;
}
For (k = 0; k <8; k ++)
{
Int Tx = n. x + dir [k] [0];
Int ty = n. Y + dir [k] [1];
If (TX> = 1 & TX <= 8 & ty> = 1 & ty <= 8 & hash [TX] [ty]) // mark errors at the beginning and write them accidentally! Hash [TX] [ty]
{
P. x = TX;
P. Y = ty;
P. t = n. t + 1;
Q. Push (P );
Hash [p. x] [P. Y] = false;

}

}
}
Return 1;
 
}
Int main ()
{
While (scanf ("% S % s", str1, str2 )! = EOF)
{
SI = str1 [1]-'0 ';
SJ = str1 [0]-96; // @ at the beginning, enter SJ = str1 [0]-'0'-96. After debugging, the SJ value turns to a negative number.
Ei = str2 [1]-'0 ';
EJ = str2 [0]-96;

Memset (hash, true, sizeof (hash ));
BFS (Si, SJ );
// Cout <Endl;
}
}

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.