Given a rectangular matrix, we can move from current cell in 4 directions with equal probability. The 4 directions are right, left, top or bottom. Calculate the Probability that after N moves from a given position (i, j) in the matrix, we will not cross boundaries of the matrix at any point.
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to perform something similar to DFS. We recursively traverse in each of the 4 allowed direction and for each cell encountered, we calculate the required probability with one less move. As each direction has equal probability, each direction will contribute to 1/4 of total probability of that cell i.e. 0.25. We return 0 if we step outside the matrix and return 1 if N steps are completed without crossing matrix boundaries.
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
bool isSafe( int x, int y, int m, int n)
{
return (x >= 0 && x < m &&
y >= 0 && y < n);
}
double findProbability( int m, int n, int x,
int y, int N)
{
if (!isSafe(x, y, m, n))
return 0.0;
if (N == 0)
return 1.0;
double prob = 0.0;
prob += findProbability(m, n, x - 1,
y, N - 1) * 0.25;
prob += findProbability(m, n, x,
y + 1, N - 1) * 0.25;
prob += findProbability(m, n, x + 1,
y, N - 1) * 0.25;
prob += findProbability(m, n, x,
y - 1, N - 1) * 0.25;
return prob;
}
int main()
{
int m = 5, n = 5;
int i = 1, j = 1;
int N = 2;
cout << "Probability is "
<< findProbability(m, n, i, j, N);
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
bool isSafe( int x, int y, int m, int n)
{
return (x >= 0 && x < m && y >= 0 && y < n);
}
double findProbability( int m, int n, int x, int y, int N)
{
if (!isSafe(x, y, m, n))
return 0.0;
if (N == 0)
return 1.0;
double prob = 0.0;
prob += findProbability(m, n, x - 1, y, N - 1) * 0.25;
prob += findProbability(m, n, x, y + 1, N - 1) * 0.25;
prob += findProbability(m, n, x + 1, y, N - 1) * 0.25;
prob += findProbability(m, n, x, y - 1, N - 1) * 0.25;
return prob;
}
int main()
{
int m = 5, n = 5;
int i = 1, j = 1;
int N = 2;
printf ( "Probability is %f" ,findProbability(m, n, i, j, N));
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isSafe( int x, int y,
int m, int n)
{
return (x >= 0 && x < m &&
y >= 0 && y < n);
}
static double findProbability( int m, int n,
int x, int y,
int N)
{
if (! isSafe(x, y, m, n))
return 0.0 ;
if (N == 0 )
return 1.0 ;
double prob = 0.0 ;
prob += findProbability(m, n, x - 1 ,
y, N - 1 ) * 0.25 ;
prob += findProbability(m, n, x, y + 1 ,
N - 1 ) * 0.25 ;
prob += findProbability(m, n, x + 1 ,
y, N - 1 ) * 0.25 ;
prob += findProbability(m, n, x, y - 1 ,
N - 1 ) * 0.25 ;
return prob;
}
public static void main (String[] args)
{
int m = 5 , n = 5 ;
int i = 1 , j = 1 ;
int N = 2 ;
System.out.println( "Probability is " +
findProbability(m, n, i,
j, N));
}
}
|
Python3
def isSafe(x, y, m, n):
return (x > = 0 and x < m and
y > = 0 and y < n)
def findProbability(m, n, x, y, N):
if ( not isSafe(x, y, m, n)):
return 0.0
if (N = = 0 ):
return 1.0
prob = 0.0
prob + = findProbability(m, n, x - 1 ,
y, N - 1 ) * 0.25
prob + = findProbability(m, n, x,
y + 1 , N - 1 ) * 0.25
prob + = findProbability(m, n, x + 1 ,
y, N - 1 ) * 0.25
prob + = findProbability(m, n, x,
y - 1 , N - 1 ) * 0.25
return prob
if __name__ = = '__main__' :
m = 5
n = 5
i = 1
j = 1
N = 2
print ( "Probability is" ,
findProbability(m, n, i, j, N))
|
C#
using System;
class GFG
{
static bool isSafe( int x, int y,
int m, int n)
{
return (x >= 0 && x < m &&
y >= 0 && y < n);
}
static double findProbability( int m, int n,
int x, int y,
int N)
{
if (! isSafe(x, y, m, n))
return 0.0;
if (N == 0)
return 1.0;
double prob = 0.0;
prob += findProbability(m, n, x - 1,
y, N - 1) * 0.25;
prob += findProbability(m, n, x, y + 1,
N - 1) * 0.25;
prob += findProbability(m, n, x + 1,
y, N - 1) * 0.25;
prob += findProbability(m, n, x, y - 1,
N - 1) * 0.25;
return prob;
}
public static void Main ()
{
int m = 5, n = 5;
int i = 1, j = 1;
int N = 2;
Console.Write( "Probability is " +
findProbability(m, n, i,
j, N));
}
}
|
PHP
<?php
function isSafe( $x , $y , $m , $n )
{
return ( $x >= 0 && $x < $m &&
$y >= 0 && $y < $n );
}
function findProbability( $m , $n , $x ,
$y , $N )
{
if (!isSafe( $x , $y , $m , $n ))
return 0.0;
if ( $N == 0)
return 1.0;
$prob = 0.0;
$prob += findProbability( $m , $n , $x - 1,
$y , $N - 1) * 0.25;
$prob += findProbability( $m , $n , $x ,
$y + 1, $N - 1) * 0.25;
$prob += findProbability( $m , $n , $x + 1,
$y , $N - 1) * 0.25;
$prob += findProbability( $m , $n , $x ,
$y - 1, $N - 1) * 0.25;
return $prob ;
}
$m = 5; $n = 5;
$i = 1; $j = 1;
$N = 2;
echo "Probability is " ,
findProbability( $m , $n , $i , $j , $N );
?>
|
Javascript
<script>
function isSafe(x, y, m, n){
return (x >= 0 && x < m &&
y >= 0 && y < n);
}
function findProbability( m, n, x,
y, N){
if (!isSafe(x, y, m, n))
return 0.0;
if (N == 0)
return 1.0;
let prob = 0.0;
prob += findProbability(m, n, x - 1,
y, N - 1) * 0.25;
prob += findProbability(m, n, x,
y + 1, N - 1) * 0.25;
prob += findProbability(m, n, x + 1,
y, N - 1) * 0.25;
prob += findProbability(m, n, x,
y - 1, N - 1) * 0.25;
return prob;
}
let m = 5, n = 5;
let i = 1, j = 1;
let N = 2;
document.write( "Probability is "
+findProbability(m, n, i, j, N));
</script>
|
OutputProbability is 0.875
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.