ajay-dhangar / algo

This repository contains a collection of data structures and algorithms implemented in various programming languages. It is designed to help learners understand key concepts through hands-on examples. Contributions and improvements are welcome!
https://ajay-dhangar.github.io/algo/
MIT License
70 stars 231 forks source link

[Feature]: <Feature Name> #1159

Closed shashmitha46 closed 2 weeks ago

shashmitha46 commented 3 weeks ago

Feature Name

Adding N-Queens Backtracking Algorithm

Feature Description

What it does:

The N-Queens backtracking algorithm solves the classic N-Queens problem. In this problem, the goal is to place N queens on an NxN chessboard so that no two queens can attack each other. Queens can move horizontally, vertically, and diagonally, so the challenge is to position them in a way where none of them are in the same row, column, or diagonal. This algorithm uses a method called "backtracking" to try placing queens on the board one by one and "backtracks" if it encounters a problem, like two queens being able to attack each other.

Why it's useful:

This feature is useful for solving problems that involve making multiple choices and testing different possibilities. The N-Queens problem is a well-known example of a constraint satisfaction problem, which is common in areas like AI, scheduling, and optimization. By adding this algorithm, we provide an elegant solution to a complex puzzle that can also serve as a learning tool for understanding backtracking, recursion, and problem-solving techniques.

Additional Context:

The algorithm can be applied to boards of different sizes, making it versatile. It’s also a foundation for solving other similar problems where multiple solutions are possible, and we need to search for all valid configurations. The backtracking approach used in this algorithm helps explore many potential solutions without having to check every single possibility, making it efficient.

Motivation

The N-Queens algorithm is a fundamental problem that demonstrates the backtracking technique, making it a valuable educational tool for users learning recursion and constraint satisfaction. By adding this feature, users gain access to a practical example of solving complex problems efficiently. It benefits users by enhancing their problem-solving skills, offering a reusable template for similar challenges, and contributing to Algo’s collection of essential algorithms for competitive programming and interviews. The algorithm is also relevant to real-world applications like scheduling and optimization.

Implementation Suggestions (Optional)

  1. Use a recursive backtracking approach to explore each row, placing queens and backtracking if necessary.
  2. Represent the board as a 2D list or an array storing queen positions.
  3. Implement a helper function to check if a position is safe (no conflicts in column or diagonals).
  4. Store valid solutions as a list of board configurations (each as a list of strings).
  5. Write test cases using Python’s unittest framework for different values of N.
  6. No external libraries are needed, but NumPy can be used for matrix handling if desired.

Feature Type

New Algorithm

Does this feature require additional resources?

References (Optional)

No response

github-actions[bot] commented 3 weeks ago

👋 Hi @shashmitha46! Thank you for opening your first issue on the Algo project. We're excited to help you out and appreciate your contribution. Please provide as much detail as possible to assist us in addressing the issue effectively. Welcome aboard! 😊

github-actions[bot] commented 3 weeks ago

👋 Hi @shashmitha46! Thanks for opening this issue. We appreciate your contribution to the Algo project. Our team will review it soon.