Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class alaxon {
- public static boolean calc_slash_diagonal(int[][] sqr) { // calc by right and down
- // # right through the first row
- int i=0;
- int ret = calc_slash_diagonal(sqr, 0, i, 0);
- i++;
- while (i <= sqr.length-1) {
- if (ret == calc_slash_diagonal(sqr, 0, i, 0))
- i++;
- else
- return false;
- }
- // # down through the last column from the second line
- int j=1;
- i = sqr.length-1;
- while (j <= sqr.length-1) {
- if (ret == calc_slash_diagonal(sqr, j, i, 0))
- j++;
- else
- return false;
- }
- return true;
- }
- public static int calc_slash_diagonal(int[][] sqr, int x, int y, int sum) {
- if ((x > sqr.length-1) || (x < 0) || (y > sqr.length-1 || (y < 0))) { // end of diagonal
- return sum;
- }
- return calc_slash_diagonal(sqr, x+1, y-1, sum+sqr[x][y]);
- }
- public static void main(String[] args) {
- int[][] sqr = { {8, -3, 3, 1, 0},
- {11, 4, 3, 2, 8},
- {1, 6, 3, 0, 8},
- {-2, 2, 0, 7, 3},
- {1, 0, -7, 5, 8} };
- int[][] sqr2 = { {8, -3, 3, 1, 0},
- {11, 4, 3, 2, 8},
- {1, 6, 3, 0, 8},
- {-2, 2, 0, 7, 3},
- {1, 0, -7, 5, 9} };
- int[][] sqr3 = { {8, 8, 8, 8, 8},
- {0, 0, 0, 0, 8},
- {0, 0, 0, 0, 8},
- {0, 0, 0, 0, 7},
- {0, 0, 0, 1, 8} };
- System.out.println(calc_slash_diagonal(sqr));
- System.out.println(calc_slash_diagonal(sqr2));
- System.out.println(calc_slash_diagonal(sqr3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement