0% found this document useful (0 votes)
34 views3 pages

BACKTRACKING Questions

rdshtfrjkfvbbdhb

Uploaded by

v28424976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

BACKTRACKING Questions

rdshtfrjkfvbbdhb

Uploaded by

v28424976
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Backtracking

(Assignment Questions)

Note - These are classical & important questions. Please positively solve
them.

Question 1 :
Rat in a Maze
You are given a starting position for a rat which is stuck in a maze at an initial point (0, 0) (the
maze can be thought of as a 2-dimensional plane). The maze would be given in the form of a
square matrix of order N * N where the cells with value 0 represent the maze’s blocked
locations while value 1 is the open/available path that the rat can take to reach its destination.
The rat's destination is at (N - 1, N - 1).
Your task is to find all the possible paths that the rat can take to reach from source to
destination in the maze.
The possible directions that it can take to move in the maze are 'U'(up) i.e. (x, y - 1) , 'D'(down)
i.e. (x, y + 1) , 'L' (left) i.e. (x - 1, y), 'R' (right) i.e. (x + 1, y).

(This problem is similar to Grid ways.)

Sample Input : int maze[ ][ ] = { { 1, 0, 0, 0 },


{ 1, 1, 0, 1 },
{ 1, 1, 0, 0 },
{ 0, 1, 1, 1 } };

Sample Output : DDRDRR


DRDDRR

Hint : To track which cell has or not been visited, create a NxN vector called visited.
This vector will be initialized with false values for all cells & make the value for a particular cell
to true when you have visited it.

Question 2 :
Keypad Combinations
Given a string containing digits from 2-9 inclusive, print all possible letter combinations that
the number could represent. You can print the answer in any order.

A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1
does not map to any letters.

Sample Input 1 : digits = "23"


Sample Output 1 : "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
Sample Input 2 : digits = "2"
Sample Output 2 : "a", "b", "c"

Sample Input 3 : digits = ""


Sample Output 3 : ””

Question 3 :
Knight’s Tour
Given a N*N board with the Knight placed on the first block of an empty board. Moving
according to the rules of chess, knights must visit each square exactly once. Print the order
of each cell in which they are visited.

Sample Input 1 : N = 8
Sample Output 1 :
0 59 38 33 30 17 8 63
37 34 31 60 9 62 29 16
58 1 36 39 32 27 18 7
35 48 41 26 61 10 15 28
42 57 2 49 40 23 6 19
47 50 45 54 25 20 11 14
56 43 52 3 22 13 24 5
51 46 55 44 53 4 21 12

(Hint : Similar to N Queens)

https://t.me/+nEKeBr_yhXtmY2Yx
https://telegram.me/+nEKeBr_yhXtmY2Yx

You might also like