Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- char trial[8][8] = {{'0', '0', '0', '0', '0', '0', '0', '0'},
- {
- '0', '0', '0', '0', '0', '0', '0', '0'},
- {
- '0', '0', '0', '0', '0', '0', '0', '0'},
- {
- '0', '0', '0', '0', '0', '0', '0', '0'},
- {'0', '0', '0', '0', '0', '0', '0', '0'},
- {
- '0', '0', '0', '0', '0', '0', '0', '0'},
- {
- '0', '0', '0', '0', '0', '0', '0', '0'},
- {
- '0', '0', '0', '0', '0', '0', '0', '0'}};
- int counter = 0;
- void putQueen(int rowIndex, int columnIndex) {
- trial[rowIndex][columnIndex] = '1';
- }
- void removeQueen(int rowIndex, int columnIndex) {
- trial[rowIndex][columnIndex] = '0';
- }
- bool canPut(int r, int c) {
- int rowIndex = r;
- int columnIndex = c;
- while (rowIndex < sizeof(trial[0]) / sizeof(trial[0][0]) && columnIndex < sizeof(trial[0]) / sizeof(trial[0][0]))
- if (trial[rowIndex++][columnIndex++] == '1')
- return false;
- rowIndex = r;
- columnIndex = c;
- while (rowIndex >= 0 && columnIndex >= 0)
- if (trial[rowIndex--][columnIndex--] == '1')
- return false;
- rowIndex = r;
- columnIndex = c;
- while (rowIndex >= 0 && columnIndex < sizeof(trial[0]) / sizeof(trial[0][0]))
- if (trial[rowIndex--][columnIndex++] == '1')
- return false;
- rowIndex = r;
- columnIndex = c;
- while (rowIndex < sizeof(trial[0]) / sizeof(trial[0][0]) && columnIndex >= 0)
- if (trial[rowIndex++][columnIndex--] == '1')
- return false;
- for (int i = r; i < sizeof(trial[0]) / sizeof(trial[0][0]); i++)
- if (trial[i][c] == '1')
- return false;
- for (int i = c; i < sizeof(trial[0]) / sizeof(trial[0][0]); i++)
- if (trial[r][i] == '1')
- return false;
- for (int i = r; i >= 0; i--)
- if (trial[i][c] == '1')
- return false;
- for (int i = c; i >= 0; i--)
- if (trial[r][i] == '1')
- return false;
- return true;
- }
- void queen(int rowIndex) {
- if (rowIndex == 8)
- counter++;
- for (int columnIndex = 0; columnIndex < 8; columnIndex++)
- if (canPut(rowIndex, columnIndex)) {
- putQueen(rowIndex, columnIndex);
- queen(rowIndex + 1);
- removeQueen(rowIndex, columnIndex);
- // لو اللي بعدي ملقاش مكان يتحط فيه، شيلني وغير مكاني وكمل
- //او لو لقيت طريقة ناجحة وعايز ارجع عشان اشوف بقيت الطرق
- }
- }
- int main() {
- queen(0);
- cout << counter << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement