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
Reverse a doubly linked list
This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.
View Solution →Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →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 →