Poj 2243 zoj 1091 uvaoj 439 knight moves (BFS breadth-first search)

Source: Internet
Author: User

// Typical BFS (breadth-first search) <br/> // the minimum number of steps required for the Trojan to take the day <br/> # include <iostream> <br/> using namespace STD; <br/> int map [8] [8]; // array of record steps <br/> int vis [8] [8]; // array of accessed records <br/> char a [3], B [3]; <br/> int X1, Y1, X2, Y2; <br/> void set (INT X1, int Y1) <br/> {<br/> // cut out unreasonable range <br/> If (X1 + 1 <= 7 & Y1 + 2 <= 7 &&! Vis [X1 + 1] [Y1 + 2]) <br/> map [X1 + 1] [Y1 + 2] = map [X1] [Y1] + 1; <br/> If (X1 + 1 <= 7 & y1-2> = 0 &&! Vis [X1 + 1] [y1-2]) <br/> map [X1 + 1] [y1-2] = map [X1] [Y1] + 1; <br/> If (x1-1> = 0 & Y1 + 2 <= 7 &&! Vis [x1-1] [Y1 + 2]) <br/> map [x1-1] [Y1 + 2] = map [X1] [Y1] + 1; <br/> If (x1-1> = 0 & y1-2> = 0 &&! Vis [x1-1] [y1-2]) <br/> map [x1-1] [y1-2] = map [X1] [Y1] + 1; <br/> If (X1 + 2 <= 7 & Y1 + 1 <= 7 &&! Vis [X1 + 2] [Y1 + 1]) <br/> map [X1 + 2] [Y1 + 1] = map [X1] [Y1] + 1; <br/> If (x1-2> = 0 & Y1 + 1 <= 7 &&! Vis [x1-2] [Y1 + 1]) <br/> map [x1-2] [Y1 + 1] = map [X1] [Y1] + 1; <br/> If (X1 + 2 <= 7 & y1-1> = 0 &&! Vis [X1 + 2] [y1-1]) <br/> map [X1 + 2] [y1-1] = map [X1] [Y1] + 1; <br/> If (x1-2> = 0 & y1-1> = 0 &&! Vis [x1-2] [y1-1]) <br/> map [x1-2] [y1-1] = map [X1] [Y1] + 1; <br/>}< br/> void search (INT X1, int Y1, int X2, int Y2) <br/>{< br/> int step = 1; <br/> vis [X1] [Y1] = 1; <br/> set (x1, Y1 ); <br/> // breadth-first search process <br/> while (Map [X2] [y2] = 0) <br/> {<br/> for (INT I = 0; I <8; ++ I) <br/> for (Int J = 0; j <8; + + J) <br/> If (Map [I] [J] = step) <br/> {<br/> vis [I] [J] = 1; <br/> set (I, j); <br/>}< br/> + + step; <br/>}< br/> int main () <br/>{< br/> while (CIN> A> B) <br/> {<br/> memset (MAP, 0, sizeof (MAP); // initialization <br/> memset (VIS, 0, sizeof (VIS )); // initialization <br/> X1 = A [0]-'A'; <br/> Y1 = A [1]-'1 '; <br/> X2 = B [0]-'A'; <br/> Y2 = B [1]-'1'; <br/> If (strcmp (, b) = 0) <br/> cout <"to get from" <A <"to" <B <"takes 0 knight moves. "<Endl; <br/> else <br/> {<br/> Search (x1, Y1, X2, Y2 ); <br/> cout <"to get from" <A <"to" <B <"takes" <map [X2] [y2] <" knight moves. "<Endl; <br/>}< br/> return 0; <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.