2

I have a problem at hand which is a variation of the N-Queen-Problem.The problem goes: Find a way to place 4 queens and 1 knight on a 8*8 chessboard, so that all blocks can be attacked by these pieces.It is OK for the pieces to attack each other.

I understand how to solve the original N-Queen-Problem using backtracking. However, I could not come up with an idea on how to reduce the number of searches in this new problem here. I hope someone could give me a hint.Thank you.


Thank you people.@vish4071 @Karoly Horvath @M Oehm I will consider using brute force and see what I get.

antande
  • 169
  • 1
  • 13
  • 1
    "I could not come up with an idea on how to reduce the number of searches in this new problem here" - please clarify. I smell *censored*. Post your backtracking algorithm that was too brute-force and needs "reduction". – Karoly Horvath Nov 16 '15 at 06:51
  • @KarolyHorvath I meant, if there is a rule on that the pieces can not attack each other, at least you can assume that no two queens can be in the same column, and take that as the starting point as your backtracking program, thus reducing the possible solutions from all possible combinations of N positions to a much smaller number. However in my case there is no such restriction to help,therefore I wonder if there is a better way than searching all possible position combinations. – antande Nov 16 '15 at 07:00
  • OTOH, you have only 5 pieces, not 8. And for both problems it's enough to check one of the quarters for the first piece. But seriously, just write your code.... – Karoly Horvath Nov 16 '15 at 07:04

2 Answers2

3

Use Brute Force. It should give you answer in short time.

You have 64 squares to consider and 5 pieces to put. Simply select 5 squares and put 5 pieces and see if this scenario covers all squares. This can be done in C(64,5) * 5 ways, which is ~3.8e7, which is easily computable in well under 1s on a standard machine now a days.

Also, you can reduce some computation if you put 4 queens in select 4 of 64 squares and then place the knight to cover the remaining squares only. This will take around C(64,4) * k computations, which is ~1e6.

vish4071
  • 5,135
  • 4
  • 35
  • 65
  • I would argue that this can be even better improved by trying these two ideas: 1. Place the knight so it attacks a non-attacked square. No need in trying to place it where it doesn't add to the attacked squares. 2. Though I wouldn't dismiss a solution where there are two queens in the same row/column, I'd start my code to try first the options where each queen has its own row/column. – shapiro yaacov Nov 16 '15 at 08:40
  • @shapiroyaacov, The second method may not be used, as though it seems intuitive that we should place queens on different row/col, one of the solutions as given by MOehm is Q(c2,c5,c8,f1), N(f5) is where 3 queens stand on same column. – vish4071 Nov 16 '15 at 08:52
  • That is why I wrote that I would try *first* the options with different rows/columns. If no solution was found, then try the other options... – shapiro yaacov Nov 16 '15 at 09:11
0

I don't think a naive algorithm needs any kind of optimization as it's probably going to be fast anyways... but, here it is:

You can always restrict the first move to one of the quarters (e.g.: upper left), since the rest of the solutions can be generated from that by mirroring or rotating.

You can count the covered places, and there's a limit how much one piece can cover (22 for a queen and 9 for a king), so you can use that as a rule for early terminating a hopeless branch.

Note: since you have only 4 queens, putting 2 in the same row or column is probably a bad idea. You can try that as a restriction.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • Yes restricting the first move is probably a good idea. However,you can only find out whether your pieces' attacking range covers the whole board after you have placed all of them,right? Therefore I don't see any room for using backtracking. You'll simply have to try all possible layouts(restricting your first move, of course.) to find out,or is there an efficient way? – antande Nov 16 '15 at 07:24
  • See my update. In any way, *please* test your algorithm before thinking about improving it. How slow is the brute-force approach? – Karoly Horvath Nov 16 '15 at 07:28
  • I've got a brute-force solution that doesn't exploit symmetry and I get many solutions where the queens share a rank or file. For example, you can put the queens in C2, C5, C8 and F1 and the knight in F5. – M Oehm Nov 16 '15 at 07:37