给定二进制N x N矩阵,我们需要找到存在无穷路径的矩阵位置的总数。当且仅当位置(i,j)的值为1并且行(i)和列(j)的所有下一个位置都应具有值时,任何位置(i,j)才被认为具有无限路径1.如果行(i)或列(j)中(i,j)旁边的任何位置都为0,则位置(i,j)没有任何无穷路径。
例子:
输入:0 1 0 1 1 1 0 1 1输出:4无限点是(1、1),(1、2),(2、1)和(2、2)。对于所有其他点,到某个角的路径在某个点处被阻塞。 输入:0 1 1 1 1 0 0 1 0输出:1端点为(0,2)。
天真的方法:
我们遍历所有位置,对于每个位置,我们都要检查该位置是否有无尽的路径。如果是,则将其计数,否则将其忽略。但是像往常一样,它的时间复杂度似乎很高。
时间复杂度:O(n 3 )
进阶方法(动态编程):
我们可以很容易地说,如果在任何位置都为零,那么它将阻塞所有留在其顶部和顶部的位置的路径。
同样,我们可以说,如果(i,j + 1)将具有无限行且(i,j)的值为1,则任何位置(i,j)将具有无限行。
类似地,我们可以说,如果(i + 1,j)将具有无限列且(i,j)的值为1,则任何位置(i,j)将具有无限列。
因此,我们应该维护两个矩阵,一个矩阵用于行,一个矩阵用于列。始终从最右边的行开始,从最下面的位置开始,仅检查下一个位置是否有无尽的路径。
最后,如果任何位置在行矩阵和列矩阵中都具有无限路径,则该位置被称为具有无限路径。
C++
// C++ program to find count of endless points
#include
using namespace std;
const int MAX = 100;
// Returns count of endless points
int countEndless(bool input[][MAX], int n)
{
bool row[n][n], col[n][n];
// Fills column matrix. For every column, start
// from every last row and fill every entry as
// blockage after a 0 is found.
for (int j=0; j=0; i--)
{
// encountered a '0', set the isEndless
// variable to false
if (input[i][j] == 0)
isEndless = 0;
col[i][j] = isEndless;
}
}
// Similarly, fill row matrix
for (int i=0; i=0; j--)
{
if (input[i][j] == 0)
isEndless = 0;
row[i][j] = isEndless;
}
}
// Calculate total count of endless points
int ans = 0;
for (int i=0; i
Java
// Java program to find count of endless points
class GFG {
static final int MAX = 100;
// Returns count of endless points
static int countEndless(boolean input[][], int n)
{
boolean row[][] = new boolean[n][n];
boolean col[][] = new boolean[n][n];
// Fills column matrix. For every column,
// start from every last row and fill every
// entry as blockage after a 0 is found.
for (int j = 0; j < n; j++)
{
// flag which will be zero once we get
// a '0' and it will be 1 otherwise
boolean isEndless = true;
for (int i = n-1; i >= 0; i--)
{
// encountered a '0', set the
// isEndless variable to false
if (input[i][j] == false)
isEndless = false;
col[i][j] = isEndless;
}
}
// Similarly, fill row matrix
for (int i = 0; i < n; i++)
{
boolean isEndless = true;
for (int j = n-1; j >= 0; j--)
{
if (input[i][j] == false)
isEndless = false;
row[i][j] = isEndless;
}
}
// Calculate total count of endless points
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = 1; j < n; j++)
// If there is NO blockage in row
// or column after this point,
// increment result.
if (row[i][j] && col[i][j])
ans++;
return ans;
}
//driver code
public static void main(String arg[])
{
boolean input[][] = {
{true, false, true, true},
{false, true, true, true},
{true, true, true, true},
{false, true, true, false}};
int n = 4;
System.out.print(countEndless(input, n));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to find count of
# endless points
import numpy as np
# Returns count of endless points
def countEndless(input_mat, n) :
row = np.zeros((n, n))
col = np.zeros((n, n))
# Fills column matrix. For every column,
# start from every last row and fill
# every entry as blockage after a 0 is found.
for j in range(n) :
# flag which will be zero once we
# get a '0' and it will be 1 otherwise
isEndless = 1
for i in range(n - 1, -1, -1) :
# encountered a '0', set the
# isEndless variable to false
if (input_mat[i][j] == 0) :
isEndless = 0
col[i][j] = isEndless
# Similarly, fill row matrix
for i in range(n) :
isEndless = 1
for j in range(n - 1, -1, -1) :
if (input_mat[i][j] == 0) :
isEndless = 0
row[i][j] = isEndless
# Calculate total count of endless points
ans = 0
for i in range(n) :
for j in range(1, n) :
# If there is NO blockage in row
# or column after this point,
# increment result.
#print(row[i][j] , col[i][j])
if (row[i][j] and col[i][j]) :
ans += 1
#print(ans)
return ans
# Driver code
if __name__ == "__main__" :
input_mat = [[1, 0, 1, 1],
[0, 1, 1, 1],
[1, 1, 1, 1],
[0, 1, 1, 0]]
n = 4
print(countEndless(input_mat, n))
# This code is contributed by Ryuga
C#
// C# program to find count of
// endless points
using System;
public class GFG {
// Returns count of endless points
static int countEndless(bool [,]input, int n)
{
bool [,]row = new bool[n,n];
bool [,]col = new bool[n,n];
// Fills column matrix. For every
// column, start from every last
// row and fill every entry as
// blockage after a 0 is found.
for (int j = 0; j < n; j++)
{
// flag which will be zero
// once we get a '0' and it
// will be 1 otherwise
bool isEndless = true;
for (int i = n - 1; i >= 0; i--)
{
// encountered a '0', set
// the isEndless variable
// to false
if (input[i,j] == false)
isEndless = false;
col[i,j] = isEndless;
}
}
// Similarly, fill row matrix
for (int i = 0; i < n; i++)
{
bool isEndless = true;
for (int j = n - 1; j >= 0; j--)
{
if (input[i,j] == false)
isEndless = false;
row[i,j] = isEndless;
}
}
// Calculate total count of
// endless points
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = 1; j < n; j++)
// If there is NO blockage
// in row or column after
// this point, increment
// result.
if (row[i,j] && col[i,j])
ans++;
return ans;
}
//Driver code
public static void Main()
{
bool [,]input = {
{true, false, true, true},
{false, true, true, true},
{true, true, true, true},
{false, true, true, false}};
int n = 4;
Console.Write(countEndless(input, n));
}
}
// This code is contributed by Sam007.
PHP
= 0; $i--)
{
// encountered a '0',
// set the isEndless
// variable to false
if ($input[$i][$j] == 0)
$isEndless = 0;
$col[$i][$j] = $isEndless;
}
}
// Similarly, fill row matrix
for ($i = 0; $i < $n; $i++)
{
$isEndless = 1;
for ($j = $n - 1; $j >= 0; $j--)
{
if ($input[$i][$j] == 0)
$isEndless = 0;
$row[$i][$j] = $isEndless;
}
}
// Calculate total count
// of endless points
$ans = 0;
for ($i = 0; $i < $n; $i++)
for ($j = 1; $j < $n; $j++)
// If there is NO blockage
// or column after this point,
// increment result.
if ($row[$i][$j] &&
$col[$i][$j])
$ans++;
return $ans;
}
// Driver code
$input = array(array(1, 0, 1, 1),
array(0, 1, 1, 1),
array(1, 1, 1, 1),
array(0, 1, 1, 0));
$n = 4;
echo countEndless($input, $n);
// This code is contributed
// by shiv_bhakt.
?>
输出:
5