Skip to content

Complete Backtracking-3#1082

Open
PrakarshKamal wants to merge 1 commit intosuper30admin:masterfrom
PrakarshKamal:master
Open

Complete Backtracking-3#1082
PrakarshKamal wants to merge 1 commit intosuper30admin:masterfrom
PrakarshKamal:master

Conversation

@PrakarshKamal
Copy link

No description provided.

@super30admin
Copy link
Owner

N-Queens (NQueens.java)

Your approach to solving the N-Queens problem is on the right track, but there are a few critical errors to address:

  1. Recursive Call: In the helper method, you are passing n as the first argument when you should be passing row+1. The correct call should be helper(row+1, n, board, ans). This mistake will cause the recursion to fail because the row parameter is being overwritten by n.

  2. Validation Checks: In the isValid method, your loops for checking diagonals start at the current row (row) and go downwards. However, you should start from row-1 because the current row (where you are about to place the queen) hasn't been fully populated yet. For example:

    • For the left upper diagonal, you should start at row-1 and col-1 and go upwards.
    • Similarly for the right upper diagonal, start at row-1 and col+1.
    • The vertical check is correct: it checks from row 0 to row-1.

    Currently, your loops start at row and go down to 0. This works by chance because the current cell (row, col) is still '.' during the check, so it doesn't cause a false positive. However, it is inefficient and confusing. Change the loops to start from row-1:

    • For left diagonal: for (int i = row-1, j = col-1; i >=0 && j>=0; i--, j--)
    • For right diagonal: for (int i = row-1, j = col+1; i>=0 && j<n; i--, j++)
  3. Parameter Order: Ensure consistency in the order of parameters. Your helper method has parameters (row, n, board, ans), while isValid has (row, col, n, board). It's better to keep the order consistent to avoid mistakes.

  4. Initialization: You initialize the board with '.' which is correct. However, you can avoid initializing the entire board by only setting the necessary cells during backtracking. But this is minor.

  5. Efficiency: Consider using additional data structures to mark occupied columns and diagonals to make the isValid function O(1). This would optimize the solution from O(n * n!) to O(n!), but it requires more space. For n up to 9, it might not be necessary.

After fixing these issues, your solution should work correctly. Keep up the good work in thinking through the backtracking approach!

VERDICT: NEEDS_IMPROVEMENT


Word Search (WordSearch.java)

Your solution is on the right track but has a critical bug related to backtracking the visited state. Specifically, when your DFS function finds a path (returns true), it does not reset the visited state of the current cell. This leaves the visited array in an inconsistent state for subsequent searches from other starting points. To fix this, you must ensure that the current cell's visited state is reset even when returning true. However, note that if you are returning true because you found the word, you don't want to reset the visited state for the entire path? Actually, for the problem, we only need to find one occurrence. So if we find the word, we can return true immediately without resetting the visited array? But wait: the visited array is shared across all starting points. When we start a new search from a new cell, we need the visited array to be clean. Therefore, you should reset the visited state of the current cell

VERDICT: NEEDS_IMPROVEMENT

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