2

I don't know whether this problem has been studied or not, it just came to my mind while trying out the general N-Queens problem. Given a N*N chessboard , what is the minimum number of Queens required, which, when placed strategically, renders all cells under attack by at least one of the queens.

I tried it with pen and paper for N = 3,4,5, I got 2,3,4. So is the answer always N-1? Is there a proof for it? And secondly, if so, how to print out that configuration (if more than 1 configuration is possible, print them all)?

Frank
  • 16,476
  • 7
  • 38
  • 51
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
  • Huh? On a 3*3 board, there's only 1 queen needed (placed in the middle). Did you mean N-2 queens, perhaps? – Bart Kiers Nov 01 '12 at 07:30
  • Oh right, indeed 1 is required. So how to find out for a given number of queens? – SexyBeast Nov 01 '12 at 07:32
  • Yeah, for 4 it looks like 2, not 3. So will the answer always be `N-2`? – SexyBeast Nov 01 '12 at 07:33
  • 2
    From the comment discussion I assume a queen can guard itself. If not - 3x3 board does need 2 queens. – amit Nov 01 '12 at 08:29
  • No, the answer will not always be `n-2`. A queen can cover up to `4(n-1)` squares if placed in the middle, so an lower limit for queens is `n^2/(4n-1)` which is `n/4` for large n. If the queens are placed on the rim, the lower limit is `n/3`. Why don't you write a program to find out the minimum for distinct values? :-) – Gunther Piez Nov 01 '12 at 09:29
  • @hirschhornsalz: Your lower bound of n/4 is just that -- a lower bound. There's no reason to assume it is tight. – j_random_hacker Nov 01 '12 at 09:36
  • @j_random_hacker No of course not. Replace "not alaways" by "most likely not". A "I found out by pen and paper it is 2,3,4 (and did it even wrong)" is surely no better estimate. – Gunther Piez Nov 01 '12 at 09:41
  • @hirschhornsalz: The OP presented a guess as a guess. You presented a mathematical argument as proof when it was not. – j_random_hacker Nov 01 '12 at 10:07
  • @j_random_hacker Yes. Unfortunately I can't edit my comment any longer, so I leave it there to always remember me of my my hybris. Hopefully the public humiliation will teach me something ;-) – Gunther Piez Nov 01 '12 at 10:17
  • @hirschhornsalz: Fair enough :-P – j_random_hacker Nov 01 '12 at 10:21

2 Answers2

3

The problem has been studied and the minimum number at which k queens cover an nxn grid is known as the domination number.

The k for the first n are

1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 5, 6, 7, 8, 9, 9, 9, 9

as given by OEIS. This means for an 8x8 board 5 queens are sufficient.

It has been conjectured that for all n which satisfy n=4m+1 (such as 5,9,13...) 2m+1 queens are sufficient. This and lot more advanced algorithms are presented in Matthew D. Kearse and Peter B. Gibbons, "Computational Methods and New Results for Chessboard Problems"

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • How to do code it so that it returns the result for any given `n`? Any algorithm? – SexyBeast Nov 01 '12 at 09:55
  • @Cupidvogel A naive approach: Start with k=1. Iterate `k` queens over a `nxn` board. Test in each iteration if all squares are covered. If you found at least one position where all squares are covered, you are done. Otherwise increase k and repeat. – Gunther Piez Nov 01 '12 at 10:02
  • Yeah, of course I can do that. But if I give that answer in an interview, they will kick me out immediately! What about an optimal solution? – SexyBeast Nov 01 '12 at 11:10
  • There is no "optimal" algorithm for solving this problem. Study the paper which I linked, there are some "good" algorithms mentioned. But I suspect if you give one of them as an answer in an interview, they will kick you out even faster. The algorithm I gave will always give you correct minimum, why do you think its not a good answer? – Gunther Piez Nov 01 '12 at 12:34
  • How can someone kick me if I give an optimum solution as an answer? The one you mentioned is essentially brute force, right? – SexyBeast Nov 01 '12 at 12:42
  • 2
    @Cupidvogel I would kick you because you propose a solution with a difficult, hard to implement algorithm which you need weeks to get it running, when a easy solution is available which can be done in a day. No I wouldn't, just kidding :-) All known algorithms are brute force. So what? Do really think an interviewer expect you to solve a problem where no non brute force algorithm is known with a non exponential solution? The question more interesting to the interviewer is: Are you able to find a working algorithm at all and implement it? Are you? – Gunther Piez Nov 01 '12 at 14:12
2

Well, it's not N-2, because an 11x11 grid requires at most 8 queens (and possibly fewer -- this is just an example I found by hand):

11x11 grid covered by 8 queens

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169