I was solving the N Queen problem where we need to place 4 queens on a 4 X 4 chess board such that no two queens can attack each other. I tried this earlier but my approach did not involve backtracking, so I was trying again. The code snippets are
int size=4,i,j;
int arr[4][4];
int lastjindex[4]; // to store the last location which we may need to backtrack
void placeQueen(int i,int j)
{
int availableornot=0;
for(j=0;j<size;j++)
{
if(isAvailable(i,j)==1)
{
availableornot=1;
break;
}
}
if(availableornot==1)
{
arr[i][j]=1;
lastjindex[i]=j;
if((i+1)!=size)
{
placeQueen(i+1,0);
}
}
else
{
// no column was availabe so we backtrack
arr[i-1][lastjindex[i-1]]=0;
placeQueen(i-1,lastjindex[i-1]+1);
}
}
The isAvailable() method returns 1 if arr[i][j] is not under attack, else it returns 0.
int isAvailable(int i,int j)
{
int m,n,flag=0;
for(m=0;m<i;m++)
{
for(n=0;n<size;n++)
{
int k=abs(i-m);
int l=abs(j-n);
if(arr[m][j]==0 || arr[k][l]==0)
{
flag=1;
break;
// means that spot is available
}
}
}
return flag;
}
I call the above method from main as
placeQueen(0,0);
My program compiles successfully but it prints all zeroes.
Is there any problem with my recursion? Please help me correct my code as I am trying to learn how to implement backtracking algorithms!
Also I am not able to decide the base condition to end recursion. How do I choose it here?
There's no printing in the code you posted. If you print after you have backtracked, you will be back to the initial condition of no queens on the board. Print after you have placed N queens, which is also the end condition for recursion. If you only want to print one solution, exit after printing, or set a flag that tells the caller that you're done so you pop all the way out. If you print all solutions, that will include reflections and rotations. You can eliminate one axis of reflection by only placing queens within size/2 in the first level.
Also, there are some clear logic errors in you code, such as
arr[m][j]==0 || arr[k][l]==0
A queen can only be placed if it isn't attacked on the file and it isn't attacked along a diagonal. Use a debugger or add printfs to your code to trace where it is trying to place queens -- that will help you figure out what it is doing wrong.
And aside from being wrong, your isAvailable is very inefficient. You want to know if the [i,j] square is attacked along the file or a diagonal. For that you should have a single loop over the rows of the previous queens for (m = 0; m < i; m++), but you only need three tests, not a loop, to check the file and the diagonals. As soon as you find any previous queen on a file or diagonal, you're done, and the square isn't available -- return false. (And ignore people who tell you that a function should only have one return -- they are wrong, and there are lengthly discussions here at SO and even scientific studies of error rates in code that bear this out.) Only if no previous queen is found is the square available.
Your placeQueen is also wrong. For each available square on a row, you need to place a queen and then recurse, but you're just finding the first available square. And backtracking is achieved simply by removing the queen you placed and then returning ... the previous level of placeQueen will try the next available spot.
Again, trace the code to see what it's doing. And, even more importantly, think through the logic of what is needed. Write your algorithm in words, convince yourself that it will solve the problem, then write the code to carry out the algorithm.
#include <stdio.h>
#define SIZE 4
int size=SIZE;
int arr[SIZE][SIZE] = { 0 };
void placeQueen(int col){
int r,c;
if(col == size){//all queen put!
//print out
for(r = 0;r<size;++r){
for(c = 0;c<size;++c)
printf("%d", arr[c][r]);
printf("\n");
}
printf("\n");
return ;
}
for(r=0;r<size;++r){
if(isAvailable(col, r)==1){
arr[col][r]=1;
placeQueen(col+1);
arr[col][r]=0;//reset
}
}
}
int isAvailable(int col,int row){
int c;
for(c=0;c<col;++c){
int d = col - c;
if(arr[c][row]==1)
return 0;//queen already same row
if(row+d < size && arr[c][row+d]==1 || row-d >= 0 && arr[c][row-d]==1)
return 0;//queen already same slanting position
}
return 1;
}
int main(){
placeQueen(0);
return 0;
}
Related
I´m following the famous course cs50.
During a class, the teacher showed a program that printed out a pyramid of a height given as an input from the user:
#include <cs50.h>
#include <stdio.h>
void pyramid(int n);
int main (void)
{
int height = get_int("height:");
pyramid(height);
return 0;
}
void pyramid(int n)
{
if (n==0)
{
return;
}
pyramid(n-1);
for (int i=0; i<n;i++)
{
printf("#");
}
printf("\n");
}
Can please someone explain me, what the recursive function pyramid does?
I debug it and I see that given the input is checking if it is equalto 0, then it calls itself until n==0 and returns. After that the debugger goes to the loop for and does it n time.
Following a liner path, it is not supposed to go to the for loop.
Why it does it?
Thank you very much for your help!
After some value of height is set, then a function piramid is called. If the value of height is 0, then the function finishes. Otherwise it calls itself with decreased argument height, which allows you to print each level of the pyramide. As you can see in the body of void piramid(int n), it calls itself first, then prints n "blocks".
Let's say we are working with height = n and try to analyze what happens (each dot is another step):
piramid(n) calls:
piramid(n-1) calls:
piramid(n-2) calls:
...
piramid(1) calls:
piramid(0) - that returns nothing, it is the last recursive call of piramid, so we start going back: in our case it means we print hashes in the order from top to bottom:
# is printed, as piramid(1) is on top of the stack and after we print it, then we pop it, so piramid(2) becomes the first element on top
## are printed, piramid(2) is on top of the stack, we pop it and continue working with other calls the same way,
...
(n-1) # are printed, we pop it from the stack,
n # are printed, our stack is now empty.
To make it understandable, let's take an example with a small number :
#include <stdio.h>
void piramid (int n);
int
main (void)
{
piramid (2);
}
void
piramid (int n)
{
if (n == 0)
{
return;
}
piramid (n - 1);
for (int i = 0; i < n; i++)
{
printf ("#");
}
printf ("\n");
}
What it does:
main calls piramid(2)
piramid(2) calls piramid(1) however piramid(2) didn't finish as you stated
piramid(1) calls piramid(0)
piramid(0) does nothing as it returns immediately
piramid(1) finishes and prints #
piramid(2) finishes and prints ##
If n was bigger it would follow this but with more steps
Hope it could help and sorry if my answer isn't great as it's my first try answering on stackoverflow (if you have suggestions of how I should answer don't hesitate to tell me)
I'm assigned a program which will show a star pattern as follows:-
*
**
***
****
And then it will disappear from its place & then re-appear forming a motion like moving from left to right.
I have tried a lot. But, The best I could get was to move only the first star like this:-
*
**
***
****
The whole other part of pattern remains there.
I used delay function and gotoxy and clear screen along with for Loops but unable to do it the right way!
My pattern code:-
#include <stdio.h>
main()
{
char star='*';
int row, col;
int rowFinal;
printf("Enter the number of rows to be printed\n");
scanf("%d",&rowFinal);
for (row=1; row<=rowFinal; row++)
{
for (col=1; col<=row; col++)
{
printf("%c",star);
}
printf("\n");
}
}
Help me with remaining part I have messed it up won't be of any use to show it here.
This question is a bit ambiguous (please show code!), so I'm assuming when you say "assigned" you mean "assigned to make" not "given the program as a closed box and forced to work around what would be a fairly poor design choice if it is indeed impossible or difficult to edit".
Here is a rough framework for how to do it.
for (i=1; i<MAX AMOUNT OF STARS; i++) {
printf(INDENTATION);
//Code to print i stars and \n
}
The "//Code to print..." could be the code or the function call if by "assigned" you mean "given as closed box", but that works line by line.
If your problem is how to animate *, here is a small sample code with animates 1 star in 1 line:
#include <stdio.h>
#include <unistd.h>
#define SWEEP_WIDTH 20
int main()
{
int idx = 0, i = 0;
for (i = 0; i < 100; i++)
{
if (--idx < 0)
{
idx = SWEEP_WIDTH;
}
printf("%*s%*s\r", idx, "*", (SWEEP_WIDTH-idx), " ");
fflush(stdout);
usleep(50000);
}
I don't write the whole code for you, but I guess this should be somehow enough to get how you can animate stars in your code.
By the way, you can always code better if you spend time, this is result of 5 min coding, so you may be able to write it much better.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So, I have some class work that I cannot solve. I could get the code to work partially but now it is just useless. It either freezes (doesn't complete running) or it will give me several inf in the first row.
#include <stdio.h>
#include <math.h>
int main()
{
double a[10][11]={
{3.55618, 5.87317, 7.84934, 5.6951, 3.84642, 9.15038, -1.68539, 5.03067, 7.63384, -1.75626, -1.92193},
{-4.82893, 8.38177, -0.301221, 5.10182, -4.1169,-6.09145, -3.95675, -2.33365, 1.3969, 6.54555, -2.35262},
{-7.64196, 5.66605, 3.20481, 1.55619, -1.19814, 9.79288, 5.35547, 5.86109, 4.95544, -9.35749, 2.27709},
{-2.95914, -9.16958, 7.3216, 2.39876, -8.1302, -7.55135, -2.37718, 7.29694, 5.9867, 8.5401, -2.67493},
{-8.42043, -0.369407, -5.4102, -8.00545, 9.22153, 3.96454, 5.38499, 0.438365, 0.419677, 4.17166, 1.84756},
{6.02952, 4.57728, 5.46424, 3.52915, -1.01135, -3.74686, 8.14264, -8.86961, -2.88114, 1.29821, 4.154126},
{0.519819, -6.16655, 1.13216, 2.75811, -1.05975, 4.20286, -3.45764, 0.763558, -0.281287, -9.76168, -.93387},
{5.15737, -9.67481, 9.29904, -3.93334, 9.12785, -4.25208, -6.1652, 2.5375, 0.139195, 2.00106, -1.28356},
{-4.30784, 1.40711, -6.97966, -9.29715, 5.17234, 2.42634, 1.88818, -2.05526, -3.7679, 3.3708, -3.46841},
{-4.65418, 7.18118, 6.51338, 3.13249, 0.188456, -16.85599, 7.21435, -2.93417, 1.06061, 1.10807, -2.61529}};
int i, j, k, l;
double b[10][11];
i=0;
while(i<10)
{
j=0;
l=i;
while(l<10)
{
j=0;
l++;
while(j<11)
{
This code below works fine. When used on its own, it will turn all the diagonal values into 1.
b[i][j]=a[i][j]/a[i][i];
This code used below to work partially, setting the first column equal to 0, but now it is useless. I tried to manipulate it into getting rid of all values by having that 0 be a k, with k++ within the while code, but it would return either a segmentation code if I placed it by the i++; or a bus error is placed in front of the j=0;, and it would just freeze the program if I placed the i in the 0. Now it is useless that I restored it to default
b[l][j]=a[l][j]-b[i][j]*a[l][0];
j++;
}
}
i++;
}
j=0;
i=0;
while(i<10)
{
while(j<11)
{
printf("%lf\t", b[i][j]);
j++;
}
printf("\n");
i++;
j=0;
}
return 0;
}
How do I go about solving all these issues within my C program?
Not 100% sure what your code is up to, but I see at least 1 place where I believe that you are reading off the end of your arrays:
while(l<10)
{
j=0;
l++;
...
b[l][j]=a[l][j]-b[i][j]*a[l][0];
Because you are incrementing after you check for max size, you have 1 iteration where you are probably reading passed the max size of the array.
To check, put some prints on your indexes in your code where you do your assignments or assert them or something and I think you will find the source of your woes.
Also, PLEASE use constants for your sizes instead of the magic numbers all over the place. You will thank me later when trying to do a calculation of a matrix of a different size.
I have an assignment where I am supposed to use this very very simple (or so I thought) stack that my teacher wrote in C, just using an array. From this, I have to implement reverse polish notation from a text file.
In order for me to implement this, I am using a stack, pushing values on until I hit an operation. I then do the operation and push the result back onto the stack until the user hits p to print the value.
The problem is, for some reason, my professor's implementation of the stack array defaults the first (index 0) value to 0. Printing the stack without pushing anything onto it should result in null but it appears the output is 0.
Here is my professor's implementation of the stack:
#define STK_MAX 1024
#define ELE int
ELE _stk[STK_MAX];
int _top = 0;
void stk_error(char *msg)
{
fprintf(stderr, "Error: %s\n", msg);
exit(-1);
}
int stk_is_full()
{
return _top >= STK_MAX;
}
int stk_is_empty()
{
return _top == 0;
}
void stk_push(ELE v)
{
if ( stk_is_full() )
stk_error("Push on full stack");
_stk[_top++] = v;
}
ELE stk_pop()
{
if ( stk_is_empty() )
stk_error("pop on empty stack");
return _stk[--_top];
}
void print()
{
for(int i = 0; i <= _top; ++i)
printf("%d ", _stk[i]);
printf("\n");
}
I realize that the print statement will print a value that has not been pushed yet, but the problem is, is that when I don't print it, it still ends up there and it ends up screwing up my rpn calculator. Here is what happens when I do this:
// input
stk_push(2);
print();
stk_push(4);
print();
// output
2 0
2 4 0
How do I get rid of the 0 value that is affecting my calculator? Doing stk_pop() after the pushing the first value onto the stack didn't seem to work, and checking that top == 0, then directly inserting that element before incrementing _top didn't work.
When you are printing, loop from 0 to (_top - 1), since your top most element is actually at _top - 1. Hint : Look at your pop/push method.
void print()
{
for(int i = 0; i < _top; ++i)
printf("%d ", _stk[i]);
printf("\n");
}
"The problem is, is that the rpn calculator relies on the TOS being accurate. When I do pop() though, it will pop 0 and not the real TOS."
Sounds like a problem with your calculator implementation. You assumed the top of the stack would be null, but that's not the case for your professors stack implementation. Simply a invalid assumption.
Instead he's provided a stk_is_empty() method to help determine when you've pop everything.
If you need to pop all elements, you'll need to break on the condition of stk_is_empty().
stk_push(2);
stk_push(4);
while( stk_is_empty() == false)
{
stk_pop();
}
Of course in reality you'd be setting the pop return to a variable and doing something with it. The key point is leveraging stk_is_empty().
I haven't written C++ in few years so hopefully I didn't make a minor syntax error.
The program crashes in finding a number which is not available in the array.The code works perfectly when i search for elements which are available in the array.Help much appreciated.
#include<stdio.h>
int binarySearch(int a[],int s,int key)
{
int middle;
if(s!=1)
middle=s/2;
if(a[middle]==key)
return 1;
else if(key<a[middle])
binarySearch(a,middle,key);
else if(key>a[middle])
binarySearch(&a[middle],middle,key);
else
return 0;
}
void main()
{
int i;
int a[]={1,2,3,4,6,9,10,11};
for (i =0;i<8;i++)
printf("%i ",a[i]);
if(binarySearch(a,8,5))
printf("\nFound");
else
printf("\nNot Found");
}
Change
if(s!=1)
middle=s/2;
if(a[middle]==key)
return 1;
else if(key<a[middle])binarySearch(a,middle,key);
else if(key>a[middle])binarySearch(&a[middle],middle,key);
to
if (s != 1){
middle = s / 2;
if (a[middle] == key)
return 1;
else if (key<a[middle])binarySearch(a, middle, key);
else if (key>a[middle])binarySearch(&a[middle], middle, key);
}
The variable middle is initialized only if s!=1.
I have run this code and got the value Not Found for input 5.
If you are running your code in release mode, try building it in debug mode and run step by step you will see what happens when middle is used directly without assigning it a specific value. This is harmful.
Hope this helps.
The code if(key<a[middle])binarySearch(a,middle,key); does not return anything.
Try if(key<a[middle]) return binarySearch(a,middle,key);
This may still not work as you intend it to, but at least you will get past the major, immediately visible, cause of runaway recursion.
Because there is no case if the s == 1. "Middle" is not initialized and a[middle] is potential crash, or it will just go infinite.
A few notes:
Every branch of a recursive function should return something. You'll need to modify your recursive calls to return the call
Change
binarySearch(a, middle, key)
to
return binarySearch(a, middle, key)
Also, make sure middle is computed properly. You don't properly initialize it in the situation where s == 1. You'll want this to start at 0 most likely.