# LWC 52：688. Knight Probability in Chessboard

## LWC 52：688. Knight Probability in Chessboard

Problem:

On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1). A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.

Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there. The knight continues moving until it has made exactly K moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving.

Example:

Input: 3, 2, 0, 0 Output: 0.0625 Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board. From each of those positions, there are also two moves that will keep the knight on the board. The total probability the knight stays on the board is 0.0625.

Note:

N will be between 1 and 25.

K will be between 0 and 100.

The knight always initially starts on the board.

```    int[][] dir = {{2, 1},{2, -1},{-2, -1},{-2, 1},{1, 2},{1, -2},{-1, 2},{-1, -2}};

class Pair{
int id;
double pro;

Pair(int id, double pro){
this.id = id;
this.pro = pro;
}
}

public double knightProbability(int N, int K, int r, int c) {

Queue<Pair> queue = new ArrayDeque<>();
queue.offer(new Pair(r * N + c, 1));

int turn = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; ++i) {
Pair p = queue.poll();
int cnt = 0;
List<Integer> tmp = new ArrayList<>();
for (int[] d : dir) {
int nx = d[0] + p.id / N;
int ny = d[1] + p.id % N;
if (check(nx, ny, N)) {
cnt ++;
}
}
for (int j = 0; j < tmp.size(); ++j) {
queue.offer(new Pair(tmp.get(j), p.pro * cnt / 8));
}
}
turn ++;
if (turn == K) break;
}

double ans = 0;
int size = queue.size();
while (!queue.isEmpty()) {
ans += (queue.poll().pro / size);
}

return ans;
}

boolean check(int i, int j, int N) {
return i >= 0 && i < N && j >= 0 && j < N;
}```

```    int[][] dir = {{2, 1},{2, -1},{-2, -1},{-2, 1},{1, 2},{1, -2},{-1, 2},{-1, -2}};

double[][][] mem = new double[102][32][32];
public double f(int N, int K, int R, int C) {
if (K == 0) {
return 1;
}
if (mem[K][R][C] > 0) return mem[K][R][C];
double res = 0.0;
for (int[] d : dir) {
int nx = R + d[0];
int ny = C + d[1];
if (check(nx, ny, N)) {
res += 1 / 8.0 * f(N, K - 1, nx, ny);
}
}

return mem[K][R][C] = res;
}

public void fill(double[][][] mem) {
for (int i = 0; i < mem.length; ++i) {
for (int j = 0; j < mem[i].length; ++j) {
for (int k = 0; k < mem[i][j].length; ++k) {
mem[i][j][k] = -1;
}
}
}
}

boolean check(int i, int j, int N) {
return i >= 0 && i < N && j >= 0 && j < N;
}

public double knightProbability(int N, int K, int r, int c) {
fill(mem);
return f(N, K, r, c);
}```

```    int[][] dir = {{2, 1},{2, -1},{-2, -1},{-2, 1},{1, 2},{1, -2},{-1, 2},{-1, -2}};

boolean check(int i, int j, int N) {
return i >= 0 && i < N && j >= 0 && j < N;
}

public double knightProbability(int N, int K, int r, int c) {
double[][][] mem = new double[102][32][32];
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
mem[0][i][j] = 1;
}
}

for (int k = 1; k <= K; ++k) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
for (int[] d : dir) {
int nx = i + d[0];
int ny = j + d[1];
if (check(nx, ny, N)) {
mem[k][i][j] += 1 / 8.0 * mem[k - 1][nx][ny];
}
}
}
}
}
return mem[K][r][c];
}```

98 篇文章33 人订阅

0 条评论

## 相关文章

### js登录滑动验证，不滑动无法登陆

js的判断这里是根据滑块的位置进行判断，应该是用一个flag判断 <%@ page language="java" contentType="text/html...

8908

### Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

3017

5288

### jsencrypt参数前端加密c#解密

写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证，如何防止提交的请求不被抓包后串改，虽然无法说绝对安全却给非法提交提高了难度...

4269

### LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

3025

### sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

3030

4224

3852

4425

### Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2956