We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Queens on Board
Queens on Board
Sort by
recency
|
27 Discussions
|
Please Login in order to post a comment
N queen Problem
include
include
using namespace std;
bool isSafe(vector>& board, int row, int col, int n) { for (int i = 0; i < row; ++i) { if (board[i][col] == 1) return false; if (col - (row - i) >= 0 && board[i][col - (row - i)] == 1) return false; if (col + (row - i) < n && board[i][col + (row - i)] == 1) return false; } return true; }
void solveNQueensUtil(vector>& board, int row, int n) { if (row == n) { for (auto& row : board) { for (int cell : row) { cout << (cell == 1 ? "Q " : "- "); } cout << endl; } cout << endl; return; }
}
int main() { int n; cin >> n;
}
Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Queens on Board Problem Solution
guys this code is work for me.....
include
include
define MOD 1000000007
int n, m; char g[50][5]; int good[50][1 << 5]; int szg[50]; int block[50]; int memo[50][1 << 15]; int memo2[1 << 15];
int spread(int mask) { if (memo2[mask] != -1) return memo2[mask];
int nmask = 0; for (int i = 0; i < m; i++) { if (mask & 1 << 3 * i && i > 0) { nmask |= 1 << 3 * i - 3; } if (mask & 1 << 3 * i + 1) { nmask |= 1 << 3 * i + 1; } if (mask & 1 << 3 * i + 2 && i + 1 < m) { nmask |= 1 << 3 * i + 5; } } return memo2[mask] = nmask; }
int solve (int x, int mask) { if (x == n) return 1;
mask &= ~block[x]; if (memo[x][mask] != -1) return memo[x][mask];
int ret = 0; for (int i = 0; i < szg[x]; i++) {
} return memo[x][mask] = ret; }
int solve() { for (int i = 0; i < n; i++) { block[i] = 0; int cmask = 0; for (int j = 0; j < m; j++) { if (g[i][j] == '#') { cmask |= 1 << j; block[i] |= 7 << 3 * j; } }
} memset(memo,255,sizeof memo); memset(memo2,255,sizeof memo2); return solve(0,0); }
int main() { int runs; scanf("%d",&runs); while (runs--) { scanf("%d%d",&n,&m); for (int i = 0;i < n;i++) scanf("%s",g[i]); int ret = solve(); ret = (ret - 1 + MOD) % MOD; printf("%d\n",ret); } return 0; }
Any explanation for the code please
Here is Queens on Board problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-queens-on-board-problem-solution.html