Sample Approach
Problem – Print all unique combinations of setting N pieces on an NxN board
Approach: This problem can be solved by using recursion to generate all possible solutions. Now, follow the steps below to solve this problem:
- Create a function named allCombinations, which will generate all possible solutions.
- It will take an integer piecesPlaced denoting the number of total pieces placed, integer N denoting the number of pieces needed to be placed, two integers row and col denoting the row and column where the current piece is going to be placed and a string ans for storing the matrix where pieces are placed, as arguments.
- Now, the initial call to allCombinations will pass 0 as piecesPlaced, N, 0 and 0 as row and col and an empty string as ans.
- In each call, check for the base case, that is:
- If row becomes N and all pieces are placed, i.e. piecesPlaced=N. Then print the ans and return. Else if piecesPlaced is not N, then just return from this call.
- Now make two calls:
- One to add a ‘*’ at the current position, and one to leave that position and add ‘-‘.
- After this, the recursive calls will print all the possible solutions.
How to Write DSA Articles on w3wiki?
w3wiki provides all the coding enthusiasts an opportunity to showcase their programming and Data Structures & Algorithms skills by writing coding or DSA-based articles. However, a lot of individuals (especially college students or beginners) find it difficult to articulate their learnings & skills and contribute at w3wiki. But now the problem has got solved as this article will guide you through the entire process and guidelines of writing Programming/DSA articles at w3wiki.
Let’s get started:
First and foremost, you need to know how to get started with article writing at GFG along with various other fundamental aspects like why should you contribute, where to write, etc. You can check out this link to know all these details in a comprehensive manner.