给定N,它表示该人在数字线上的初始位置。还给定L,这是该人离开的可能性。在N从点N移动之后,找到到达数字线上所有点的概率。每次移动都可以向左或向右。
例子:
Input: n = 2, l = 0.5
Output: 0.2500 0.0000 0.5000 0.0000 0.2500
The person can’t reach n-1th position and n+1th position in 2 passes, hence the probability is 0. The person can reach 0th position by only moving 2 steps left from index 2, hence the probability of reaching 0th index is 05*0.5=0.25. Similarly for 2n index, the probability is 0.25.
Input: n = 3, l = 0.1
Output: 0.0010 0.0000 0.0270 0.0000 0.2430 0.0000 0.7290
The person can reach n-1th in three ways, i.e., (llr, lrl, rll) where l denotes left and r denotes right. Hence the probability of n-1th index is 0.027. Similarly probabilities for all other points are also calculated.
方法:构造一个数组arr [n + 1] [2n + 1] ,其中每一行代表一个遍,而各列代表该线上的点。一个人可以从索引N移动的最大值是左侧的第0个索引或右侧的第2n个索引。最初,经过一遍的概率将为arr [1] [n-1]保留,而为arr [1] [n + 1]保留。剩下的n-1个动作将完成,因此两个可能的动作将是向右n步或向左n步。因此,所有人的左右移动的重复关系将是:
arr[i][j] += (arr[i – 1][j – 1] * right)
arr[i][j] += (arr[i – 1][j + 1] * left)
任何索引的所有可能移动的概率之和将存储在arr [n] [i]中。
下面是上述方法的实现:
C++
// C++ program to calculate the
// probability of reaching all points
// after N moves from point N
#include
using namespace std;
// Function to calculate the probabilities
void printProbabilities(int n, double left)
{
double right = 1 - left;
// Array where row represent the pass and the
// coloumn represents the points on the line
double arr[n + 1][2 * n + 1] = {{0}};
// Initially the person can reach left
// or right with one move
arr[1][n + 1] = right;
arr[1][n - 1] = left;
// Calculate probabilities for N-1 moves
for (int i = 2; i <= n; i++)
{
// when the person moves from ith index in
// right direction when i moves has been done
for (int j = 1; j <= 2 * n; j++)
arr[i][j] += (arr[i - 1][j - 1] * right);
// when the person moves from ith index in
// left direction when i moves has been done
for (int j = 2 * n - 1; j >= 0; j--)
arr[i][j] += (arr[i - 1][j + 1] * left);
}
// Print the arr
for (int i = 0; i < 2*n+1; i++)
printf("%5.4f ", arr[n][i]);
}
// Driver Code
int main()
{
int n = 2;
double left = 0.5;
printProbabilities(n, left);
return 0;
}
/* This code is contributed by SujanDutta */
Java
// Java program to calculate the
// probability of reaching all points
// after N moves from point N
import java.util.*;
class GFG {
// Function to calculate the probabilities
static void printProbabilities(int n, double left)
{
double right = 1 - left;
// Array where row represent the pass and the
// coloumn represents the points on the line
double[][] arr = new double[n + 1][2 * n + 1];
// Initially the person can reach left
// or right with one move
arr[1][n + 1] = right;
arr[1][n - 1] = left;
// Calculate probabilities for N-1 moves
for (int i = 2; i <= n; i++) {
// when the person moves from ith index in
// right direction when i moves has been done
for (int j = 1; j <= 2 * n; j++) {
arr[i][j] += (arr[i - 1][j - 1] * right);
}
// when the person moves from ith index in
// left direction when i moves has been done
for (int j = 2 * n - 1; j >= 0; j--) {
arr[i][j] += (arr[i - 1][j + 1] * left);
}
}
// Calling function to print the array with probabilities
printArray(arr, n);
}
// Function that prints the array
static void printArray(double[][] arr, int n)
{
for (int i = 0; i < arr[0].length; i++) {
System.out.printf("%5.4f ", arr[n][i]);
}
}
// Driver Code
public static void main(String[] args)
{
int n = 2;
double left = 0.5;
printProbabilities(n, left);
}
}
Python3
# Python3 program to calculate the
# probability of reaching all points
# after N moves from point N
# Function to calculate the probabilities
def printProbabilities(n, left):
right = 1 - left;
# Array where row represent the pass
# and the coloumn represents the
# points on the line
arr = [[0 for j in range(2 * n + 1)]
for i in range(n + 1)]
# Initially the person can reach
# left or right with one move
arr[1][n + 1] = right;
arr[1][n - 1] = left;
# Calculate probabilities
# for N-1 moves
for i in range(2, n + 1):
# When the person moves from ith
# index in right direction when i
# moves has been done
for j in range(1, 2 * n + 1):
arr[i][j] += (arr[i - 1][j - 1] * right);
# When the person moves from ith
# index in left direction when i
# moves has been done
for j in range(2 * n - 1, -1, -1):
arr[i][j] += (arr[i - 1][j + 1] * left);
# Print the arr
for i in range(2 * n + 1):
print("{:5.4f} ".format(arr[n][i]), end = ' ');
# Driver code
if __name__=="__main__":
n = 2;
left = 0.5;
printProbabilities(n, left);
# This code is contributed by rutvik_56
C#
// C# program to calculate the
// probability of reaching all points
// after N moves from point N
using System;
class GFG
{
// Function to calculate the probabilities
static void printProbabilities(int n, double left)
{
double right = 1 - left;
// Array where row represent the pass and the
// coloumn represents the points on the line
double[,] arr = new double[n + 1,2 * n + 1];
// Initially the person can reach left
// or right with one move
arr[1,n + 1] = right;
arr[1,n - 1] = left;
// Calculate probabilities for N-1 moves
for (int i = 2; i <= n; i++)
{
// when the person moves from ith index in
// right direction when i moves has been done
for (int j = 1; j <= 2 * n; j++)
{
arr[i, j] += (arr[i - 1, j - 1] * right);
}
// when the person moves from ith index in
// left direction when i moves has been done
for (int j = 2 * n - 1; j >= 0; j--)
{
arr[i, j] += (arr[i - 1, j + 1] * left);
}
}
// Calling function to print the array with probabilities
printArray(arr, n);
}
// Function that prints the array
static void printArray(double[,] arr, int n)
{
for (int i = 0; i < GetRow(arr,0).GetLength(0); i++)
{
Console.Write("{0:F4} ", arr[n,i]);
}
}
public static double[] GetRow(double[,] matrix, int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new double[rowLength];
for (var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
// Driver Code
public static void Main(String[] args)
{
int n = 2;
double left = 0.5;
printProbabilities(n, left);
}
}
/* This code contributed by PrinciRaj1992 */
0.2500 0.0000 0.5000 0.0000 0.2500
时间复杂度:O(N 2 )
辅助空间:O(N 2 )