문제

For practice I have started solving interviewstreet problems. I cam across this queens on board problem.

Queens on Board (50 Points)

You have an N * M chessboard on which some squares are blocked out. In how many ways can you place one or more queens on the board such that no two queens attack each other? Two queens attack each other if one can reach the other by moving horizontally, vertically or diagonally without passing over any blocked square. At most one queen can be placed on a square. A queen cannot be placed on a blocked square.

Input: The first line contains the number of test cases T. T test cases follow. Each test case contains integers N and M on the first line. The following N lines contain M characters each representing the board. A '#' represents a blocked square and a '.' represents an unblocked square.

Output: Output T lines containing the required answer for each test case. As the answers can be really large, output them modulo 1000000007.

https://www.interviewstreet.com/challenges/dashboard/#problem/4e904d63c5069

I am wondering what is the best algorithm to solve this problem?

Thank you.

올바른 솔루션이 없습니다

다른 팁

This page provides an easy to follow implementation of the N-Queens problem. If you want to find all permutations, in the nQueenProblem method, instead of using the simple addQueen(0, queens, n);, just iterate with different values using a for loop. And instead of returning the correct board, just count the boards.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top