Thread: "uniqe" practice for 8 qeens problem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    "uniqe" practice for 8 qeens problem

    Hi all,
    I'm trying to do a lab assignment for a 8 qeens problem:

    "There are 92 posts that are possible for 8 queens on board - chess, so they do not threaten each other.In this task You will have to get a whole number 'n' between 1 and 92 (includes 1 - 92), and print the 'n' solution in the all Possible solutions. Ie: if the input number detected is 5, will be printed only the fifth solution,in all Possible solutions. If the number is 6, will print only the sixth solution, etc..

    Instructions:
    You have to use one-dimensional array to keep the current board situation: in the - i cell saved the column that The i+1 Queen was set. Ie: no. column in which the first Queen set will be 0, no. column in which the second Queen set will be 1, etc..
    A simple solution for the problem is possible by useing - 8 nested loops. Note: In this exercise you forbidden to use - 8 nested loops."

    I'm trying to it for a week now and no success, plz help

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum!

    OK, what have you tried ? Whatever it is *post it* here, and highlight the code, and click on the # icon (just below the smilies pull-down menu), in the reply window. That makes your code stay looking like code, and not get mangled by the forum editor.

    We *don't* want to do your assignment for you, we want to *help* you, do your assignment.

    The sooner you post something up, the sooner we can get started.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Can you code the simple solution (8 nested loops)?

    The real solution is to replace those nested loops with recursive function calls (1 loop, nesting achieved through recursion).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You could write a state machine.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use an array of 92 rows of 8 columns of integer values ranging from 0 to 63 (the position of the queen on the board). When they enter a number from 0 to 91, print them out the queen's positions for that column. Win for having the fastest answer.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    What I had before would not work. I deleted the post.

    I suspect he OP does not know where to start.

    The basic approach I see is that you know there is a queen in each column. So loop through each possible column. Place a queen, using the solution number modulo the number of available spaces to determine the location in the column. Then remove all the locations attacked by that queen from future consideration.
    Last edited by King Mir; 11-14-2009 at 08:42 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He says he's been trying for a week, should have at least an include file and int main(). The problem isn't that hard after you study it a bit.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Writing code before you have an algorithm is bad practice. Design first, then code.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    I'm now at the college, I hope I get home early and will upload it.
    my idea (that not working), is to build from zero 92 bords, and when the user will hit a choice it's will build one (even that not working very well),
    the first board situation will put a qeen in (0,0), and then will arrange the others, the 2nd will put the first qeen in (0,1) etc...
    but there is an other difficulty, the board situation need to match his!

    for example:
    when the user will hit 1, the solution will be:
    0,0 1,4 2,7 3,5 4,2 5,6 6,1 7,3

    when the user will hit 1, the solution will be:
    0,1 1,3 2,5 3,7 4,2 5,0 6,6 7,4

    (this is the exmaples add to the assiment)

    no 8 nested loops allowed, no state machine and no recursive function .

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    There should be more than one solution with a queen at 0,0.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Megazord, why don't you post up the start of your program, and put 3 functions in it:

    1) markRows
    2) markCols
    3) markDiagonals or markDiag

    These will be void return functions for now.

    Although it's not usually recommended, since every function will need the board[] to do anything, make it a global array for now.

    Take the board array and set every sqr to 1. That will indicate a good square for the next queen, when we're done removing the unlawful squares.

    Include anything else you think would be helpful.

    Here's an untested bit of what I'm thinking of.

    Code:
    status: just starting, not tested.
    
    */
    #include <stdio.h>
    
    void markRow(int, int *);       //function prototypes
    void markCol(int);
    void markDiag(int);
    void printIt(void);
    void saveAnswers(char answers[92][]);
    
    board[64]; 
    
    int main() {
       char answers[92][65]; 
       int i, j, sqr, index;
       int lowRows[] = { 0,8,16,24,32,40,48,56 };
    
       for(i = 0; i < 64; i++)
         board[i] = 1;
    
       for(i = 0; i < 64; i += 8)  //marks queenside edge of our board
         board[i] = -1;
    
    
       for(i = 0; i < 92; i++) {
         for(j = 0; j < 64; j++) {
           answers[i][j] = '0';
         }
         answers[i][j] = '\0';  //set EOS at the end of every answer
       }
    
       printf("\n\n Enter the number of the solution you want to see [1-92]:");
       sqr = 26;  //assignment for now
       markRow(sqr, lowRows);
       printf("\n Rows are marked. Sqr is %d\n", sqr);
       printIt();
       j = getchar();
       printf("\n");
       markCol(sqr);
       printf("\n Columns are marked. Sqr is %d\n", sqr);
       printIt();
       j = getchar();
       printf("\n");
       markDiag(sqr);
       printf("\n Diagonals are marked. Sqr is %d\n", sqr);
       printIt();
    /*
       saveAnswer(answers[92][]);
    */
      printf("\n\n\t\t\t     press enter when ready");
      i = getchar();
      return 0;
    }
    void markDiag(int sqr) {
      int i, low; // vector;
    
      for(i = sqr; i < 64; i += 9) {    //4 o'clock vector
        if(board[i] < 0)
          break;
        else
          board[i] = 0;
    
      }
    }
    void markRow(int sqr, int lowRows[8]) {
      int i, low, hi;
    
      i = sqr/8;
      low = lowRows[i];
      hi = low + 8;
      for(i = low; i < hi; i++) {
        board[i] = 0;
      }
    }
    void markCol(int sqr) {
      int i, low;
      low = sqr % 8;
      
      for(i = low; i < 64; i+= 8) {
        board[i] = 0;
      }
    }
    void printIt(void) {
      int i;
    
      printf("\n");
      for(i = 0; i < 64; i++) {
        if(i && i % 8 == 0)
          printf("\n");
        printf("%2d ", board[i]);
    
      }
    }
    Diagonal checking for the queens is coded up for only 1 direction, and this is very much unchecked and impromptu. The tricky part is getting all the solutions, of course.
    Last edited by Adak; 11-16-2009 at 08:31 AM.

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    first of all, Adak and all thank you for the time and understanding
    Adak, no functions are allowed.

    this is my program, the first prog let the user to insert 8 coordinate in the borad, and then checks if its valid solution to 8 queen problam.
    the 2nd this is where I'm stuck.
    but I get a new idea maybe I will take arr2[8], the index of arr will be rows,
    and the the int inside will be the column, and then I will bulid a solution that will follow this ruls:
    1. will not be the same int on arr[]
    2. the tangent will be different (for diagonal check).
    the first int on arr0 will be 0.
    how to save the results, this is another story :\


    Code:
    #include<stdio.h>
    void main(){
    	int program;
    	int i,chess[8][8]={0},temp_j,temp_k,z,w,k,j,flag = 0,l;
    	int arr2[8]={0};
    	char c;
    
    	do{
    	printf("1.  8 queens assignment\n");
    	printf("2.  8 queens solution print\n");
    	printf("3.  Check query\n");
    	printf("4.  Exit program\n");
    	printf("Enter your choice (1-4):");
    	scanf("%d",&program);
    	switch(program){
    //--------------------------------action number 1---------------------
    		case(1):
    			do{
    				printf("\nEnter location of queen no. %d : ",i);
    				scanf("%d%c%d",&temp_j,&c,&temp_k);
    				if(temp_j<0||temp_j>7||temp_k<0||temp_k>7){
    					printf("\nOut of bundaries!\n");
    					printf("Enter location of queen no. %d : ",i);
    					continue;
    				}
    				if(chess[temp_j][temp_k] == 1){
    					printf("\nAlready taken!\n");
                        continue;
    				}
    				chess[temp_j][temp_k] = 1; 
    				i++;
    			}while(i<8);
    
    			for (i=0;i<8;i++){           //*--------------------*
    				if (flag)break;          //*passing on the array*
    				 for(j=0;j<8;j++){       //*--------------------*
    					if (flag)break;
    					if (chess[i][j] == 1){ //checking for queen on square
    
    						for (k=i+1;k<8;k++){//checking for queen on the same row
    							if (chess[k][j] == 1){
    							   printf("Impossible assignment!\n");
    							   flag = 1;
    							   break;
    							   }
    						}
    						if (flag)break;
    
    						for (k=j+1;k<8;k++){//checking for queen on on the same column
    								if (chess[i][k] == 1){
    								printf("Impossible assignment!\n");
    								flag = 1;
    								break;
    								}
    							}
    						if (flag)break;
    
    						w=i+1;//checking for queen on on the right diagonal 
    						z=j+1;
    						while(((z<8)&&(z>=0))&&((w>=0)&&(w<8))){
    							if (chess[w][z] == 1){
    								printf("Impossible assignment!\n");
    								flag = 1;
    								break;
    							}
    							w++;
    							z++;
    						}
    						if (flag)break;
    
    						w=i+1;//checking for queen on on the left diagonal 
    						z=j-1;
    						while(((z>=0)&&(z<8))&&((w<8)&&(w>=0))){
    						   if (chess[w][z] == 1){
    							 printf("Impossible assignment!\n");
    							 flag = 1;
    							 break;
    							}
    							w++;
    							z--;
    						}
    						if (flag)break;
    
    						}
    					}
    				}
    
    		if (flag==0)
    			printf("Possible assignment!\n");
    
        //initialization variables
    	flag=0;
    	for (i=0;i<8;i++){
    		for(j=0;j<8;j++)
    			chess[i][j]=0;
    	}
    		break;
    //--------------------------------------------Action number 2
    		case(2):break;
    //--------------------------------------------Action number 3
    		case(3):break;
    //--------------------------------------------Wrong choice - back to main menu.
         default:
    		 printf("\n");
    		 break;
       	 }
    	}while (program!=4);
    		
     }

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That looks like quite a program.

    NO functions ?? Next thing, they'll want us to code up the program while deep sea diving.

    Your assignment said you had to use a 1D array to represent the board. Your program uses a 2D chess[][] array - but you know that.

    I thought a 2D char array[][] would be good to hold the answers, but I didn't think you were coming back, so I stopped all efforts in that regard.

    Does your program generate all the possible 8 queen solutions, from any given starting square, yet?

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    what can I do, this my proffesor rules.

    yeha, it's a big program, I'm seraching for a way to make her efficient, I'm lokking for 4 directions so I need 4 loops.
    'cuse the first one ended so big, this is the reason I don't start the 2nd one until i had somthing good in mind!

    no, i write an algoritem and i'm stuck In the middel of it.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I went looking for my old 8 queens program, but when I found it - it was in another language, so I'm coding up a new program, from scratch.

    Naturally, I'm using functions, and when you didn't post up for a few days, I changed it over to a board[100] array, and have "border" values in the array:

    so it looks something like this:

    Code:
    8 8 8 8 8 8 8 8 8 8    8 is a border value Cells with an 8 never change their value
    8 2 0 0 0 0 0 0 0 8    but stop the diagonal checking loops
    8 0 0 1 0 0 0 0 0 8
    8 0 0 0 0 1 0 0 0 8    2 is the value representing the first Queen's square
    8 0 1 0 0 0 0 0 0 8
    8 0 0 0 1 0 0 0 0 8
    8 0 0 0 0 0 0 0 0 8
    8 0 0 0 0 0 0 0 0 8
    8 0 0 0 0 0 0 0 0 8
    8 8 8 8 8 8 8 8 8 8
    Did you find out if your program generates all the possible answers for 1) A queen in the first square on the board, and 2) A first queen which could be in any square ?
    Last edited by Adak; 11-17-2009 at 07:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  3. inStream practice problem not working?
    By correlcj in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 05:58 PM
  4. Question on an array practice problem
    By Vanished in forum C Programming
    Replies: 1
    Last Post: 01-22-2002, 07:12 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM

Tags for this Thread