6
1
Today marks the 15th anniversary of Garry Kasparov's defeat against Deep Blue. Speaking of chess...
Input is a string that represents an 8x8 chess board containing only empty squares (.
) and queens (Q
). Output the number of squares that any queen can move to the next move. Queens can move any number of squares vertically, horizontally or diagonally.
Write either a program or a function. Shortest code wins, but most inventive solution gets the most upvotes.
Examples
Input:
Q.......
........
.Q......
........
........
........
........
........
Output:
37
Input:
QQQQQQQQ
QQQQQQQQ
QQQQQQQQ
QQQQ.QQQ
QQQQQQQQ
QQQQQQQQ
QQQQQQQQ
QQQQQQQQ
Output:
1
1@Tim the correct output for the first test case is 36. I think you forgot to count the down-right diagonal from the second queen. – Peter Taylor – 2011-02-10T22:53:51.110
@Peter You're right, I missed that diagonal. That's what I get for trying to do a computer's job. – None – 2011-02-10T23:06:20.280
1I get 37 for the first test case – gnibbler – 2011-02-10T23:09:54.860
@gnibbler The upper-left queen reaches 7 squares in each of 3 directions. Since we don't count overlaps, the other queen reaches 5 squares in each of 3 directions. (37+35=36) – None – 2011-02-10T23:14:17.770
@Tim: you are forgetting about c7 (for a total of 37) – BlueRaja - Danny Pflughoeft – 2011-02-10T23:28:48.063
@BRDP: You're right! I'll stop trying to count them and get on with my solution now. @gnibbler You're right too :) – None – 2011-02-10T23:30:31.627
I thought this question would attract more answers – gnibbler – 2011-02-12T07:00:47.013
This is a duplicate of http://codegolf.com/dancing-queens. I guess it is okay to repost golf questions from other sources, but giving credit would seem appropriate.
– hallvabo – 2011-03-31T19:55:06.960@hallvabo: First, the problems are different. The answer to my second example would be 8 in the problem you linked. Second, it would be entirely reasonable that the same problem was thought of by two different sources. – None – 2011-04-04T10:46:23.980
@Tim: it's possible. Both challenges could also have a common ancestry (say, a textbook task).
And the difference is trivial, my solution to Dancing Queens is almost identical to gnibbler's 125 byte solution here. Anyway, people who enjoyed this challenge might like to try Dancing Queen too :-) – hallvabo – 2011-04-04T11:26:46.253