3
int bfs(vector<vector<int>> &positions, int move) {5
Position vector will contain 5 elements10
5. steps to be moved (0 to 7)12
int chessBoard[8][8]; // To maintain the state of board which cell is occupied13
memset(chessBoard, 0, sizeof(chessBoard));14
bool isVal = false; // To track if any element can be moved15
for (vector<int> &pos : positions) {16
int x = pos[0] + pos[4] * pos[2], y = pos[1] + pos[4] * pos[3];19
x = pos[0] + move * pos[2], y = pos[1] + move * pos[3];21
if (x < 0 || x > 7 || y < 0 || y > 7) return 0;24
for (int i = 0; i < 8; i++)25
for (int j = 0; j < 8; j++)26
if (chessBoard[i][j] > 1) return 0; // More than one element31
ret = bfs(positions, move + 1);34
int dfs(int idx, vector<string> &pieces, vector<vector<int>> &positions) {35
if (idx >= pieces.size()) return bfs(positions, 0);37
positions[idx].push_back(0);38
positions[idx].push_back(0);39
positions[idx].push_back(0);40
ret += dfs(idx + 1, pieces, positions); // When we decide piece will not move41
positions[idx].pop_back();42
positions[idx].pop_back();43
positions[idx].pop_back();44
// When piece will move in choosen direction.45
if (pieces[idx] == "rook" || pieces[idx] == "queen") {46
for (int x = -1; x <= 1; x++)48
for (int moves = 1; moves < 8; moves++) {49
int dx = positions[idx][0] + moves * x;50
if (dx >= 0 && dx < 8) {51
positions[idx].push_back(x);52
positions[idx].push_back(0);53
positions[idx].push_back(moves);54
ret += dfs(idx + 1, pieces, positions);55
positions[idx].pop_back();56
positions[idx].pop_back();57
positions[idx].pop_back();61
for (int y = -1; y <= 1; y++)63
for (int moves = 1; moves < 8; moves++) {64
int dx = positions[idx][1] + moves * y;65
if (dx >= 0 && dx < 8) {66
positions[idx].push_back(0);67
positions[idx].push_back(y);68
positions[idx].push_back(moves);69
ret += dfs(idx + 1, pieces, positions);70
positions[idx].pop_back();71
positions[idx].pop_back();72
positions[idx].pop_back();77
if (pieces[idx] == "bishop" || pieces[idx] == "queen") {78
for (int x = -1; x <= 1; x++)80
for (int y = -1; y <= 1; y++)82
for (int moves = 1; moves < 8; moves++) {83
int dx = positions[idx][0] + moves * x;84
int dy = positions[idx][1] + moves * y;85
if (dx >= 0 && dx < 8 && dy >= 0 && dy < 8) {86
positions[idx].push_back(x);87
positions[idx].push_back(y);88
positions[idx].push_back(moves);89
ret += dfs(idx + 1, pieces, positions);90
positions[idx].pop_back();91
positions[idx].pop_back();92
positions[idx].pop_back();99
int countCombinations(vector<string> &pieces, vector<vector<int>> &positions) {100
for (vector<int> &pos : positions) pos[0]--, pos[1]--;101
return dfs(0, pieces, positions);