Sort by

recency

|

27 Discussions

|

  • + 0 comments

    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; }

    for (int col = 0; col < n; ++col) {
        if (isSafe(board, row, col, n)) {
            board[row][col] = 1;
            solveNQueensUtil(board, row + 1, n);
            board[row][col] = 0;
        }
    }
    

    }

    int main() { int n; cin >> n;

    vector<vector<int>> board(n, vector<int>(n, 0));
    solveNQueensUtil(board, 0, n);
    
    return 0;
    

    }

  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Queens on Board Problem Solution

  • + 0 comments

    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++) {

    if (!(good[x][i] & mask)) {
    
      ret += solve(x + 1, spread(good[x][i] | mask));
      if(ret >= MOD) ret -= MOD;
    }
    

    } 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; } }

    szg[i] = 0;
    for (int j = 0; j < 1 << m; j++) {
      if ((j & cmask) == 0) {
        bool valid = true;
        for (int k = 0; k < m; k++) {
          if (j & 1 << k) {
            for (int kk = k + 1; kk < m && g[i][kk] != '#'; kk++) {
              if (j & 1 << kk) {
                valid = false;
              }
            }
          }
        }
        if (!valid) continue;
        int sp = 0;
        for (int k = 0; k < m; k++) {
          if (j & 1 << k) {
            sp |= 7 << 3 * k;
          }
        }
        good[i][szg[i]] = sp;
        szg[i]++;
      }
    }
    

    } 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; }

  • + 0 comments

    Any explanation for the code please

  • + 0 comments

    Here is Queens on Board problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-queens-on-board-problem-solution.html