当前位置: 动力学知识库 > 问答 > 编程问答 >

java - find the length matrix and finding the word

问题描述:

Give a square matrix of alphabets within contain English Letters in arbitrary manner, while searching a word in it , you can go left to right horizontally, vertically downloads or diagonally towards right(Both Upwards and downwards.

For Example,In the given matrix

a a k

a s k

a k k

The word "ASK" is matched four times in the input matrix. so the output will be 4 output should be 4 I am getting 6;

public class Pattern1

{

public static void main(String[] args)

{

mat();

}

public static void mat()

{

int count = 0;

Scanner sc1 = new Scanner(System.in);

Scanner sc2 = new Scanner(System.in);

System.out.println("enter the no of rows");

int r = sc1.nextInt();

System.out.println("Enter the no of cols");

int c = sc1.nextInt();

String m1[][] = new String[r][c];

System.out.println("Enter the elements row wise");

for (int i = 0; i < r; i++)

{

for (int j = 0; j < c; j++)

{

m1[i][j] = sc2.nextLine();

}

}

System.out.println("Matrix is");

for (int i = 0; i < r; i++)

{

for (int j = 0; j < c; j++)

{

System.out.print(m1[i][j] + " ");

}

System.out.println();

}

String s = "ask";

int count1 = 0;

String s1[] = s.split("");

int hori = 0, verti = 0, digoL = 0, digoR = 0;

//horizontal

for (int i = 0; i < r; i++) {

count1 = 0;

for (int j = 0; j < c; j++)

{

for (int k = 0; k < s1.length; k++)

{

if (m1[i][j].equals(s1[k]))

count1++;

}

}

if (count1 == s1.length)

{

hori++;

}

}

//vertical

int count2 = 0;

for (int i = 0; i < r; i++) {

count2 = 0;

for (int j = 0; j < c; j++) {

for (int k = 0; k < s1.length; k++) {

if (m1[j][i].equals(s1[k]))

count2++;

}

}

if (count2 == s1.length)

{

verti++;

}

}

//dioganal right

int count3 = 0;

for (int i = 0; i < r; i++) {

count3 = 0;

for (int j = 0; j < i; j++) {

for (int k = 0; k < s1.length; k++) {

if (m1[i][j].equals(s1[k]))

count3++;

}

}

if (count3 == s1.length)

{

digoL++;

}

}

//dioganal left

int count4 = 0;

for (int i = 0; i < r; i++) {

count4 = 0;

for (int j = 0; j < i; j++) {

for (int k = 0; k < s1.length; k++) {

if (m1[i][j].equals(s1[k]))

count4++;

}

}

if (count4 == s1.length) {

digoR++;

}

}

int sum = verti + hori + digoL + digoR;

System.out.println("sum=" + sum);

}

}

网友答案:

Your comparisons are wrong. Let'S analyse the horizontal one as an example:

for (int k = 0; k < s1.length; k++)
{
    if (m1[i][j].equals(s1[k]))
        count1++;
    }
}

For each element in the matrix you are comparing it to all elements in the pattern, since you are not changing the matrix index with k, so for the first row you are doing:

  • a==a? a==s? a==k?
  • a==a? a==s? a==k?
  • k==a? k==s? k==k?

The only reason you get a result other than 0 is, that you also forgot to reset the counter count1 if there was no match.

So a fix for the horizontal case could look like this:

//horizontal
for (int i = 0; i < r; i++) {
    count1 = 0;
    columnloop:
    for (int j = 0; j < c; j++) {
        for (int k = 0; k < s1.length; k++) {
            if (j + k < m1[i].length && m1[i][j + k].equals(s1[k])) { // raise column index with k
                count1++;
            } else {
                count1 = 0; // reset counter
                continue columnloop;
            }
        }
        if (count1 == s1.length) {
            hori++;
        }
    }
}

The inner counter is not needed though and we can reduce the comparisons to

int hori = 0, verti = 0, digoUp = 0, digoDown = 0;

//horizontal
for (int i = 0; i < r; i++) {
    columnloop: for (int j = 0; j < c-s1.length+1; j++) {
        for (int k = 0; k < s1.length; k++) {
            if (!m1[i][j + k].equals(s1[k])) continue columnloop;
        }
        hori++;
    }
}

//vertical
for (int i = 0; i < c; i++) {
    rowloop:
    for (int j = 0; j < r-s1.length+1; j++) {
        for (int k = 0; k < s1.length; k++) {
            if ( ! m1[j+k][i].equals(s1[k])) continue rowloop;
        }
        verti++;
    }
}

//diogonal up
for (int i = s1.length-1; i < r; i++) {
    columnloop:
    for (int j = 0; j < c-s1.length+1; j++) {
        for (int k = 0; k < s1.length; k++) {
            if ( !m1[i-k][j+k].equals(s1[k]) ) continue columnloop;
        }
        digoUp++;
    }
}

//diogonal down
for (int i = 0; i < r-s1.length+1; i++) {
    columnloop:
    for (int j = 0; j < c-s1.length+1; j++) {
        for (int k = 0; k < s1.length; k++) {
            if (!m1[i+k][j+k].equals(s1[k])) continue columnloop;
        }
        digoDown++;
    }
}

int sum = verti + hori + digoUp + digoDown;
分享给朋友:
您可能感兴趣的文章:
随机阅读: