在矩阵中反转对角线

时间:2014-03-04 02:29:50

标签: java loops matrix inverse

标题说明了一切,我开发了这种对角线方法来搜索Matrix'矩阵'从中心到最右角。我还有从中心到左边的另一套。我现在有了一个问题,如何让它颠倒过来,不是从底部开始,而是实际开始" C",一直走到" G"并继续向左移动。

所有它要做的就是逆转,但我已经尝试了大约2个小时但仍无济于事。这实际上是我正在进行的项目的最后一块,如果有人可以帮忙翻转,那就太棒了。

这里是代码,我拿出了很大一部分来节省空间。

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

}

2 个答案:

答案 0 :(得分:1)

要访问另一个对角线,只需使用以下循环

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

通过使用此逻辑,您将获得CEG

添加相同的逻辑来构造其他字符串。

编辑: -

通过改变这样的循环

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

您可以访问元素(0,0)(0,1)(0,2)(1,0)(1,1)(2,0)。我希望这就是你想要的。

答案 1 :(得分:0)

这是代码

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++;
            }
        }
    }    
}