| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 11220 | Permutation |
|
| 11230 | M Queens N Rocks |
|
Description
Given a set of n>=1 elements, the problem is to print all possible permutations of this set.
For example, if n = 3, then the set is (1,2,3), and the set of permutations is
(1,2,3)
(1,3,2)
(2,1,3)
(2,3,1)
(3,2,1)
(3,1,2)
- Whenever you want to swap an element, you should swap with the orginal set of the current index.
- For example, the set after (2,3,1) should swap the 1st element, which is to swap with the origin set of current index 1, that is (1,2,3), rather than swap with (2,3,1) itself. And after swap 1 and 3 from (1,2,3), the result is (3,2,1).
A recursive method to implement this problem is as follows:
Consider the case of (1,2,3,4), that is, n=4.
- Place the set elements in a global array, and set the position index “k” as 0.
- Use a for-loop to “swap” (or exchange) the 1st element with the 1st element, the 2nd element, the 3rd element, and the 4th element, respectively.
- In each loop-iteration:
- increment the position index “k” by 1 (for considering only the remaining elements in the following recursive call);
- use the updated k to recursively call your permutation function;
- note that because you use a global array, remember to swap back the two elements after the iteration.
- In each loop-iteration:
- In a recursive-call path, when k reaches n, it means that you get a possible permutation.
Input
An interger n (1=<n<=10) that represents the number of elements in the set.
Output
Print all permutations.
Note that you have to print a '\n' at the end of each line.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
The rule for placing a queen or a rock on the board is as follows:
1. Each row and column of the chessboard contains exactly 1 queen , and each diagonal also contains no more than 1 queen.
For example, a queen will threaten any other pieces on the red blocks in the following image

2. Each row and column of the chessboard contains no more than 1 rock.
That is, a rock will threaten any other pieces on the red blocks in the following image

Your have to put M queens and N rocks on an (M+N)x(M+N) chessboard in a way that no one threatens another.
Then, your task is to compute how many possible ways to put M queens and N rocks on the chessboard.
For instance, if M = 7 and N = 1, your answer should be 736 since there are 736 valid ways to arrange 7 queens and 1 rock on the chessboard.
Input
There are 2 integers in a line.
The first integer is M which means the number of queens that you need to put on the chessboard.
The second integer is N which means the number of rocks that you need to put on the chessboard.
Note that 1<=M+N<= 10
Output
An integer which represent the number of possible arrangements of M queens and N rocks.
You have to add '\n' at the end of output!