Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void n_queens(int board[10][10], int size, int solution_num)
- {
- int temp;
- int counter = 0;
- int malkot[10] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };
- for (int i = 0; i < size; i++)
- {
- temp = PutMeMalkot(malkot, size, 0, 0, i);
- if (temp != -4)
- {
- malkot[i] = temp;
- }
- else
- {
- temp = ChnageAllThePastMalkot(malkot, i, size, i);
- if (temp == -6)
- {
- printf("no solution ");
- }
- else
- {
- i = temp;
- }
- }
- if (i == size-1)
- {
- counter++;
- ChnageAllThePastMalkot(malkot, i, size, i);
- }
- if (counter==solution_num)
- {
- printmalkot(board, size, 0, 0);
- }
- }
- }
- int ChnageAllThePastMalkot(int malkot[], int Row, int size, int witchRow)
- {
- if (malkot[Row] + 1 < size - 1 && Row < 0)
- {
- if (malkot[Row - 1] + 1 > size)
- {
- malkot[Row - 1] = -1;
- return ChnageAllThePastMalkot(malkot, Row - 1, size, witchRow);
- }
- else
- {
- int temp = PutMeMalkot(malkot, size, 0, malkot[Row - 1] + 1, Row - 1);
- if (temp == -4)
- {
- malkot[Row - 1] = -1;
- return ChnageAllThePastMalkot(malkot, Row - 1, size, witchRow);
- }
- else
- {
- malkot[Row - 1] = temp;
- return Row - 1;
- }
- }
- }
- else
- {
- malkot[witchRow] = -6;
- return -6;
- }
- }
- int PutMeMalkot(int malkot[], int size, int row, int place, int WhatRow)
- {
- if (place >= size)
- {
- return -4;
- }
- if (row < size - 1 && WhatRow != 0 && row != WhatRow)
- {
- if (malkot[row] != place && (malkot[row] + (WhatRow - row) != place) && (malkot[row] - (WhatRow - row) != place))
- {
- return PutMeMalkot(malkot, size, row + 1, place, WhatRow);
- }
- else
- {
- return PutMeMalkot(malkot, size, 0, place + 1, WhatRow);
- }
- }
- else
- {
- return place;
- }
- return -2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement