Use C++ and please provide a code that works properly   input.txt e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Use C++ and please provide a code that works properly

 

input.txt

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Given a chess board, your job is to write a program that takes two squares x and y as input and
then determines the number of knight moves on a shortest route from x to y.
a b c def gh
8
8
7
7
5
4
4
2
1
a b cdef gh
Input Specification
Your program should read from an input file, which will contain one or more test cases. Each test
case consists of one line containing two squares separated by one space. A square is a string
consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the
chessboard.
Sample Input
e2 e4
al b2
b2 c3
al h8
al h7
h8 al
b1 c3
f6 f6
Output Specification
For each test case, print one line saying To get from xx to yy takes ?? knight moves. in
console.
Sample Output
Below is the correct output for the previous sample input.
To get from e2 to e4 takes 2 knight moves.
To get from al to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from al to h8 takes 6 knight moves.
To get from al to h7 takes 5 knight moves.
To get from h8 to al takes 6 knight moves.
To get from bl to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
Hint
• According to the description, the grid is 8 by 8, therefore there are 64 squares. We can then
model the chessboard as a graph with 64 vertices; and an edge between two vertices exists if
and only if we can make a knight move from one vertex to another.
• When moving to the next position, a knight can have at most eight possible directions, as
shown in the above figure for the black knight, without getting out of the chess board
boundary.
• The problem becomes a graph traversal problem!
• DFS or BFS? Which one will work?
Transcribed Image Text:Given a chess board, your job is to write a program that takes two squares x and y as input and then determines the number of knight moves on a shortest route from x to y. a b c def gh 8 8 7 7 5 4 4 2 1 a b cdef gh Input Specification Your program should read from an input file, which will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard. Sample Input e2 e4 al b2 b2 c3 al h8 al h7 h8 al b1 c3 f6 f6 Output Specification For each test case, print one line saying To get from xx to yy takes ?? knight moves. in console. Sample Output Below is the correct output for the previous sample input. To get from e2 to e4 takes 2 knight moves. To get from al to b2 takes 4 knight moves. To get from b2 to c3 takes 2 knight moves. To get from al to h8 takes 6 knight moves. To get from al to h7 takes 5 knight moves. To get from h8 to al takes 6 knight moves. To get from bl to c3 takes 1 knight moves. To get from f6 to f6 takes 0 knight moves. Hint • According to the description, the grid is 8 by 8, therefore there are 64 squares. We can then model the chessboard as a graph with 64 vertices; and an edge between two vertices exists if and only if we can make a knight move from one vertex to another. • When moving to the next position, a knight can have at most eight possible directions, as shown in the above figure for the black knight, without getting out of the chess board boundary. • The problem becomes a graph traversal problem! • DFS or BFS? Which one will work?
Expert Solution
steps

Step by step

Solved in 5 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY