一个矩阵概率问题
给定一个矩形矩阵,我们可以从当前单元格以相等的概率在 4 个方向上移动。四个方向是右、左、上或下。计算 N 从矩阵中的给定位置 (i, j) 移动后,我们在任何点都不会越过矩阵边界的概率。
我们强烈建议您最小化您的浏览器并首先自己尝试。
这个想法是执行类似于 DFS 的操作。我们在 4 个允许的方向中的每一个上递归遍历,并且对于遇到的每个单元格,我们用更少的移动来计算所需的概率。由于每个方向具有相等的概率,每个方向将贡献该单元格的总概率的 1/4,即 0.25。如果我们跨出矩阵,则返回 0,如果完成 N 步而未跨越矩阵边界,则返回 1。
以下是上述想法的实现:
C++
/// C++ program to find the probability
// that we do not cross boundary of a
// matrix after N moves.
#include
using namespace std;
// check if (x, y) is valid matrix coordinate
bool isSafe(int x, int y, int m, int n)
{
return (x >= 0 && x < m &&
y >= 0 && y < n);
}
// Function to calculate probability
// that after N moves from a given
// position (x, y) in m x n matrix,
// boundaries of the matrix will not be crossed.
double findProbability(int m, int n, int x,
int y, int N)
{
// boundary crossed
if (!isSafe(x, y, m, n))
return 0.0;
// N steps taken
if (N == 0)
return 1.0;
// Initialize result
double prob = 0.0;
// move up
prob += findProbability(m, n, x - 1,
y, N - 1) * 0.25;
// move right
prob += findProbability(m, n, x,
y + 1, N - 1) * 0.25;
// move down
prob += findProbability(m, n, x + 1,
y, N - 1) * 0.25;
// move left
prob += findProbability(m, n, x,
y - 1, N - 1) * 0.25;
return prob;
}
// Driver code
int main()
{
// matrix size
int m = 5, n = 5;
// coordinates of starting point
int i = 1, j = 1;
// Number of steps
int N = 2;
cout << "Probability is "
<< findProbability(m, n, i, j, N);
return 0;
}
Java
// Java program to find the probability
// that we do not cross boundary
// of a matrix after N moves.
import java.io.*;
class GFG {
// check if (x, y) is valid
// matrix coordinate
static boolean isSafe(int x, int y,
int m, int n)
{
return (x >= 0 && x < m &&
y >= 0 && y < n);
}
// Function to calculate probability
// that after N moves from a given
// position (x, y) in m x n matrix,
// boundaries of the matrix will
// not be crossed.
static double findProbability(int m, int n,
int x, int y,
int N)
{
// boundary crossed
if (! isSafe(x, y, m, n))
return 0.0;
// N steps taken
if (N == 0)
return 1.0;
// Initialize result
double prob = 0.0;
// move up
prob += findProbability(m, n, x - 1,
y, N - 1) * 0.25;
// move right
prob += findProbability(m, n, x, y + 1,
N - 1) * 0.25;
// move down
prob += findProbability(m, n, x + 1,
y, N - 1) * 0.25;
// move left
prob += findProbability(m, n, x, y - 1,
N - 1) * 0.25;
return prob;
}
// Driver code
public static void main (String[] args)
{
// matrix size
int m = 5, n = 5;
// coordinates of starting point
int i = 1, j = 1;
// Number of steps
int N = 2;
System.out.println("Probability is " +
findProbability(m, n, i,
j, N));
}
}
// This code is contributed by KRV.
Python3
# Python3 program to find the probability
# that we do not cross boundary of a
# matrix after N moves.
# check if (x, y) is valid matrix coordinate
def isSafe(x, y, m, n):
return (x >= 0 and x < m and
y >= 0 and y < n)
# Function to calculate probability
# that after N moves from a given
# position (x, y) in m x n matrix,
# boundaries of the matrix will
# not be crossed.
def findProbability(m, n, x, y, N):
# boundary crossed
if (not isSafe(x, y, m, n)):
return 0.0
# N steps taken
if (N == 0):
return 1.0
# Initialize result
prob = 0.0
# move up
prob += findProbability(m, n, x - 1,
y, N - 1) * 0.25
# move right
prob += findProbability(m, n, x,
y + 1, N - 1) * 0.25
# move down
prob += findProbability(m, n, x + 1,
y, N - 1) * 0.25
# move left
prob += findProbability(m, n, x,
y - 1, N - 1) * 0.25
return prob
# Driver code
if __name__ == '__main__':
# matrix size
m = 5
n = 5
# coordinates of starting po
i = 1
j = 1
# Number of steps
N = 2
print("Probability is",
findProbability(m, n, i, j, N))
# This code is contributed by PranchalK
C#
// C# program to find the probability
// that we do not cross boundary
// of a matrix after N moves.
using System;
class GFG
{
// check if (x, y) is valid
// matrix coordinate
static bool isSafe(int x, int y,
int m, int n)
{
return (x >= 0 && x < m &&
y >= 0 && y < n);
}
// Function to calculate probability
// that after N moves from a given
// position (x, y) in m x n matrix,
// boundaries of the matrix will
// not be crossed.
static double findProbability(int m, int n,
int x, int y,
int N)
{
// boundary crossed
if (! isSafe(x, y, m, n))
return 0.0;
// N steps taken
if (N == 0)
return 1.0;
// Initialize result
double prob = 0.0;
// move up
prob += findProbability(m, n, x - 1,
y, N - 1) * 0.25;
// move right
prob += findProbability(m, n, x, y + 1,
N - 1) * 0.25;
// move down
prob += findProbability(m, n, x + 1,
y, N - 1) * 0.25;
// move left
prob += findProbability(m, n, x, y - 1,
N - 1) * 0.25;
return prob;
}
// Driver code
public static void Main ()
{
// matrix size
int m = 5, n = 5;
// coordinates of starting point
int i = 1, j = 1;
// Number of steps
int N = 2;
Console.Write("Probability is " +
findProbability(m, n, i,
j, N));
}
}
// This code is contributed by nitin mittal.
PHP
= 0 && $x < $m &&
$y >= 0 && $y < $n);
}
// Function to calculate probability
// that after N moves from a given
// position (x, y) in m x n matrix,
// boundaries of the matrix will
// not be crossed.
function findProbability($m, $n, $x,
$y, $N)
{
// boundary crossed
if (!isSafe($x, $y, $m, $n))
return 0.0;
// N steps taken
if ($N == 0)
return 1.0;
// Initialize result
$prob = 0.0;
// move up
$prob += findProbability($m, $n, $x - 1,
$y, $N - 1) * 0.25;
// move right
$prob += findProbability($m, $n, $x,
$y + 1, $N - 1) * 0.25;
// move down
$prob += findProbability($m, $n, $x + 1,
$y, $N - 1) * 0.25;
// move left
$prob += findProbability($m, $n, $x,
$y - 1, $N - 1) * 0.25;
return $prob;
}
// Driver code
// matrix size
$m = 5; $n = 5;
// coordinates of starting point
$i = 1; $j = 1;
// Number of steps
$N = 2;
echo "Probability is ",
findProbability($m, $n, $i, $j, $N);
// This code is contributed by nitin mittal.
?>
Javascript
输出 :
Probability is 0.875