130 likes | 542 Views
d036: Maximum Empress Problem. Po-Lung Chen Team Dont Block Me, National Taiwan University March 26, 2010. N -Queen Problem. How many ways to place Queens on an chessboard so that no Queens attack others? A Queen can attack in 8-directions on the board.
E N D
d036: Maximum Empress Problem Po-Lung Chen Team Dont Block Me, National Taiwan University March 26, 2010
N-Queen Problem • How many ways to place Queens on an chessboard so that no Queens attackothers? • A Queen can attack in 8-directions on the board.
Problem Description (1/2) • An Empress split herself into four Queens at rotation positions.
Problem Description (2/2) • Let be the maximum number of Empress that can be placed on a chessboard such that none of the Queens split by Empresses can attack. • How many ways to place Empresses on a chessboard “peacefully”?
Observation • If is odd, then we must place an Empress at center. • For any solution, if we split the Empresses into Queens, then rotate the chessboard by 90, 180 or 270 degrees remain the same status on the chessboard. • Consider the Upper-Left region only. • Run a “Depth First Search” on this region.
Simple DFS structure • DFS(row, Now_Empress, Board_Status) • If(row==[n/2]) • If(Now_Empress > Max_Empresses) • Max_Empresses = Now_Empress; • AnswerCount = 0; • If(Now_Empress == Max_Empresses) • AnswerCount++; • Else • DFS(row+1, Now_Empress, Board_Status) • Find a safe place on this row, and modify Board_Status. • DFS(row+1, Now_Empress+1, Modified_Board_Status) The answer is .
Chessboard Status • How do we check whether a place is safe or not? • We can use four arrays of bits to record: • : rows. • : columns. • : diagonal directions. • : off-diagonal directions. Check and Update costs only 4*4 per Empress.
Speeding Up • Even we can do per check and update the status, and only search the upper-left corner, the searching time for is still too long. • Trying to cut off some branches that we don’t need. • Use some estimation, and reflection properties!
Speeding Up – 1 • If it is impossible to reach Max_Empresses, cut it off. • DFS(row, Now_Empress, Board_Status) • If([n/2]-row + Now_Empress < Max_Empresses) • Return; • If(row==[n/2]) • If(Now_Empress > Max_Empresses) • Max_Empresses = Now_Empress; • AnswerCount = 0; • If(Now_Empress == Max_Empresses) • AnswerCount++; • Else • DFS(row+1, Now_Empress, Board_Status) • Find a safe place on this row, and modify Board_Status. • DFS(row+1, Now_Empress+1, Modified_Board_Status)
Speeding Up – 2 • If this is a possible solution with a Queen in the first row of R, then its mirror is another solution. • So, if we find such one, we can count as two. • No need to consider a Queen at first column! R R R R R R R R R R R R
Speeding Up – 3 • Using Dancing Links (DLX) to maintain a sequence (linked list) of places that can still put Empresses in. • Not easy to implement, be careful handling pointers. • This speeds up when the recursion get deeper. • This speeds up if you implement it well • More information please go to: • http://en.wikipedia.org/wiki/Dancing_Links
By the way… • A Rotational N-Queen Problem, is to find how many N-Queen solutions is invariant under rotation. • The Empress Problem is exactly the same with rotational N-Queen Problem when N is in the form of or . • We can find the sequence of solutions to rotational N-Queen Problem on Web “Integer Sequence”. • See: http://www.research.att.com/njas/sequences/A033148
Finally… Thanks for your attention!