Skip to content

Commit 695e22a

Browse files
committed
knight probablity on chaessboard if can not come back on chessboard
1 parent d24d70e commit 695e22a

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This problem was asked by **Two Sigma**.
2+
3+
A knight is placed on a given square on an 8 x 8 chessboard. It is then moved randomly several times, where each move is a standard [knight move](<https://en.wikipedia.org/wiki/Knight_(chess)#Movement>). If the knight jumps off the board at any point, however, it is not allowed to jump back on.
4+
5+
After k moves, what is the probability that the knight remains on the board?
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
public class Approach{
2+
3+
static int possibleMoves[][] = {
4+
{2, -1},
5+
{2, 1},
6+
{-2, -1},
7+
{-2, 1},
8+
{1, 2},
9+
{1, -2},
10+
{-1, 2},
11+
{-1, -2},
12+
};
13+
14+
static int [][]visited;
15+
16+
// for testing purpose
17+
static void print(){
18+
for (int [] row:visited){
19+
String s = " ";
20+
for(int col: row){
21+
s += col;
22+
}
23+
System.err.println(s);
24+
}
25+
}
26+
27+
static double knightProbability(int K,int N,int row,int col){
28+
if (visited[row][col] == 1) {
29+
return 0;
30+
} else if (K == 0) {
31+
return 1;
32+
}
33+
34+
visited[row][col] = 1;
35+
double probability = 0;
36+
int countMove = 0;
37+
for (int [] knightMove : possibleMoves) {
38+
int xMove = knightMove[0] + row;
39+
int yMove = knightMove[1] + col;
40+
if (0 <= xMove && xMove < N && 0 <= yMove && yMove < N) {
41+
countMove++;
42+
// System.err.println(""+K+""+probability+""+xMove+""+yMove);
43+
// print();
44+
probability += knightProbability(K-1, N, xMove, yMove);
45+
}
46+
}
47+
visited[row][col] = 0;
48+
return countMove>0 ? probability / countMove : probability;
49+
}
50+
51+
public static void main(String args[]){
52+
int N = 3;
53+
int K = 7;
54+
55+
if (visited == null){
56+
visited = new int[N][N];
57+
}
58+
59+
System.out.println(knightProbability(K, N, 0, 0));
60+
}
61+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function knightProbability(K, N, row, col) {
2+
let possibleMoves = [
3+
[2, -1],
4+
[2, 1],
5+
[-2, -1],
6+
[-2, 1],
7+
[1, 2],
8+
[1, -2],
9+
[-1, 2],
10+
[-1, -2],
11+
];
12+
13+
let visited = [];
14+
for (let i = 0; i < N; i++) {
15+
visited.push(Array(N).fill(0));
16+
}
17+
18+
function moveProbability(k, n, i, j) {
19+
if (visited[i][j] == 1) {
20+
return 0;
21+
} else if (k == 0) {
22+
return 1;
23+
}
24+
25+
visited[i][j] = 1;
26+
let probability = 0;
27+
let countMove = 0;
28+
for (let knightMove of possibleMoves) {
29+
let xMove = knightMove[0] + i;
30+
let yMove = knightMove[1] + j;
31+
if (0 <= xMove && xMove < N && 0 <= yMove && yMove < N) {
32+
countMove++;
33+
// console.log(k, probability, xMove, yMove, visited);
34+
probability += moveProbability(k - 1, n, xMove, yMove);
35+
}
36+
}
37+
visited[i][j] = 0;
38+
return countMove ? probability / countMove : probability;
39+
}
40+
return moveProbability(K, N, row, col);
41+
}
42+
43+
console.log(knightProbability(7, 3, 0, 0));

0 commit comments

Comments
 (0)