我正在尝试编写一种算法来在给定的子矩阵中查找子矩阵。为了解决这个问题,我编写了以下代码:
public class SubMatTry {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = { { 2, 3, 5, 7 }, { 5, 8, 3, 5 }, { 7, 6, 9, 2 },
{ 3, 8, 5, 9 } };
int b[][] = { { 9, 2 }, { 5, 9 } };
int k = 0;
int l = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.println("Element of a= " + a[i][j]);
if (b[k][l] == a[i][j]) {
System.out.println(b[k][l] + " = " + a[i][j]);
if (b[k][l + 1] == a[i][j + 1]) {
System.out.println(b[k][l + 1] + " = " + a[i][j + 1]);
if (b[k + 1][l] == a[i + 1][j]) {
System.out.println(b[k + 1][l] + " = "
+ a[i + 1][j]);
if (b[k + 1][l + 1] == a[i + 1][j + 1]) {
System.out.println(b[k + 1][l + 1] + " = "
+ a[i + 1][j + 1]);
System.out.println("Array found at" + i + " ,"
+ j);
System.exit(0);
}
}
}
}
}
}
}}
这段代码工作正常,但我不确定它是问题的确切解决方案还是只是一种解决方法。请提供您的专家意见。提前致谢。