Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need to solve a problem of placing N queens and M rooks on an (M+N)×(M+N) chessboard.
1. Each row and column of the chessboard contains exactly 1 queen , and each diagonal also contains no more than 1 queen
2.Each row and column of the chessboard contains no more than 1 rock.

I test my code on visual studio, it works when N=0, but if N>0, the result will be greater than the correct answer. I think it might be because placeQ calls placeR too many times or something wrong with vaildQ. Please tell me what is wrong with my program or any other ways to find correct answer.

What I have tried:

#include <stdio.h>
#include <stdlib.h>
int *Qcol;
int *Rcol;
static int answer=0;

int vaildQ(int row,int col);
void placeQ(int col,int num_queens,int M,int N);
int vaildR(int row,int col);
void placeR(int col,int num_rooks,int M,int N);

int main()
{
	int M,N,i;
	scanf("%d %d",&M,&N);
	Qcol=(int*)malloc(sizeof(int)*(M+N+1));
	for(i=0;i<=M+N;i++)
		Qcol[i]=0;

	Rcol=(int*)malloc(sizeof(int)*(M+N+1));
	for(i=0;i<=M+N;i++)
		Rcol[i]=0;
	placeQ(1,0,M,N);
	printf("%d\n",answer);
	system("pause");
}

int vaildQ(int row,int col)
{
	int i;
	for(i=0;i<col;i++)
	{
		if(Qcol[i]==row||row-Qcol[i]==col-i||col-i==Qcol[i]-row)
			return 0;
	}
	return 1;
}
void placeQ(int col,int num_queens,int M,int N)
{
	int i,j;
	if(num_queens==M)
	{		
		placeR(1,0,M,N);
	}
	else
	{
		for(j=col;j<=N+M;j++)
		{
			for(i=1;i<=M+N;i++)
			{
				if(vaildQ(i,j))
				{
					Qcol[j]=i;
					placeQ(j+1,num_queens+1,M,N);
					Qcol[j]=0;
				}
			}
		}
	}
}

int vaildR(int row,int col)
{
	int i;
	for(i=0;i<col;i++)
	{
		if(Rcol[i]==row)
		{
			return 0;
		}
	}
	if(Qcol[col]==row)
		return 0;

	return 1;
}
void placeR(int col,int num_rooks,int M,int N)
{
	int i,j;
	if(num_rooks==N)
	{
		answer++;
	}
	else
	{
		for(j=col;j<=M+N;j++)
		{
			for(i=1;i<=M+N;i++)
			{
				if(vaildR(i,j))
				{
					Rcol[j]=i;
					placeR(j+1,num_rooks+1,M,N);
					Rcol[j]=0;
				}
			}
		}
	}
}
Posted
Updated 8-Sep-21 21:36pm
v6
Comments
jeron1 8-Sep-21 13:30pm    
Many times you get an error due to array indexes being out of range, or your exit from recursion is incorrect.
[no name] 9-Sep-21 1:12am    
thanks!

Quote:
I test my code on visual studio, it works when N=0, but if N>0, the result will be greater than the correct answer. I think it might be because placeQ calls placeR too many times or something wrong with vaildQ.

Stop guessing, make sure with the debugger.
You can also start by printing every solution. It may help.
Try this, it may help
C++
void placeR(int col,int num_rooks,int M,int N)
{
	int i,j;
	if(num_rooks==N)
	{
		answer++;
		printf("Q ");
		for(i=0;i<=M+N;i++) {
		    printf("%d ",Qcol[i]);
		}
		printf("\nR ");
		for(i=0;i<=M+N;i++) {
		    printf("%d ",Rcol[i]);
		}
		printf("\n\n");
	}


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v3
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.

But in this case,m you need to learn the next step... because compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
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