688. Knight Probability in Chessboard
阿新 • • 發佈:2017-11-05
ability als oba oca check return pro board int
1 /* 2 dp[k][i][j]代表從坐標為(i,j)的地方走k步,還在棋盤內的概率 3 狀態方程:dp[k][i][j] = dp[k-1][x][y]*(本次走的8種情況中不會出局的數量)/8 4 若情況1,2,3不會出局,那麽 5 for(i= (1,2,3)): 6 dp[k][i][j] += dp[k-1][x][y]*1/8 7 */ 8 public double knightProbability(int N, int K, int r, int c) { 9 int[][] location = newint[][]{{-2,-1},{-1,-2},{1,-2},{2,-1},{-2,1},{-1,2},{1,2},{2,1}}; 10 double[][][] dp = new double[K+1][N][N]; 11 //初始值,當一步也不走時,概率是1 12 for(int i = 0;i < N;i++) 13 { 14 Arrays.fill(dp[0][i],1); 15 } 16 return move(N,K,location,dp,r,c); 17 }18 boolean check(int x,int y,int n) 19 { 20 if (x>=0 && y>=0 && x<n && y <n) 21 return true; 22 else return false; 23 } 24 double move(int N, int K,int[][] location,double[][][] dp,int r,int c) 25 { 26 for (int k = 1;k <= K;k++)27 { 28 for (int i = 0; i < N; i++) { 29 for (int j = 0; j < N; j++) { 30 for (int[] a : 31 location) { 32 int x = i + a[0]; 33 int y = j + a[1]; 34 if (check(x,y,N)) 35 //這裏第一次把dp[k-1][x][y]寫成了dp[k-1][i][j] 36 //這裏的思想其實是,從當前走k步不出局的概率是從下一步(8種走法)走k-1步的概率相加 37 dp[k][i][j] += dp[k-1][x][y]/8; 38 } 39 } 40 } 41 } 42 return dp[K][r][c]; 43 }
688. Knight Probability in Chessboard