it just prints out the solution, but I want solution like For example, the outpu
ID: 3592996 • Letter: I
Question
it just prints out the solution, but I want solution like For example, the output for the input described above could be: 1 1 2 5 3 2 4 6 5 3 6 7 7 4 No solution How can I do that ?
Main objective: Solve the n queens problems. You have to place n queens on an n ×
n chessboard such that no two attack each other. Important: the chessboard should be
indexed starting from 1, in standard (x, y) coordinates. Thus, (4, 3) refers to the
square in the 4th column and 3rd row.
We have a slight twist in this assignment. We will take as input the position of
one queen, and have to generate a solution with a queen in this position.
Specifics: You should provide a Makefile. On running make, it should create
“NQueens.jar”. You should run the jar with two command line arguments: the first is
an input file, the second is the output file. For example, we would call the command:
java -jar NQueens.jar in.txt solution.txt
The file in.txt will have inputs to the main program (as described below), and
the file solution.txt will have the desired solution.
Each line of the input file corresponds to a different instance. Each line of the
input file will have three integers: the chessboard size, the column where the input
queen is placed, and the row where the input queen is placed. For example, the file
may look like:
7 3 2
4 1 1
The first line means we have a 7 × 7 chessboard, with one queen placed at (3, 2).
We wish to place 6 more queens so that none of the 7 queens attack any other. The
second line means we have a 4 × 4 chessboard, with one queen at (1, 1). We wish to
place 3 more queens without any attacks. So on and so forth.
Output: On running the command, the following should be printed in the output
file. For each line of the input file, there is a line in the output file with the placement 2
of queens.
• If there is no solution to the problem, print “No solution” (with a newline at the
end)
• If there is a solution, print the position of each queen as <column> <space>
<row> <space> followed by the position for the next queen. The positions
should be in increasing order of column, so first column first, second column
second, etc. Please follow this format exactly, so that the checking script works
for you. The last character on the line will be a space, then followed by a
newline. This format should make your code easier to write.
For example, the output for the input described above could be:
1 1 2 5 3 2 4 6 5 3 6 7 7 4
No solution
Explanation / Answer
// C++ program for activity selection problem.
// The following implementation assumes that the activities
// are already sorted according to their finish time
#include<stdio.h>
// Prints a maximum set of activities that can be done by a single
// person, one at a time.
// n --> Total number of activities
// s[] --> An array that contains start time of all activities
// f[] --> An array that contains finish time of all activities
void printMaxActivities(int s[], int f[], int n)
{
int i, j;
printf ("Following activities are selected n");
// The first activity always gets selected
i = 0;
printf("%d ", i);
// Consider rest of the activities
for (j = 1; j < n; j++)
{
// If this activity has start time greater than or
// equal to the finish time of previously selected
// activity, then select it
if (s[j] >= f[i])
{
printf ("%d ", j);
i = j;
}
}
}
// driver program to test above function
int main()
{
int s[] = {1, 3, 0, 5, 8, 5};
int f[] = {2, 4, 6, 7, 9, 9};
int n = sizeof(s)/sizeof(s[0]);
printMaxActivities(s, f, n);
return 0;
}
// C++ program for activity selection problem.
// The following implementation assumes that the activities
// are already sorted according to their finish time
#include<stdio.h>
// Prints a maximum set of activities that can be done by a single
// person, one at a time.
// n --> Total number of activities
// s[] --> An array that contains start time of all activities
// f[] --> An array that contains finish time of all activities
void printMaxActivities(int s[], int f[], int n)
{
int i, j;
printf ("Following activities are selected n");
// The first activity always gets selected
i = 0;
printf("%d ", i);
// Consider rest of the activities
for (j = 1; j < n; j++)
{
// If this activity has start time greater than or
// equal to the finish time of previously selected
// activity, then select it
if (s[j] >= f[i])
{
printf ("%d ", j);
i = j;
}
}
}
// driver program to test above function
int main()
{
int s[] = {1, 3, 0, 5, 8, 5};
int f[] = {2, 4, 6, 7, 9, 9};
int n = sizeof(s)/sizeof(s[0]);
printMaxActivities(s, f, n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.