**Knight Remains - Amazon Top Interview Questions**

### Problem Statement :

You are given four integers n, x, y, and k. n represents an n by n chessboard and x, y represents a knight positioned at (x, y). The knight has to take exactly k steps, where at each step it chooses any of the 8 directions uniformly at random. Return the percentage chance rounded down to the nearest integer that the knight remains in the chessboard after taking k steps, with the condition that it can’t enter the board again once it leaves it. Constraints 1 ≤ n ≤ 25 0 ≤ k ≤ 100 Example 1 Input n = 8 x = 0 y = 0 k = 1 Output 25 Explanation This is an 8x8 chessboard and the initial position of the knight is (0, 0). It can take k = 1 step. After taking one step it will lie inside the board only at 2 out of 8 positions, and will lie outside at other positions. So, the probability is 2/8 = 0.25

### Solution :

` ````
Solution in C++ :
int x[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int y[8] = {1, 2, 2, 1, -1, -2, -2, -1};
double dp[26][26][101];
double s1(int n, int i, int j, int k) {
if (i < 0 || j < 0 || i >= n || j >= n) {
return 0;
}
if (k == 0) return 1;
if (dp[i][j][k] != 0) return dp[i][j][k];
double temp = 0;
for (int t = 0; t < 8; t++) {
temp += 0.125 * s1(n, i + x[t], j + y[t], k - 1);
}
return dp[i][j][k] = temp;
}
int solve(int n, int x, int y, int k) {
memset(dp, 0, sizeof(dp));
double k1 = s1(n, x, y, k);
k1 = k1 * 100;
// cout<<k1;
return (int)k1;
}
```

` ````
Solution in Python :
class Solution:
def solve(self, n, x, y, K):
def isvalid(i, j):
return 0 <= i < n and 0 <= j < n
movement = [[2, 1], [2, -1], [-2, 1], [-2, -1], [1, 2], [-1, 2], [1, -2], [-1, -2]]
@lru_cache(None)
def dp(x, y, k):
if not isvalid(x, y):
return 0
if k == 0:
return 1
res = 0
for mov in movement:
res += dp(x + mov[0], y + mov[1], k - 1)
return res
return dp(x, y, K) * 100 // (8 ** K)
```

## View More Similar Problems

## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →## Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →