C Programming Language (CS2402): HW6

Instructor: J.-S. R. Jang


Due date: Nov. 4, 1996
  1. (60%) Develop a program to solve the Eight-Queen problem mentioned in Exercise 6.26 (page 255) of Chapter 6. To make it simple, we do not need to use any heuristics. The basic strategy is to randomly pick a queen position that will not be attacked by previously placed queens, and keep doing this until all 8 queens are placed. If you cannot place all 8 queens, print out the number of queens been placed and start all over again. A sample output list is as follows:
    Try 1: 7 queens have been placed
    Try 2: 6 queens have been placed
    Try 3: 5 queens have been placed
    Try 4: 7 queens have been placed
    Try 5: 7 queens have been placed
    Try 6: 7 queens have been placed
    Try 7: 7 queens have been placed
    Try 8: 5 queens have been placed
    Try 9: Congratulations on successfully placed all 8 queens!
    
    1 1 1 1 1 1 1 2
    1 1 2 1 1 1 1 1
    2 1 1 1 1 1 1 1
    1 1 1 1 1 2 1 1
    1 2 1 1 1 1 1 1
    1 1 1 1 2 1 1 1
    1 1 1 1 1 1 2 1
    1 1 1 2 1 1 1 1                     
    	
    In the preceding output list, "2" denotes the location of the queens. Note that the total number of trys (9 in the above case) should be something between 1 and 100. If you program constantly takes more than 100 trys to solve the problem, then very probably there is a bug in your program.