Question

Title says it all, I developed this diagonal method that searches the Matrix 'matrix' and goes from the Center to the far right corner. I also have it another set from the center to the left. I now have the question, how would I make it reversed, not starting from the bottom but actually starting "C", go all the way to "G" and keep moving towards the Left.

All it has to do is be reversed but I have tried and tried for about 2 hours and still to no avail. This is actually the final piece to my project I have going on and would awesome if someone could help flip.

Here's the code, I took out a large portion to conserve space.

public class Word {

    public static int curCol = 10;
    public static int curRow = 10;

    public static String[][] matrix = {{"A","B","C"},
                                       {"D","E","F"},
                                       {"G","H","I"}};

    private static void searchDiagonalCenterToRight(String word) {//Center to bottom Righ    t. Diagnol Works, debug to go along column is needed

        int rowOn = 0;
        int colOn = 0;

        int resetableCol = curCol;
        resetableCol--;//Just resets everything then starts again.

        int decreaser = curCol;//What to decrease by everytime it runs 10,9,8,7 all the way to 1
        int resetableDec = decreaser;
        resetableDec--;

        char c;

        String toFind = word.toUpperCase();

        String developingInverse = "";

        int integer = 0;

        for(int row = 0; row < curRow; row++)//Matrices Row
        {
            for(int i = 0; i <= resetableDec; i++)
            {

                String developingWord = "";

                integer = i;

                for(int j = integer; j <= resetableDec; j++,integer++)
                {              
                    c = matrix[j][integer+row].charAt(0);//Sets to whatever letter it is on
                    char uC = Character.toUpperCase(c);
                    developingWord = developingWord + "" +uC;

                    System.out.println("On Row: " + row + " Started From: " + integer + " Now On: " + j);
                    System.out.println("Processing Letter: " + matrix[j][integer] + " Adding Letter To: " + developingWord);

                }

            }
            resetableDec--;
        }
        System.out.println("\nNo Matching Word Was Found, Center To Left.");
    }

}

No correct solution

OTHER TIPS

Here is the code

public class ReverseDiagonalMatrix {

    public static void main(String[] args) {
        int [][] a = {{ 1, 2, 3, 4},
                      { 5, 6, 7, 8},
                      { 9,10,11,12},
                      {13,14,15,16}};
        int a1[][]= {{1,2,3},
                     {4,5,6},
                     {7,8,9}};
        int a2[][]= {{1,2},
                     {3,4}};
        int [][] a3 = {{ 1, 2, 3, 4, 5},
                       { 6, 7, 8, 9,10},
                       {11,12,13,14,15},
                       {16,17,18,19,20},
                       {21,22,23,24,25}};
        System.out.println("==========5x5==============");
        findReverseDiagonalOrder(a3);
        System.out.println("==========4x4==============");
        findReverseDiagonalOrder(a);
        System.out.println("===========3x3=============");
        findReverseDiagonalOrder(a1);
        System.out.println("===========2x2=============");
        findReverseDiagonalOrder(a2);
    }

    public static void findReverseDiagonalOrder(int[][] a) {
        int m = a.length;

        int row=0;
        int col = m-1;

        for(int i=0;i<m*m;i++) {
            System.out.println(a[row][col]);
            if(col==m-1) {
                if(row==0)
                    col--;
                else {
                    col= (row==col)? 0:col-(row+1);
                    row= (row==m-1)? 1:0;
                }
            }
            else if(row==m-1) {
                if(col-1==0 && col>0)
                col--;
                else {
                    row = m-col;
                    col=0;
                }

            }
            else {
                row++;
                col++;
            }
        }
    }    
}

To access the other diagonal just use the following loop

for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
               if(i==(n-j-1)){
                     //Do something
               }
       }
 }

By using this logic you will get CEG

Add the same logic to construct other strings too.

EDIT:-

By changing loop like this

for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i<=(n-j-1)){
                System.out.println("("+i+","+j+")");
            }
        }
    }

You can access elements (0,0) (0,1) (0,2) (1,0) (1,1) (2,0). I hope that is what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top