0

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.");
    }

}
dwurf
  • 12,393
  • 6
  • 30
  • 42
Abszol
  • 57
  • 3
  • 11
  • I didn't get exactly what you are trying to achieve. So please let me know if this is not what you want. Let me tell what I understood first. Now you are accessing elements in positions (0,0) (1,1) (2,2) etc. ie diagonal elements. And you want to access (0,2) (1,2) (2,0) positioned elements. ie the other diagonal. Is that you want? – Manu Viswam Mar 04 '14 at 04:13
  • I am getting index out of bound exception when running you program – Manu Viswam Mar 04 '14 at 04:16
  • Basically you got the idea of it, also the index is from the non adjustment of the integers since it goes from 3 to 2 across when it moves the column. All it is that I need is it to work from what originally did which was AEI then BF then C (Ignore the A, AE, AEI) to what I am trying now which is, CEG then BD then A – Abszol Mar 04 '14 at 04:28
  • There was a mistake in first comment I was supposed to say (0,2) (1,1) (2,0) – Manu Viswam Mar 04 '14 at 04:48

2 Answers2

3

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++;
            }
        }
    }    
}
Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
Avinash
  • 31
  • 2
2

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.

Manu Viswam
  • 1,606
  • 2
  • 18
  • 33
  • Hey, here's a better example of the actual diagonal method from Center to Right. http://pastebin.com/p7M7mZBp Wasn't too sure that I understood what you posted but you seem to have the right idea. Anyways thanks for helping thus far. – Abszol Mar 04 '14 at 05:05