Skip to content

Conversation

@thondaha
Copy link

  1. NQueens problem
  2. WordSearch problem

@super30admin
Copy link
Owner

Strengths:

  • The backtracking approach is correctly implemented.
  • The code is well-commented and easy to understand.
  • The helper methods (buildSolution, isValid) are appropriately separated.
  • The solution handles the base case correctly by building the solution when all rows are filled.

Areas for improvement:

  • The solution file includes an unrelated WordSearch class, which should be removed or placed in a separate file to maintain focus on the N-Queens problem.
  • The isValid method can be optimized by checking only the rows above the current row (since we place queens row by row). The current implementation checks from the current row downward, but since we are placing queens from top to bottom, there are no queens below the current row. The loop in isValid starts at row and goes down to 0, which is correct, but the initial condition for (int r = row; r >= 0; r--) is efficient. However, note that the first loop in isValid checks the same column for rows from row down to 0. This is correct, but the loop variable should start at row-1 since the current row hasn't been placed yet (the board at row is being checked before placement). Actually, the current implementation is correct because it checks from the current row downward, but since the current row hasn't been set to true yet, it effectively checks only above. However, the loop includes the current row, which is unnecessary. You can start from row-1 to avoid checking the current row (which is not yet set). But since the current row is not set to true, it doesn't cause an error. It's a minor inefficiency.

Suggestion:

  • Change the loops in isValid to start from row-1 for the same column and diagonals. For example:
    • Same column: for (int r = row-1; r >=0; r--)
    • Upper-left diagonal: for (int r = row-1, c = col-1; r >=0 && c>=0; r--, c--)
    • Upper-right diagonal: for (int r = row-1, c = col+1; r>=0 && c<n; r--, c++)

This avoids checking the current row, which is not necessary since the current cell is not yet set.

Overall, the solution is correct and efficient, but the inclusion of the WordSearch class is a distraction.

@thondaha
Copy link
Author

This Backtracking-3 Repo contains two problems , submitted one pull request for 2 problems i.e.,

  1. word search
  2. N queens in two different files it self

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants