原文:
给定一个仅包含较低字母字符的矩阵,我们需要在给定的矩阵中打印所有回文路径。 路径定义为从左上角的单元格到右下角的单元格的一系列单元格。 我们只能从当前单元格左右移动。 我们不能斜着走。
例如:
input : mat[][] = {"aaab",
"baaa"
"abba"}
output :aaaaaa, aaaaaa, abaaba
explanation :
aaaaaa (0, 0) -> (0, 1) -> (1, 1) ->
(1, 2) -> (1, 3) -> (2, 3)
aaaaaa (0, 0) -> (0, 1) -> (0, 2) ->
(1, 2) -> (1, 3) -> (2, 3)
abaaba (0, 0) -> (1, 0) -> (1, 1) ->
(1, 2) -> (2, 2) -> (2, 3)
输出数组中元素的顺序无关紧要。
这个想法很简单。 我们从左上角(0, 0)
开始,探索到右下角的所有路径。 如果路径变成回文,我们将其打印出来。
c
// c program to print all palindromic paths
// from top left to bottom right in a grid.
#include
using namespace std;
#define n 4
bool ispalin(string str)
{
int len = str.length() / 2;
for (int i = 0; i < len; i )
{
if (str[i] != str[str.length() - i - 1])
return false;
}
return true;
}
// i and j are row and column indexes of current cell
// (initially these are 0 and 0).
void palindromicpath(string str, char a[][n],
int i, int j, int m, int n)
{
// if we have not reached bottom right corner, keep
// exlporing
if (j < m - 1 || i < n - 1)
{
if (i < n - 1)
palindromicpath(str a[i][j], a, i 1, j, m, n);
if (j < m - 1)
palindromicpath(str a[i][j], a, i, j 1, m, n);
}
// if we reach bottom right corner, we check if
// if the path used is palindrome or not.
else {
str = str a[n - 1][m - 1];
if (ispalin(str))
cout<<(str)<
java
// java program to print all palindromic paths
// from top left to bottom right in a grid.
public class palinpath {
public static boolean ispalin(string str)
{
int len = str.length() / 2;
for (int i = 0; i < len; i ) {
if (str.charat(i) != str.charat(str.length() - i - 1))
return false;
}
return true;
}
// i and j are row and column indexes of current cell
// (initially these are 0 and 0).
public static void palindromicpath(string str, char a[][],
int i, int j, int m, int n)
{
// if we have not reached bottom right corner, keep
// exlporing
if (j < m - 1 || i < n - 1) {
if (i < n - 1)
palindromicpath(str a[i][j], a, i 1, j, m, n);
if (j < m - 1)
palindromicpath(str a[i][j], a, i, j 1, m, n);
}
// if we reach bottom right corner, we check if
// if the path used is palindrome or not.
else {
str = str a[n - 1][m - 1];
if (ispalin(str))
system.out.println(str);
}
}
// driver code
public static void main(string args[])
{
char arr[][] = { { 'a', 'a', 'a', 'b' },
{ 'b', 'a', 'a', 'a' },
{ 'a', 'b', 'b', 'a' } };
string str = "";
palindromicpath(str, arr, 0, 0, 4, 3);
}
}
python 3
# python 3 program to print all
# palindromic paths from top left
# to bottom right in a grid.
def ispalin(str):
l = len(str) // 2
for i in range( l) :
if (str[i] != str[len(str) - i - 1]):
return false
return true
# i and j are row and column
# indexes of current cell
# (initially these are 0 and 0).
def palindromicpath(str, a, i, j, m, n):
# if we have not reached bottom
# right corner, keep exlporing
if (j < m - 1 or i < n - 1) :
if (i < n - 1):
palindromicpath(str a[i][j], a,
i 1, j, m, n)
if (j < m - 1):
palindromicpath(str a[i][j], a,
i, j 1, m, n)
# if we reach bottom right corner,
# we check if the path used is
# palindrome or not.
else :
str = str a[n - 1][m - 1]
if ispalin(str):
print(str)
# driver code
if __name__ == "__main__":
arr = [[ 'a', 'a', 'a', 'b' ],
['b', 'a', 'a', 'a' ],
[ 'a', 'b', 'b', 'a' ]]
str = ""
palindromicpath(str, arr, 0, 0, 4, 3)
# this code is contributed
# by chitranayal
c
// c# program to print all palindromic paths
// from top left to bottom right in a grid.
using system;
class gfg
{
public static bool ispalin(string str)
{
int len = str.length / 2;
for (int i = 0; i < len; i )
{
if (str[i] != str[str.length - i - 1])
{
return false;
}
}
return true;
}
// i and j are row and column indexes of
// current cell (initially these are 0 and 0).
public static void palindromicpath(string str, char[][] a,
int i, int j, int m, int n)
{
// if we have not reached bottom
// right corner, keep exlporing
if (j < m - 1 || i < n - 1)
{
if (i < n - 1)
{
palindromicpath(str a[i][j],
a, i 1, j, m, n);
}
if (j < m - 1)
{
palindromicpath(str a[i][j],
a, i, j 1, m, n);
}
}
// if we reach bottom right corner,
// we check if the path used is
// palindrome or not.
else
{
str = str a[n - 1][m - 1];
if (ispalin(str))
{
console.writeline(str);
}
}
}
// driver code
public static void main(string[] args)
{
char[][] arr = new char[][]
{
new char[] {'a', 'a', 'a', 'b'},
new char[] {'b', 'a', 'a', 'a'},
new char[] {'a', 'b', 'b', 'a'}
};
string str = "";
palindromicpath(str, arr, 0, 0, 4, 3);
}
}
// this code is contributed by shrikant13
输出:
abaaba
aaaaaa
aaaaaa
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处