2013-02-15 3 views
1

나는 약간의 비틀어서 N*N 퀸 알고리즘을 구현하려고합니다. 이 버전에서 여왕은 기사처럼 움직일 수 있습니다 ...좌표를 얻는 N * N 여왕 알고리즘

모든 것이 잘 작동하지만 모든 가능한 해결책의 좌표를 얻으려고합니다. 문제는 내가 그것을 col == n 안에 넣으면 단지 마지막 것을 인쇄한다는 것입니다. 이 문제를 해결하는 방법에 대한 아이디어가 있습니까?

static void placement(int col, int queens[], int n){ 
    //int solution =0; 
    for (int row = 1; row <= n; row++) { 
     queens[col] = row; 
     if((check_Queen(row,col,queens)) == true) 
     { 
     if((check_KnightMove(row,col,queens)) == true) 
     { 
      if(col == n) 
      { 
      System.out.println("("+row + "," + col); 
      System.out.println("solution=" + solution); 
      solution++; 
      } 
      else 
      { 
      placement(col+1,queens,n); 
      } 
     } 
     } 
    } 
    queens[col] = 0; 
    } 

    public static void main(String[] args) { 
    int solution =0; 
    Scanner scanner=new Scanner(System.in); 
    System.out.print("Please enter N"); 
    int n = scanner.nextInt();// TODO Auto-generated method stub 
    int queens[] = new int[n+1]; 
    placement(1,queens,n); 
    System.out.println("nQueens: solution=" + solution); 
    } 
     static boolean check_Queen(int row, int col, int queens[]) 
{ 

    //boolean flag = false; 
    for(int i =1; i<col; i++) 
    { 
     if (queens[col-i] == row || 
       queens[col-i] == row-i || 
       queens[col-i] == row+i) { 
       //flag = false; 
       return false; 
      } 

    } 
    return true; 


} 
      static boolean check_KnightMove(int row, int col, int queens[]) 
      { 
    if(col>=2&&(queens[col-2] == (row -1) || queens[col-2] == (row+1) || queens[col-1] == (row-2) || queens[col-1] == (row+2))) 
    { 
     return false; 
    } 
    return true; 

} 


} 
+0

어쩌면 마지막 n 개의 솔루션을 저장하는 배열입니까? – user1665569

답변

0

check_Queencheck_KnightMove 정의하는 방법을 모르고 솔루션에 어떤 문제가 있는지 알 어렵다. 여기에 내가 작업 해결할 방법입니다 솔루션에서

public class Queens 
{ 
    static void printSolution (int [] queens) 
    { 
     int l = queens.length; 
     for (int i = 0; i < l; i++) 
     { 
      for (int j = 0; j < l; j++) 
      { 
       System.out.print (queens [i] == j ? 'Q' : '.'); 
       System.out.print (' '); 
      } 
      System.out.println(); 
     } 
     System.out.println(); 
    } 

    static int placement (int [] queens, int c) 
    { 
     if (c == queens.length) 
     { 
      printSolution (queens); 
      return 1; 
     } 
     else 
     { 
      int solutionCount = 0; 
      int l = queens.length; 

      for (int r = 0; r < l; r++) 
      { 
       boolean flag = false; 
       for (int i = 0; i < c; i++) 
       { 
        int xd = c - i; 
        int yd = Math.abs (r - queens [i]); 

        if (yd == 0 || xd == yd) 
        { 
         flag = true; 
         break; 
        } 

        // Knight move support 
        if ((xd == 1 && yd == 2) || (xd == 2 && yd == 1)) 
        { 
         flag = true; 
         break; 
        } 
       } 

       if (!flag) 
       { 
        queens [c] = r; 
        solutionCount += placement (queens, c + 1); 
       } 
      } 

      return solutionCount; 
     } 
    } 

    public static void main (String [] args) 
    { 
     System.out.println (
      "Total solutions found: " + placement (new int [11], 0)); 
    } 
} 

를, 방법 check_KnightMove이 올바르지 않습니다. col == 2 일 때, 퀸이 항상 행, 즉 보드 외부에 있다고 믿기 때문에 퀸이 1 열에 배치되는 것을 허용하지 않습니다. 수정 된 버전은 다음과 같습니다.

static boolean check_KnightMove (int row, int col, int queens[]) 
{ 
    if (col >= 3 
      && (queens [col - 2] == (row - 1) || queens [col - 2] == (row + 1))) 
    { 
     return false; 
    } 

    if (col >= 2 
      && (queens [col - 1] == (row - 2) || queens [col - 1] == (row + 2))) 
    { 
     return false; 
    } 
    return true; 
} 
+0

감사합니다. @Mikhail. 내 수표 여왕을 추가하고 나이트 수표를 추가했습니다 ... 누군가가 그것을보고 무엇이 잘못되었는지 말해 주면 ... 그리고 마지막으로 n-1 올바른 좌표 만 얻습니다. – user1665569

+0

'check_KnightMove'가 틀립니다 : 'col == 2 '일 때는 col = 0, row = 0에 여왕이 있다고 믿기 때문에 여왕이 1 열에 놓이는 것을 허용하지 않습니다. 내 답변에 최근 추가 된 내용을 확인하십시오. –

+0

고맙습니다. 나는 문제가 생길 때 문제의 오른쪽 좌표를 얻으려고 할 때 "col == n"이전의 마지막 (n-1) 옳음을 알 수있다. – user1665569

관련 문제