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!!!