Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
19 views
5 pages
Lab Assignment Solution
Solution
Uploaded by
nahid_1104063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Lab Assignment Solution For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
19 views
5 pages
Lab Assignment Solution
Solution
Uploaded by
nahid_1104063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Lab Assignment Solution For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save Lab Assignment Solution For Later
You are on page 1
/ 5
Search
Fullscreen
tora, 987 AM Chet Kh Probl Fn the shore path om sou odstnaon— Tah Del TECHIE DELIGHT > Q FAANG Interview Prep Practice "°" Data Structures and Algorithms ¥ Chess Knight Problem | Find the shortest path from source to destination Given a chessboard, find the shortest distance (minimum number of steps) taken by a knight to reach a given destination from a given source. For example, Input: N= 8 (8 x 8 board) Source = (7, @) Destination = (8, 7) Output: Minimum number of steps required is 6 The knight's movement is illustrated in the following figure’ hitpsi ww techiedelight. com/chess-knight-problem-find-shortest-path-source-destination! 1612119921, 9:57 AM ‘Chess Knight Problem | Find the shortest path fom source to destination — Techie Deligh Practice this problem ‘The idea is to use Breadth-first search (BFS) as it is the shortest path problem. Following is the complete algorithm 1. Create an empty queue and enqueue the source cell having a distance of 0 from the source (itself. 2. Loop till queue is empty: | Dequeue next unvisited node. IL If the popped node is the destination node, return its distance. Ill Otherwise, we mark the current node as visited. For each of eight possible movements for a knight, enqueue each valid movement with +1 distance (minimum distance of a given node from the source is one more than the minimum distance of parent from source) A knight can move in eight possible directions from a given cell as illustrated in the following figure: We can find all the possible locations the knight can move to from the given location by using the array that stores the relative position of knight movement from any location. For example, if the kee? current location is (x, y) ,we can move to (x + row[k], y + col[k]) for using the following array: row] = [ 2, 2, -2, -2, 1, 1, -1, -1] colf] = [ -1, 4, 1, -1, 2, -2, 2, -2] hitpsi ww tachiedelight. com/chess-knight-problem-find-shortest-path-source-destination! 21812119921, 9:57 AM ‘chess Knight Problem | Find the shortest path rom source to destination — Techie Delight So, from position (x, y) knight's can move to’ (+2 y-4) (+2 y+) (-2 y+) Qe - 2, y - 1) (x +1, y + 2) (+4, y- 2) (4, y +2) (x- 4, y - 2) Note that in BFS, all cells having the shortest path as 1 are visited first, followed by their adjacent cells having the shortest path as 1 + 1 = 2 and so on... so if we reach any node in BFS, its shortest path = shortest path of parent + 1. So, the destination cell's first occurrence gives us the result, and we can stop our search there. The shortest path cannot exist from some other cell for which we haven't reached the given node yet. If any such path were possible, we would have already explored i The algorithm can be implemented as follows in C+ +, Java, and Python: CH ~ Java v Python 1 | import sys 2 | fron collections import deque 3 4 5 | # A queue node used in BFS 6 | class Node: 7 # (x, y) represents chessboard coordinates 8 # ‘dist’ represents its minimum distance from the source 9 def _init_(self, x, y, dist-@): 10 self.x = x rte self.y =y 2 self.dist = dist 3B 14 # As we are using “Node” as a key in a dictionary, a5 # we need to override the “_hash_()° and *_eq_()° function 16 def _hash_(self): v7 return hash((self.x, self.y, self.dist)) 18 19 def _eq_(self, other): 20 return (self.x, self.y, self.dist) == (other.x, other.y, other.dist) 2 2 23 | # Below lists detail all eight possible movements for a knight 24 [2, 2, -2, -2, 1, 4, -1, -1] 25 » 4,1, -1, 2, -2, 2, -2] 26 27 28 | # Check if (x, y) is valid chessboard coordinates. 29 | # Note that a knight cannot go out of the chessboard tps ww techiedelight. comichess-knight-problemsfind-shortest-path-source-destinaion! as12119921, 9:57 AM ‘chess Knight Problem | Find the shortest path rom source to destination — Techie Delight 30 | def isvalid(x, y, N): 31 return not (x < @ or y < @ or x >= Nor y >= N) 32 33 34 | # Find the minimum number of steps taken by the knight 35 | # from the source to reach the destination using BFS 36 | def findShortestDistance(src, dest, N): 37 38 # set to check if the matrix cell is visited before or not 39 visited = set() 40 a # create a queue and enqueue the first node a2 q = deque() 43 q-append(sre) 44 45 # loop till queue is empty 46 while q: 47 48 # dequeue front node and process it 49 node = q.popleft() 50 51 x = node.x 52 y = node.y 53 Gist = node.dist 54 55 # if the destination is reached, return distance 56 if x == dest.x and y == dest.y: 57 return dist 58 59 # skip if the location is visited before 60 if node not in visited: 61 # mark the current node as visited 62 visited. add(node) 63 64 # check for all eight possible movements for a knight 65 # and enqueue each valid movement 66 for i in range(1en(row)): 67 # get the knight's valid position from the current position ¢ 68 # the chessboard and enqueue it with +1 distance 69 x1 = x + row[i] 76 yl = y + colli] n 2 if isvalid(xt, yt, B @.append(Node(x1, y1, dist + 1)) 74 B # return infinity if the path is not possible 76 return sys.maxsize 7 8 79 if _name_ == '_main_': 80 81 N=8 #N XN matrix 82 src = Node(@, 7) # source coordinates 83 dest = Node(7, @) # destination coordinates 84 85 print ("The minimum number of steps required is*, 86 ‘findShortestDistance(sre, dest, N)) 87 Download Run Code output: The minimum number of steps required is 6 The time complexity of the proposed solution is O(M * N) and requires O(M x N) extra space, where M and N are dimensions of the matrix. hips: techiedstight con/chess-Anightprlem-ind-shortest-pa-soure- destination! 4s12119921, 9:57 AM ‘chess Knight Problem | Find the shortest path rom source to destination — Techie Delight Exercise: Extend the solution to print the paths as well & Graph, Matrix, Queue # Algorithm, Breadth-first search, FIFO, Hard Privacy Policy | Send feedback Techie Delight © 2021 All Rights Reserved. hitpsi ww techiedeligh. comchess-knight-problen-find-shortest-path-source-destnaion! 5s
You might also like
Knight's Travails CST 8203 - Advanced Programming and Data Structure Final Project
PDF
100% (1)
Knight's Travails CST 8203 - Advanced Programming and Data Structure Final Project
3 pages
CSE316 Lab Assignment Fall 2021
PDF
No ratings yet
CSE316 Lab Assignment Fall 2021
3 pages
Knight On Chessboard
PDF
No ratings yet
Knight On Chessboard
7 pages
DC Report B3 Group B1 Batch
PDF
No ratings yet
DC Report B3 Group B1 Batch
7 pages
Red Knights Shortest Path English PDF
PDF
No ratings yet
Red Knights Shortest Path English PDF
2 pages
Knight Moves: Input
PDF
No ratings yet
Knight Moves: Input
1 page
SamratBFS DFS - Word
PDF
No ratings yet
SamratBFS DFS - Word
7 pages
A Chess Knight: Input
PDF
No ratings yet
A Chess Knight: Input
3 pages
DC Miniproject
PDF
No ratings yet
DC Miniproject
7 pages
BFS DFS
PDF
No ratings yet
BFS DFS
8 pages
Ai Manuval
PDF
No ratings yet
Ai Manuval
49 pages
Multistage Backward
PDF
No ratings yet
Multistage Backward
13 pages
3 8 (Amar)
PDF
No ratings yet
3 8 (Amar)
23 pages
AI - Lab Manual - Removed
PDF
No ratings yet
AI - Lab Manual - Removed
28 pages
BT-Knight Tour PDF
PDF
No ratings yet
BT-Knight Tour PDF
8 pages
Ai Outputs
PDF
No ratings yet
Ai Outputs
12 pages
AI Assiggnment Afrar
PDF
No ratings yet
AI Assiggnment Afrar
9 pages
DAA Mini Project Report
PDF
No ratings yet
DAA Mini Project Report
20 pages
62837lecture 04 (B) Dijkstras Algorithm
PDF
No ratings yet
62837lecture 04 (B) Dijkstras Algorithm
7 pages
Eloise's Claim: by Showing That Franklin Is One of Eloise's Ancestors or by Showing That Eloise Is One of Franklin's
PDF
No ratings yet
Eloise's Claim: by Showing That Franklin Is One of Eloise's Ancestors or by Showing That Eloise Is One of Franklin's
7 pages
Pranav Dutta
PDF
No ratings yet
Pranav Dutta
6 pages
Black and Purple Gradient Modern Artificial Intelligence Presentation
PDF
No ratings yet
Black and Purple Gradient Modern Artificial Intelligence Presentation
21 pages
N Queen Problen & TSP PDF
PDF
No ratings yet
N Queen Problen & TSP PDF
6 pages
Algorithm 1: Input:: Step Equals N M
PDF
No ratings yet
Algorithm 1: Input:: Step Equals N M
1 page
AI Practicals
PDF
No ratings yet
AI Practicals
33 pages
CSA (2001) : Fundamental in AIML, Module 2
PDF
No ratings yet
CSA (2001) : Fundamental in AIML, Module 2
67 pages
Certificate: MSG-SGKM College Arts, Science and Commerce
PDF
No ratings yet
Certificate: MSG-SGKM College Arts, Science and Commerce
34 pages
Path Planning
PDF
No ratings yet
Path Planning
92 pages
A Algorithm
PDF
No ratings yet
A Algorithm
3 pages
Implementation of BFS For Tic-Tac-Toe Problem
PDF
No ratings yet
Implementation of BFS For Tic-Tac-Toe Problem
2 pages
Practical No. 01: Write The Program The Following
PDF
No ratings yet
Practical No. 01: Write The Program The Following
31 pages
Lab 9
PDF
No ratings yet
Lab 9
3 pages
8 Puzzel
PDF
No ratings yet
8 Puzzel
3 pages
Programs
PDF
No ratings yet
Programs
6 pages
AIMLLab 2
PDF
No ratings yet
AIMLLab 2
4 pages
Chapter 4 Backtracking Student
PDF
No ratings yet
Chapter 4 Backtracking Student
10 pages
ML Exeperiment7
PDF
No ratings yet
ML Exeperiment7
8 pages
Backtracking PDF
PDF
No ratings yet
Backtracking PDF
54 pages
Shortestpath
PDF
No ratings yet
Shortestpath
11 pages
AI Lets Go
PDF
No ratings yet
AI Lets Go
28 pages
Ai 4
PDF
No ratings yet
Ai 4
8 pages
Artificial Intelligence.r
PDF
No ratings yet
Artificial Intelligence.r
58 pages
Lecture 12 - Graphs P2 PDF
PDF
No ratings yet
Lecture 12 - Graphs P2 PDF
59 pages
21BCB7005 AI Experiment-5
PDF
No ratings yet
21BCB7005 AI Experiment-5
2 pages
Group 20 Lab 4 5
PDF
No ratings yet
Group 20 Lab 4 5
22 pages
AD3311-Artifical Intelligence Laboratory Biew
PDF
No ratings yet
AD3311-Artifical Intelligence Laboratory Biew
63 pages
P03 A Star Algorithm 35 Anushka Shetty
PDF
No ratings yet
P03 A Star Algorithm 35 Anushka Shetty
23 pages
Solving Knight S Tour Problem Using Fire
PDF
No ratings yet
Solving Knight S Tour Problem Using Fire
4 pages
AI Complete Print
PDF
No ratings yet
AI Complete Print
18 pages
AI Labfile
PDF
No ratings yet
AI Labfile
36 pages
11
PDF
No ratings yet
11
2 pages
Ai Fie
PDF
No ratings yet
Ai Fie
15 pages
8 Puzzle Problem A Star
PDF
No ratings yet
8 Puzzle Problem A Star
3 pages
56d0f5 - Informed Search Algorithms
PDF
No ratings yet
56d0f5 - Informed Search Algorithms
16 pages
AI-G Lab
PDF
No ratings yet
AI-G Lab
37 pages
AAI Journal
PDF
No ratings yet
AAI Journal
17 pages