ñ* N皇后算法得到的坐标 [英] N * N queen algorithm getting the coordinates

查看:169
本文介绍了ñ* N皇后算法得到的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实施 N *ñ皇后算法有一点扭曲它。在这个版本中的女王还可以将喜欢的骑士......

一切工作正常,但我试图让所有可能的解决方案的坐标。问题是,如果我把它放在山坳==ñ它只是打印最后一个。如何解决这个任何想法?

 静态无效的位置(INT山坳,诠释女王[],INT N){
    // INT溶液= 0;
    对于(INT行= 1;行< = N;排++){
      皇后[COL] =行;
      如果((check_Queen(行,列,皇后))==真)
      {
        如果((check_KnightMove(行,列,皇后))==真)
        {
          如果(COL == N)
          {
            的System.out.println((+行+,+列);
            的System.out.println(溶液=+溶液);
            解决方案++;
          }
          其他
          {
            安置(COL + 1,王后,N);
          }
        }
      }
    }
    皇后[COL] = 0;
  }

  公共静态无效的主要(字串[] args){
    INT溶液= 0;
    扫描仪扫描=新的扫描仪(System.in);
    System.out.print(请输入N);
    INT N = scanner.nextInt(); // TODO自动生成方法存根
    INT皇后[] =新的INT [N + 1];
    安置(1,王后,N);
    的System.out.println(nQueens:液=+解决方案);
  }
      静态布尔check_Queen(INT行,诠释山坳,诠释女王[])
{

    //布尔标志= FALSE;
    的for(int i = 1; I<西;我++)
    {
        如果(皇后[COL-I] ==排||
                 皇后[COL-I] ==行我||
                 皇后[COL-I] ==行+ I){
                //标志= FALSE;
                返回false;
             }

    }
    返回true;


}
           静态布尔check_KnightMove(INT行,诠释山坳,诠释女王[])
           {
    如果(COL> = 2及及(皇后[COL-2] ==(行-1)||皇后[COL-2] ==(行+ 1)||皇后[COL-1] ==(行-2)||皇后[COL-1] ==(行+ 2)))
    {
        返回false;
    }
    返回true;

}


}
 

解决方案

这是很难说什么是错的解决方案中的不知道如何 check_Queen check_KnightMove 定义。下面是我会怎么解决的任务:

 公共类皇后
{
    静态无效printSolution(INT []皇后)
    {
        INT L = queens.length;
        的for(int i = 0; I<升;我++)
        {
            对于(INT J = 0; J< L,J ++)
            {
                System.out.print(皇后[我] == J'Q':'。');
                System.out.print('');
            }
            的System.out.println();
        }
        的System.out.println();
    }

    静态INT位置(INT []王后,INT C)
    {
        如果(C == queens.length)
        {
            printSolution(皇后);
            返回1;
        }
        其他
        {
            INT solutionCount = 0;
            INT L = queens.length;

            对于(INT R = 0;为r,L,R ++)
            {
                布尔标志= FALSE;
                的for(int i = 0;我c为C;我++)
                {
                    INT XD = C  - 我;
                    INT YD = Math.abs(R  - 皇后[I]);

                    如果(YD == 0 || XD ==码)
                    {
                        标志=真正的;
                        打破;
                    }

                    //骑士举措支持
                    如果((XD == 1&安培;&安培;码== 2)||(XD == 2&安培;&安培;码== 1))
                    {
                        标志=真正的;
                        打破;
                    }
                }

                如果(!标志)
                {
                    皇后[C] = R;
                    solutionCount + =安置区(Queens,C + 1);
                }
            }

            返回solutionCount;
        }
    }

    公共静态无效的主要(字串[] args)
    {
        的System.out.println(
            一共找到解决方案:+就业(新INT [11],0));
    }
}
 

在您的解决方案,方法 check_KnightMove 不正确。当山坳== 2 它不允许皇后被放置在第1行,因为它认为总有一个女王在(西:0 ,排:0),即外板。下面是修改后的版本:

 静态布尔check_KnightMove(INT行,诠释山坳,诠释女王[])
{
    如果(COL> = 3
            &功放;&安培; (皇后[COL  -  2] ==(行 -  1)||皇后[COL  -  2] ==(行+ 1)))
    {
        返回false;
    }

    如果(COL> = 2
            &功放;&安培; (皇后[COL  -  1] ==(行 -  2)||皇后[COL  -  1] ==(行+ 2)))
    {
        返回false;
    }
    返回true;
}
 

I'm trying to implement the N*N queen algorithm with a little twist to it. In this version the queen can also move like a knight...

Everything is working fine, but I'm trying to get the coordinates of all the possible solutions. The problem is that if I put it inside col == n it just prints the last one. Any ideas of how to solve this?

  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;

}


}

解决方案

It is hard to tell what is wrong in your solution without knowing how check_Queen and check_KnightMove are defined. Here is how I would solve the task:

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

In your solution, method check_KnightMove is incorrect. When col == 2 it does not allow Queen to be placed at row 1, because it believes that there is always a Queen at (col: 0, row: 0), i.e. outside the board. Here is corrected version:

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

这篇关于ñ* N皇后算法得到的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆