Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int board[10][10];
- int N;
- void print()
- {
- for(int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- printf(" %d ", board[i][j]);
- }
- printf("\n");
- }
- printf("\n");
- }
- int isSafe(int row, int col)
- {
- int i, j;
- for(i = 0; i < col; i++)
- if (board[row][i])
- return 0;
- for(i=row, j=col; i>=0 && j>=0; i--, j--)
- if (board[i][j])
- return 0;
- for(i=row, j=col; j>=0 && i<N; i++, j--)
- if (board[i][j])
- return 0;
- return 1;
- }
- int solve(int col)
- {
- if(col == N) {
- print();
- return 1;
- }
- int res = 0;
- for(int i = 0; i < N; i++) {
- if(isSafe(i, col)) {
- board[i][col] = 1;
- res = solve(col + 1) || res;
- board[i][col] = 0;
- }
- }
- return res;
- }
- int main()
- {
- scanf("%d", &N);
- solve(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement