开发者

Finding the Diagonal Containing a Specific Cell in a Grid

开发者 https://www.devze.com 2023-04-13 02:24 出处:网络
Lets say we have a grid containing cells, and that we want to return two arrays where each contains all the cells in the diagonal with that cell, up-left to bottom-right and the opposite one. What wou

Lets say we have a grid containing cells, and that we want to return two arrays where each contains all the cells in the diagonal with that cell, up-left to bottom-right and the opposite one. What would be the best way to go about it?

I tried the following in javascript (note that board is a one dimensional array that supposedly represents a square grid with a side length of boardSize. position is the cell which I am trying to find its diagonals.)

    var diagonal1 = [];
    var diagonal2 = [];
    for(var i = 0; i < board.length; i++) { 
        if (i == position) 开发者_JAVA技巧diagonal.move_index = diagonal.length
        if (Math.abs(position - i) % (boardSize + 1) == 0) {
            diagonal1.push(board[i]);
        }
        else if (Math.abs(position - i) % (boardSize - 1) == 0) { 
            diagonal2.push(board[i]);
        }
    }

but this is only doing the job for elements that lie in the main diagonal, not others. Any ideas?

Example:

if board = [1,2,3,4,5,6,7,8]
board:
        1 2 3 
        4 5 6 
        7 8 9 
then If I say that I want to find the diagonals for position = 4 
I should get:
diagonal1 = [4, 8] 
diagonal2 = [2, 4] 

and if I choose another position, lets say position = 5, then:
diagonal1 = [1, 3, 5] 
diagonal2 = [3, 5, 7]


Code yourself :

Idea :

1) Implement the board as a two dimensional array.

(Example of an array a[3][3],given in terms of array index)

00 01 02 03
10 11 12 13
20 21 22 23
30 31 32 33

2) Find the position of element in the array. say for example is at position a[1][1], when we find this in program, we get it in terms of [i][j] where i=1, j=1 .

3)To get diagonal1, get the values at positions a[i-1][j-1] until i=0 and j=0. AND values at positions a[i+1][j+1] until you reach array bounds,in this case 3.

4)To get diagonal2 get the values at positions a[i-1][j+1] until you reach array bounds AND values at positions a[i+1][j-1] until you reach array bounds.


Does the order of the cells in the diagonals matter? If not, the algorithm in this pseudocode should suffice. If it does matter, you just need to reorder some chunks and play around with the loop variables...

getDiagonals(x, y) {

    Set<Cell> diagonal1;
    Set<Cell> diagonal2;

    diagonal1.add(board[x][y]); //manually add center cell to both diagonals
    diagonal2.add(board[x][y]);

    //Sweep forwards, adding higher cells to diag1 and lower cells to diag2
    for(int i=1; x+i < board.size(); i++) { //board.size() for 'horizontal' size
        if(y-i >= 0) diagonal1.add(board[x+i][y-i]);
        if(y+i < board[0].size()) diagonal2.add(board[x+i][y+i]); //board[0].size for 'vertical' size
    }

    //Sweep backwards, adding higher cells to diag2 and lower cells to diag1
    for(int i=1; x-i >= 0; i++) {
        if(y-i >= 0) diagonal2.add(board[x-i][y-i]);
        if(y+i < board[0].size()) diagonal1.add(board[x-i][y+i]);
    }

    return (diagonal1, diagonal2);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号