Category : n-queens

I’m trying to do this nQueens problem and my code looks like this: class Solution { public: vector<vector<string>> ans; bool canPlace(vector<string> &board, int row, int col, int n){ //upper left diagonal int rowIndex = row; int colIndex = col; while(rowIndex >= 0 and colIndex >= 0){ if(board[rowIndex][colIndex] == ‘Q’){ return false; } rowIndex–; colIndex–; } ..

Read more

here is my code #include <iostream> #include <vector> using namespace std; int k = 4; vector<int> QLocation(k,0); vector<vector<string> > board(k, vector<string>(k, ".")); bool check(int row, int column) { if (row == 0) return true; for (int m = row – 1, n = 1; m >= 0; m–, n++) { if (board[m][column] == "Q") return ..

Read more

I managed to understand the solution for the N-Queens problem but I have a slight confusion regarding the time complexity of the solution. I tink that overall complexity would be O(n!*(n^2)). I know it is wrong. Please Help. ` void placeQueens(int row, vector<int> &columns,int n, vector<vector<int>>&ans){ if(row==n){ ans.push_back(columns); return; } for(int col=0;col<n;col++){ // O(n) time ..

Read more

template void Stack<Item>::push(const Item& entry) // Library facilities used: cassert { assert(size()<CAPACITY); data[used]=entry; ++used; } template <class Item> Item Stack<Item>::pop( ) // Library facilities used: cassert { assert(!empty()); –used; return data[used]; } I have these two functions for a stack,but I want to change them so that the stack grows down , the first push ..

Read more