Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- long int nrBoards;
- typedef struct {
- int board[8][8];
- } chessboard;
- int maxN;
- int checkPlace(int y, int x, chessboard boards) {
- if (boards.board[y - 2][x - 1] == 1) {
- return 0;
- }
- if (boards.board[y - 1][x - 2] == 1) {
- return 0;
- }
- if (boards.board[y - 2][x + 1] == 1) {
- return 0;
- }
- if (boards.board[y - 1][x + 2] == 1) {
- return 0;
- }
- if (boards.board[y + 1][x + 2] == 1) {
- return 0;
- }
- if (boards.board[y + 1][x - 2] == 1) {
- return 0;
- }
- if (boards.board[y + 2][x - 1] == 1) {
- return 0;
- }
- if (boards.board[y + 2][x + 1] == 1) {
- return 0;
- }
- return 1;
- }
- void printBoard(chessboard boards) {
- int i, j;
- for(i = 0; i < 8; i++) {
- for(j = 0; j < 8; j++) {
- printf("%d ", boards.board[i][j]);
- if (j == 7) {
- printf(" %d", i);
- }
- }
- printf("\n");
- if (i == 7) {
- printf("\n0 1 2 3 4 5 6 7\n");
- }
- }
- printf("\n");
- }
- void placeKnight(int y, int x, int n, int f, chessboard boards) {
- /* base case */
- if ((n <= 0)) {
- printBoard(boards);
- nrBoards++;
- printf("nrBoards = %ld\n\n", nrBoards);
- return;
- }
- if ((x > 7) && (y < 7)) {
- x = 0;
- y++;
- }
- if ((x > 7) && (y == 7)) {
- return;
- }
- /* recursive case */
- if ((f >= n)) {
- placeKnight(y, x + 1, n, f - 1, boards);
- }
- if (checkPlace(y, x, boards) == 1) {
- boards.board[y][x] = 1;
- placeKnight(y, x + 1, n - 1, f - 1, boards);
- }
- }
- int main(int argc, char* argv[]){
- chessboard boards;
- int i, j, n;
- n = 0;
- scanf("%d", &maxN);
- printf("start");
- for(i = 0; i < 8; i++) {
- for(j = 0; j < 8; j++) {
- boards.board[i][j] = 0;
- }
- }
- placeKnight(0, 0, maxN, 64, boards);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement