Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In this program, the user should be the one to input the row and column of the queen on every iteration and fortunately I managed to do that. The problem is that, on every iteration of the queen, there should be a function that will check whether the row and column is safe to place or not. The function must check the vertical, horizontal and diagonal if there is a queen exist, if so a message will display that a queen exist and will go back on the current counter of the queen. This is what I've done so far. Any help is highly appreciated, Thank You.

#include<stdio.h>
#include<math.h>
int i, j,n;
int a[20][20];
int row, col;
main()
{
	clrscr();
	printf("Enter no. of queen(s): ");
	scanf("%d",&n);

	print_board();

	for(i=1;i<=n;i++)
	{
		printf("\n\nPosition of Queen %d",i);
		printf("\n\tRow: ");
		scanf("%d",&row);
		printf("\tColumn: ");
		scanf("%d",&col);

		if(safeToPlace(row,col) == 1)
		{
			place_Queen(row,col);
		}

	}
	print_Array();


getch();
}

int print_board()
{
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			a[i][j] = '-';
		}
	}

}

int print_Array()
{
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			printf("%c\t",a[i][j]);
		}
	printf("\n\n");
	}
printf("\n");
}

int place_Queen(int row, int col)
{
	a[row][col] = 'Q';
}

/* The function were I got the problem */
int safeToPlace(int row, int col)
{
	int i, j, flag;
	for(i=1;i<=row;i++)
	{
		for(j=1;j<=n;j++)
		{
			int k = abs(row - i);
			int l = abs(col - n);
			if(a[i][col] != 'Q' && a[k][l] != 'Q')
			{
				flag =1;
			}
			else
			{
				flag =0;
				break;
			}
		}
	}
	return flag;
}




What I have tried:

I tried to google it, and I got the point. But on the other N Queen Program is way different because of the row and column must be a user input.
Posted
Updated 20-Sep-16 3:24am
v5
Comments
[no name] 20-Sep-16 8:30am    
Your teacher can help you if you do not understand your homework assignment. That is their job so you should ask them.
[no name] 20-Sep-16 8:55am    
Posting updated 5 times already and still no sign of an actual question.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we may help.

[Update]
You should return something in functions that are not of type void.
 
Share this answer
 
v2
You're trying to do three things at once which is never a good idea.

You essentially need to know the answer to three questions:

Is there a queen in the same column?
Is there a queen in the same row?
Is there a queen on a diagonal?

The best approach is to write separate methods for those, so your SafeToPlace() can then look something like:

C++
if (QueensInColumn() == 0 && QueensInRow() == 0 && QueensOnDiagonal() == 0)
     return 1;

return 0;


There are a great many advantages to breaking code down into discreet elements (you will probably come across the Single Responsibility Principle at some point) not least of which is that it makes it an awful lot easier to debug your code. In this example, if it keeps getting it wrong for the diagonal but works for rows and columns, you know exactly where to look and you won't find yourself changing the bits that are already working.

Another hint for easier debugging (and all round better code) is to use more meaningful variable names. Use of labels like a, i, j, k, l etc. make your code very hard to read and code that is hard to read is hard to maintain.

As a third point, you seem to be trying to apply 1-based indexes to a 0-based index language and confusing yourself in the process. 0-base everything and things should get a bit clearer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900