How do you test for diagonal in n-queens?

余生长醉 提交于 2021-02-07 06:48:27

问题


I'm studying the n-queen backtracker. Can someone explain to me how other_row_pos checks for diagonals? I'm not sure why it works or how it works.

taken from wikibooks - http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/N-Queens:

bool isSafe(int queen_number, int row_position) {
    // Check each queen before this one
    for(int i=0; i<queen_number; i++) {
        // Get another queen's row_position
        int other_row_pos = position[i];
        // Now check if they're in the same row or diagonals
        if(other_row_pos == row_position || // Same row
           other_row_pos == row_position - (queen_number-i) || // Same diagonal
           other_row_pos == row_position + (queen_number-i))   // Same diagonal
            return false;
    }
    return true;
}

回答1:


Let delta_row = the difference in rows between the two queens, and delta_col = the difference in columns. The two queens will be on the same diagonal if delta_row == delta_col or delta_row == -delta_col.

With the variables you have:

delta_row = other_row_pos - row_position
delta_col = queen_number - i

So the queens are on the same diagonal if:

other_row_pos - row_position == queen_number - i ||
other_row_pos - row_position == -(queen_number - i)

If you add row_position to both sides of the equality, you get the condition in your code:

other_row_pos == row_position + (queen_number-i) ||
other_row_pos == row_position - (queen_number-i)



回答2:


Consider you have to check if board element (x,y) can be attacked from any diagonal element or not. (x,y) can be attacked diagonally if any element lying on its diagonal element is a queen.Assume (p,q) is board element having a queen.Now condition for element(x,y) to lie on diagonal coordinates of board element (p,q) would be p+q == x+y or p-q == x-y .This can also be interpreted as the condition for elements (p,q) and (x,y) to lie on same diagonal.So, if there is queen at (p,q) and we have to check whether (x,y) can be attacked by this queen or not, the condition for this would be:-

            if((board[p][q] == 1 ) && ((p+q == x+y) || (p-q == x-y))){
                return true; 
            }

Complete function for checking if element at (x,y) i.e., board[x,y] is attacked by diagonal elements or not would be:-

for(int p=1;p<board.length;p++){
        for(int q=1;q<board.length;q++){

            if(p==x && q==y){   //skipping check if element under consideration is same
                continue;
            }

            if((board[p][q] == 1 )&& ((p+q == x+y) || (p-q == x-y))){
                return true;
            }
        }
    }

Complete function for checking if element (x,y) is attacked or not would be:-

    public static boolean is_attacked(int x,int y,int board[][],int n){
    for(int i = 1;i < board.length;i++){
        if(board[x][i] == 1){            //if any cell in xth row is 1 i.e.,queen is there in that row
            return true;
        }
    }
    for(int i = 1;i < board.length;i++){     
        if(board[i][y] == 1){         //if any cell in yth column is 1 i.e.,queen is there in that column
            return true;
        }
    }
    for(int p=1;p<board.length;p++){
        for(int q=1;q<board.length;q++){

            if(p==x && q==y){
                continue;
            }

            if((board[p][q]== 1 )&& ((p+q== x+y) || (p-q == x-y))){
                return true;
            }
        }
    }
    return false;
}

Hope this helps!!!



来源:https://stackoverflow.com/questions/19524155/how-do-you-test-for-diagonal-in-n-queens

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!