我有一个字符矩阵,假设是4 * 5矩阵
A-C-P-的˚F
X-S-的 0 -P
V-的û -Q-N
W-G-的Ñ -M
d -A-T-I
我想知道该矩阵中是否存在 FOUND 字样。
我的矩阵如下(JAVA代码)
char matrixOfChars[][]={
{'A','C','P','F'},
{'X','S','O','P'},
{'V','U','Q','N'},
{'W','G','N','M'},
{'D','A','T','I'}};
注意:在矩阵中,第一个字总是在第一行,第二个字总是在第二行,依此类推
目前为单个字符串完成的代码在
之下private static void generateCombinations(String original, String combination) {
if (original.length() == 0) {
System.out.println(combination);
} else {
for(int i=0;i<original.length();i++){
generateCombinations(original.substring(0,i) + original.substring(i+1,original.length()),combination + original.charAt(i));
}
}
}
请帮我在JAVA中找到上面的解决方案。如果使用java递归给出解决方案会更好。
谢谢。
答案 0 :(得分:1)
使用循环迭代FOUND的字符,在矩阵的每一行中使用Arrays.asList
和indexOf
来获取位置并创建一个新数组或所需内容
String word = "FOUND";
int[] sollution = new int[work.length()];
for (int i = 0; i < word.length(); i++) {
int index = Arrays.asList(matrixOfChars[i]).indexOf(word.charAt(i));
System.out.print(index + "\n");
sollution[i] = index;
}
Arrays.toString(sollution);
您将获得此输出:
3
2
1
2
0
sollution = {3 , 2 , 1 , 2 , 0 }
答案 1 :(得分:0)
试试此代码
public class Test {
public static void main(String[] args) {
char matrixOfChars[][]={
{'A','C','P','F'},
{'X','S','O','P'},
{'V','U','Q','N'},
{'W','G','N','M'},
{'D','A','T','I'}};
System.out.println(find(matrixOfChars,5,4, "FOUND"));
}
public static boolean find(char matrixOfChars[][],int rowsSize,int columnSize,String word)
{
if(word.length()!=rowsSize)
return false;
String temp="";
for (int i = 0; i < rowsSize; i++) {
for (int j = 0; j < columnSize; j++) {
if(word.charAt(i)==matrixOfChars[i][j])
{
temp+=word.charAt(i);
break;
}
}
}
System.out.println(temp);
if(temp.equals(word))
{
return true;
}
else
{
return false;
}
}
}
递归解决方案:
public static void main(String[] args) {
char matrixOfChars[][] = { { 'A', 'C', 'P', 'F' },
{ 'X', 'S', 'O', 'P' }, { 'V', 'U', 'Q', 'N' },
{ 'W', 'G', 'N', 'M' }, { 'D', 'A', 'T', 'I' } };
String word="AGUNC";
String res=find2(matrixOfChars, 5, 4, word);
System.out.println(res);
System.out.println(res.equals(word));
}
static String temp = "";
static int n = 0;
public static String find2(char matrixOfChars[][], int rowsSize,
int columnSize, String word) {
if (word.length() != rowsSize || n == rowsSize)
return temp;
else {
for (int j = 0; j < columnSize; j++) {
if (word.charAt(n) == matrixOfChars[n][j]) {
temp += word.charAt(n);
n++;
return find2(matrixOfChars, rowsSize, columnSize, word);
}
}
n++;
return "";
}
}
答案 2 :(得分:0)
使用递归:
class Test {
static char matrixOfChars[][]={
{'A','C','P','F'},
{'X','S','O','P'},
{'V','U','Q','N'},
{'W','G','N','M'},
{'D','A','T','I'}};
static int n = 0;
public static void main(String args[]) {
n = matrixOfChars.length - 1;
find();
}
public static void find() {
String word = "";
if(n >= 0) {
for(int i = 0 ; i < matrixOfChars[n].length ; i ++) {
word += matrixOfChars[n][i];
}
if(word.equals("FOUND")) {
System.out.println("FOUND is found");
} else {
n--;
find();
}
} else {
System.out.println("FOUND is not found");
}
}
}
答案 3 :(得分:0)
尝试以下代码:
char matrixOfChars[][]={
{'A','C','P','F'},
{'X','S','O','P'},
{'V','U','Q','N'},
{'W','G','N','M'},
{'D','A','T','I'}};
String word = "FOUND";
int[] sol = new int[word.length()];
for (int i = 0; i < word.length(); i++) {
char y = word.charAt( i );
int z = new String( matrixOfChars[i] ).indexOf( y );
System.out.print( z + "\n" );
sol[i] = z;
}
System.out.println( Arrays.toString( sol ) );