Sicily 1936. Knight Moves, sicilyknight

Source: Internet
Author: User

Sicily 1936. Knight Moves, sicilyknight

Ideas:

I didn't understand the question at the beginning... Orz... depends on what the gods understand.

The question is an 8x8 chess game. The server guard moves around in the form of a horse ("day"), specifies two points, and outputs the smallest step.

You can use breadth search to solve the problem.

The Code is as follows:

`1 # include <iostream> 2 # include <queue> 3 # include <cstring> 4 # include <string> 5 using namespace std; 6 7 int dx [] = {-1, -2,-2,-1, 1, 2, 2, 1}; // you can take eight directions: 8 int dy [] = {-2,-1, 1, 2, 2, 1,-1,-2}; 9 10 bool visited [100]; 11 12 int main () {13 int t; 14 cin> t; 15 while (t --) {16 memset (visited, false, sizeof (visited); 17 int distance [100] = {0}; 18 19 string node1, node2; 20 cin> node1> node2; 21 22 int X = (node1 [0]-'A') * 8 + node1 [1]-'1 '; 23 int Y = (node2 [0]-'A') * 8 + node2 [1]-'1'; 24 25 queue <int> store; 26 store. push (X); 27 while (! Store. empty () {28 if (store. front () = Y) 29 break; 30 31 int x = store. front ()/8; 32 int y = store. front () % 8; 33 34 for (int I = 0; I <8; I ++) {35 int nx = x + dx [I]; 36 int ny = y + dy [I]; 37 38 if (nx <0 | nx> 7 | ny <0 | ny> 7) 39 continue; 40 int temp = nx * 8 + ny; 41 42 if (! Visited [temp]) {43 store. push (temp); 44 visited [temp] = true; 45 distance [temp] = distance [store. front ()] + 1; 46} 47} 48 store. pop (); 49} 50 cout <"To get from" <node151 <"to" <node2 <"takes" 52 <distance [Y] <"knight moves. \ n "; 53} 54 55 return 0; 56} 57`

Related Keywords:
Related Article

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.