completed problem 1 & problem 2#1089
Conversation
N-Queens (problem1.cpp)Your solution shows a good understanding of the backtracking approach for the N-Queens problem. However, there are a few key issues to address:
Here is a corrected version of your approach without pushing/popping: class Solution {
private:
bool isValid(int row, int col, int n, vector<string>& board) {
// Check column above
for (int i = 0; i < row; i++) {
if (board[i][col] == 'Q') return false;
}
// Check top-left diagonal
for (int i = row-1, j = col-1; i >=0 && j>=0; i--, j--) {
if (board[i][j] == 'Q') return false;
}
// Check top-right diagonal
for (int i = row-1, j = col+1; i>=0 && j<n; i--, j++) {
if (board[i][j] == 'Q') return false;
}
return true;
}
void helper(int row, int n, vector<string>& board, vector<vector<string>>& solutions) {
if (row == n) {
solutions.push_back(board);
return;
}
for (int col = 0; col < n; col++) {
if (isValid(row, col, n, board)) {
board[row][col] = 'Q';
helper(row+1, n, board, solutions);
board[row][col] = '.';
}
}
}
public:
vector<vector<string>> solveNQueens(int n) {
vector<string> board(n, string(n, '.'));
vector<vector<string>> solutions;
helper(0, n, board, solutions);
return solutions;
}
};This version initializes the board with n rows of n dots. Then, in the helper function, it directly modifies the current row and column. This avoids the pushing and popping and ensures correctness. VERDICT: NEEDS_IMPROVEMENT Word Search (problem2.cpp)Note: The verdict should be based on whether the solution is correct and efficient enough to pass the problem constraints. Minor issues can be addressed in feedback but may not necessarily require a NEEDS_IMPROVEMENT if the solution is fundamentally correct. Now, evaluate the student's solution. VERDICT: NEEDS_IMPROVEMENT |
No description provided.