Deitel & Deitel, C How to Program 6th Edition, Exercise 6.26 (Eight Queens)

/*6.26 (Eight Queens) Another puzzler for chess buffs is the Eight Queens problem. Simply stated:
Is it possible to place eight queens on an empty chessboard so that no queen is “attacking” any other—
that is, so that no two queens are in the same row, the same column, or along the same diagonal?
Use the kind of thinking developed in Exercise 6.24 to formulate a heuristic for solving the Eight
Queens problem. Run your program. [Hint: It’s possible to assign a numeric value to each square
of the chessboard indicating how many squares of an empty chessboard are “eliminated” once a
queen is placed in that square. For example, each of the four corners would be assigned the value
22, as in Fig. 6.26.]
Once these “elimination numbers” are placed in all 64 squares, an appropriate heuristic might
be: Place the next queen in the square with the smallest elimination number. Why is this strategy
intuitively appealing?*/

#include <stdio.h>


ones(int, int, int boart[8][8]);
rates(int boar[8][8]);
decide(int brd[8][8]);
print(int a[8][8]);


int main() {

int board[8][8] = { 0 };

rates(board);
print(board);

smallestRate(board);
rates(board);
print(board);

smallestRate(board);
rates(board);
print(board);

smallestRate(board);
rates(board);
print(board);

smallestRate(board);
rates(board);
print(board);

smallestRate(board);
rates(board);
print(board);

smallestRate(board);
rates(board);
print(board);

smallestRate(board);
rates(board);
print(board);
return 0;
}

print(int a[8][8]) {
int i, j;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++)
printf("%3d", a[i][j]);
printf("\n");
}
printf("\n");
}

smallestRate(int bo[8][8]) {
int a, b, i, j, smallest = 99;
for (a = 0; a < 8; a++)
for (b = 0; b < 8; b++) {
if (bo[a][b] < smallest && bo[a][b] != -1) {
smallest = bo[a][b];
i = a;
j = b;
}
}
ones(i, j, bo);

}

decide(int brd[8][8]) {
int i, j, sum, a, b, max = 0, bestRow, bestColumn;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
if (brd[i][j] != -1) {
sum = brd[i][j];
a = i + 1;
b = j;
while (a < 8) {
brd[a][b] != -1 ? sum += brd[a][b] : 0;
a++;
}
a = i - 1;
b = j;
while (a > -1) {
brd[a][b] != -1 ? sum += brd[a][b] : 0;
a--;
}
a = i;
b = j + 1;
while (b < 8) {
brd[a][b] != -1 ? sum += brd[a][b] : 0;
b++;
}
a = i;
b = j - 1;
while (b > -1) {
brd[a][b] != -1 ? sum += brd[a][b] : 0;
b--;
}
//çaprazlar
a = i + 1;
b = j + 1;
while (a < 8 && b < 8) {
brd[a][b] != -1 ? sum += brd[a][b] : 0;
a++;
b++;
}
a = i - 1;
b = j + 1;
while (a > -1 && b < 8) {
brd[a][b] != -1 ? sum += brd[a][b] : 0;
a--;
b++;
}
a = i + 1;
b = j - 1;
while (a < 8 && b > -1) {
brd[a][b] != -1 ? sum += brd[a][b] : 0;
a++;
b--;
}
a = i - 1;
b = j - 1;
while (a > -1 && b > -1) {
brd[a][b] != -1 ? sum += brd[a][b] : 0;
a--;
b--;
}
if (sum > max) {
max = sum;
bestRow = i;
bestColumn = j;
}
}
}
}
ones(bestRow, bestColumn, brd);

}

ones(int row, int column, int boart[8][8]) { // a and b are where you put your queen
int counter = 1, a, b;
boart[row][column] = -1;
a = row + 1;
b = column;

while (a < 8) {
boart[a][b] = -1;
a++;
}
a = row - 1;
b = column;
while (a > -1) {
boart[a][b] = -1;
a--;
}
a = row;
b = column + 1;
while (b < 8) {
boart[a][b] = -1;
b++;
}
a = row;
b = column - 1;
while (b > -1) {
boart[a][b] = -1;
b--;
}
//çaprazlar
a = row + 1;
b = column + 1;
while (a < 8 && b < 8) {
boart[a][b] = -1;
a++;
b++;
}
a = row - 1;
b = column + 1;
while (a > -1 && b < 8) {
boart[a][b] = -1;
a--;
b++;
}
a = row + 1;;
b = column - 1;
while (a < 8 && b > -1) {
boart[a][b] = -1;
a++;
b--;
}
a = row - 1;
b = column - 1;
while (a > -1 && b > -1) {
boart[a][b] = -1;
a--;
b--;
}
}
rates(int boar[8][8]) {
int i, j, counter = 1, a, b;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
if (boar[i][j] != -1) {
a = i + 1;
b = j;
while (a < 8) {
boar[a][b] != -1 ? counter++ : 0;
a++;
}
a = i - 1;
b = j;
while (a > -1) {
boar[a][b] != -1 ? counter++ : 0;
a--;
}
a = i;
b = j + 1;
while (b < 8) {
boar[a][b] != -1 ? counter++ : 0;
b++;
}
a = i;
b = j - 1;
while (b > -1) {
boar[a][b] != -1 ? counter++ : 0;
b--;
}
//çaprazlar
a = i + 1;
b = j + 1;
while (a < 8 && b < 8) {
boar[a][b] != -1 ? counter++ : 0;
a++;
b++;
}
a = i - 1;
b = j + 1;
while (a > -1 && b < 8) {
boar[a][b] != -1 ? counter++ : 0;
a--;
b++;
}
a = i + 1;
b = j - 1;
while (a < 8 && b > -1) {
boar[a][b] != -1 ? counter++ : 0;
a++;
b--;
}
a = i - 1;
b = j - 1;
while (a > -1 && b > -1) {
boar[a][b] != -1 ? counter++ : 0;
a--;
b--;
}
boar[i][j] = counter;
counter = 1;
}
}
}
}


Let me know in the comments if this page helped you somehow.

It is nice to know if my work helped someone, thanks :)

Comments

Popular posts from this blog

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.23 (Turtle Graphics)

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.22 (Total Sales)

Deitel & Deitel, C How to Program 6th Edition, Exercise 5.47 (Craps Game Modification)