【问题标题】:R: N-Queens finding the DiagonalR: N-Queens 找到对角线
【发布时间】:2018-04-23 03:52:53
【问题描述】:

今天早些时候发表了一篇文章,表示我试图找到 N-Queens 问题的解决方案。第一部分是确定以下函数:

>safe(chess.piece,chess.board)

地点:

>chess.board <- matrix(0,8,8)
>chess.board[r,c] <- 1 #1 represents the queen which can be placed in any row/column on the board
>chess.piece <- c(x,x) #basically and row/column value I choose in the 8x8 matrix

Currentley 我有以下代码用于水平和垂直平面:

>safe <- function(a,b){
  if((sum(b[a[1],])<1) & (sum(b[,a[2]])<1))
  {return(TRUE)
  }else{
    return(FALSE)
  }
}

基本上用于对行/列求和并检查是否大于 (FALSE) 或等于 (TRUE) 为 0。但是我到底如何根据确定的行/列找到 chess.board 矩阵的对角线和通过棋子?!我正在为此扯头发,因为我对 R 很陌生,但我需要一个解决方案,但似乎无法弄清楚。 任何帮助将不胜感激!提前致谢,J.S

【问题讨论】:

    标签: r chess n-queens chessboard.js


    【解决方案1】:

    考虑你必须检查棋盘元素 (x,y) 是否可以从任何对角元素攻击。如果位于其对角元素上的任何元素是皇后,则可以对角攻击 (x,y)。假设 (p,q) 是具有皇后的棋盘元素。现在条件元素 (x,y) 位于棋盘的对角坐标上元素 (p,q) 将是 p+q == x+y 或 pq == xy 。这也可以解释为元素 (p,q) 和 (x,y) 位于同一对角线上的条件。所以, 如果 (p,q) 有女王,我们必须检查 (x,y) 是否可以被这个女王攻击,条件是:-

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

    检查 (x,y) 处的元素,即 board[x,y] 是否被对角元素攻击的完整函数是:-

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

    检查元素(x,y)是否受到攻击的完整功能是:-

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

    希望对你有帮助!!!

    【讨论】:

      【解决方案2】:

      一种方法是编写几个函数来从方阵中提取部分对角线,使用的事实是您可以使用一维索引,因为矩阵只是底层的向量,并且这些对角线对应于某些算术索引序列:

      udiag <- function(A,i,j){
        n <- nrow(A)
        a <- i + n*(j-1) - (n-1)*min(n-i,j-1)
        b <- i + n*(j-1) + (n-1)*min(i-1,n-j)
        A[seq(a,b,n-1)]
      }
      
      ddiag <- function(A,i,j){
        n <- nrow(A)
        a <- i + n*(j-1) - (n+1)*min(i-1,j-1)
        b <- i + n*(j-1) + (n+1)*min(n-i,n-j)
        A[seq(a,b,n+1)]
      }
      

      这两个函数分别提取向上倾斜和向下倾斜的对角线。你可以使用这两个函数并像这样写你的safe

      safe <- function(x,y){
        i <- x[1]
        j <- x[2]
        sum(y[i,]) == 0 & sum(y[,j]) == 0 & sum(udiag(y,i,j)) == 0 & sum(ddiag(y,i,j)) == 0
      }
      

      编辑时:考虑到预期的应用,我写了udiag()ddiag() 来只处理方阵。由于它们在潜在的非方阵中可能还有其他用途,因此可以轻松修改它们以处理此类情况:

      udiag <- function(A,i,j){
        m <- nrow(A)
        n <- ncol(A)
        a <- i + m*(j-1) - (m-1)*min(m-i,j-1)
        b <- i + m*(j-1) + (m-1)*min(i-1,n-j)
        A[seq(a,b,m-1)]
      }
      
      ddiag <- function(A,i,j){
        m <- nrow(A)
        n <- ncol(A)
        a <- i + m*(j-1) - (m+1)*min(i-1,j-1)
        b <- i + m*(j-1) + (m+1)*min(m-i,n-j)
        A[seq(a,b,m+1)]
      }
      

      例如:

      > A <- matrix(1:12,nrow = 3)
      > A
           [,1] [,2] [,3] [,4]
      [1,]    1    4    7   10
      [2,]    2    5    8   11
      [3,]    3    6    9   12
      > udiag(A,2,3)
      [1]  6  8 10
      > ddiag(A,2,3)
      [1]  4  8 12
      

      【讨论】:

      • 你的救生员约翰。做得好!想想我对此感到非常沮丧,我从未真正考虑过开发其他功能来帮助我解决问题!再次感谢!
      猜你喜欢
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多