SlideShare a Scribd company logo
1 of 12
Download to read offline
Suppose we have a solution to the n_Queens problem instance in which n = 4. Can we extend the
solution to find a solution to the problem instance n = 5? Can we then use the solution for n = 4
and n = 5 to construct a solution to the instance n = 6 and continue this dynamic programming
approach to find a solution to any instance in which n > 4? Justify your answer.
Solution
solution
package com.qp.queens;
import java.util.Scanner;
public class QueensProblem {
public static void possibleChoices(int k) {
int[] choices = new int[k];
possibleChoices (choices, 0);
}
public static void possibleChoices(int[] queen, int no) {
int n = queen.length;
if (no == n) display(queen);
else {
for (int i = 0; i < n; i++) {
queen[no] = i;
if (isConsistentApproach(queen, no)) possibleChoices(queen, no+1);
}
}
}
public static void display(int[] queen) {
int z = queen.length;
for (int k = 0; k < z; k++) {
for (int j = 0; j < z; j++) {
if (queen[k] == j) System.out.print("p ");
else System.out.print("* ");
}
System.out.println();
}
System.out.println();
}
public static boolean isConsistentApproach(int[] queen, int no) {
for (int k = 0; k < no; k++) {
if (queen[k] == queen[no]) return false; // same column
if ((queen[k] - queen[no]) == (no - k)) return false; // same major diagonal
if ((queen[no] - queen[k]) == (no - k)) return false; // same minor diagonal
}
return true;
}
public static void main(String... arguments) {
Scanner scan=new Scanner(System.in);
System.out.println("enter the instance you want");
int choice=scan.nextInt();
QueensProblem.possibleChoices(choice);
}
}
output
enter the instance you want
5
p * * * *
* * p * *
* * * * p
* p * * *
* * * p *
p * * * *
* * * p *
* p * * *
* * * * p
* * p * *
* p * * *
* * * p *
p * * * *
* * p * *
* * * * p
* p * * *
* * * * p
* * p * *
p * * * *
* * * p *
* * p * *
p * * * *
* * * p *
* p * * *
* * * * p
* * p * *
* * * * p
* p * * *
* * * p *
p * * * *
* * * p *
p * * * *
* * p * *
* * * * p
* p * * *
* * * p *
* p * * *
* * * * p
* * p * *
p * * * *
* * * * p
* p * * *
* * * p *
p * * * *
* * p * *
* * * * p
* * p * *
p * * * *
* * * p *
* p * * *
enter the instance you want
6
* p * * * *
* * * p * *
* * * * * p
p * * * * *
* * p * * *
* * * * p *
* * p * * *
* * * * * p
* p * * * *
* * * * p *
p * * * * *
* * * p * *
* * * p * *
p * * * * *
* * * * p *
* p * * * *
* * * * * p
* * p * * *
* * * * p *
* * p * * *
p * * * * *
* * * * * p
* * * p * *
* p * * * *
enter the instance you want
7
p * * * * * *
* * p * * * *
* * * * p * *
* * * * * * p
* p * * * * *
* * * p * * *
* * * * * p *
p * * * * * *
* * * p * * *
* * * * * * p
* * p * * * *
* * * * * p *
* p * * * * *
* * * * p * *
p * * * * * *
* * * * p * *
* p * * * * *
* * * * * p *
* * p * * * *
* * * * * * p
* * * p * * *
p * * * * * *
* * * * * p *
* * * p * * *
* p * * * * *
* * * * * * p
* * * * p * *
* * p * * * *
* p * * * * *
* * * p * * *
p * * * * * *
* * * * * * p
* * * * p * *
* * p * * * *
* * * * * p *
* p * * * * *
* * * p * * *
* * * * * p *
p * * * * * *
* * p * * * *
* * * * p * *
* * * * * * p
* p * * * * *
* * * * p * *
p * * * * * *
* * * p * * *
* * * * * * p
* * p * * * *
* * * * * p *
* p * * * * *
* * * * p * *
* * p * * * *
p * * * * * *
* * * * * * p
* * * p * * *
* * * * * p *
* p * * * * *
* * * * p * *
* * * * * * p
* * * p * * *
p * * * * * *
* * p * * * *
* * * * * p *
* p * * * * *
* * * * * p *
* * p * * * *
* * * * * * p
* * * p * * *
p * * * * * *
* * * * p * *
* p * * * * *
* * * * * * p
* * * * p * *
* * p * * * *
p * * * * * *
* * * * * p *
* * * p * * *
* * p * * * *
p * * * * * *
* * * * * p *
* p * * * * *
* * * * p * *
* * * * * * p
* * * p * * *
* * p * * * *
p * * * * * *
* * * * * p *
* * * p * * *
* p * * * * *
* * * * * * p
* * * * p * *
* * p * * * *
* * * * p * *
* * * * * * p
* p * * * * *
* * * p * * *
* * * * * p *
p * * * * * *
* * p * * * *
* * * * * p *
* p * * * * *
* * * * p * *
p * * * * * *
* * * p * * *
* * * * * * p
* * p * * * *
* * * * * * p
* p * * * * *
* * * p * * *
* * * * * p *
p * * * * * *
* * * * p * *
* * p * * * *
* * * * * * p
* * * p * * *
p * * * * * *
* * * * p * *
* p * * * * *
* * * * * p *
* * * p * * *
p * * * * * *
* * p * * * *
* * * * * p *
* p * * * * *
* * * * * * p
* * * * p * *
* * * p * * *
p * * * * * *
* * * * p * *
* p * * * * *
* * * * * p *
* * p * * * *
* * * * * * p
* * * p * * *
* p * * * * *
* * * * * * p
* * * * p * *
* * p * * * *
p * * * * * *
* * * * * p *
* * * p * * *
* * * * * p *
p * * * * * *
* * p * * * *
* * * * p * *
* * * * * * p
* p * * * * *
* * * p * * *
* * * * * * p
* * p * * * *
* * * * * p *
* p * * * * *
* * * * p * *
p * * * * * *
* * * p * * *
* * * * * * p
* * * * p * *
* p * * * * *
* * * * * p *
p * * * * * *
* * p * * * *
* * * * p * *
p * * * * * *
* * * p * * *
* * * * * * p
* * p * * * *
* * * * * p *
* p * * * * *
* * * * p * *
p * * * * * *
* * * * * p *
* * * p * * *
* p * * * * *
* * * * * * p
* * p * * * *
* * * * p * *
* p * * * * *
* * * * * p *
* * p * * * *
* * * * * * p
* * * p * * *
p * * * * * *
* * * * p * *
* * p * * * *
p * * * * * *
* * * * * p *
* * * p * * *
* p * * * * *
* * * * * * p
* * * * p * *
* * * * * * p
* p * * * * *
* * * p * * *
* * * * * p *
p * * * * * *
* * p * * * *
* * * * p * *
* * * * * * p
* p * * * * *
* * * * * p *
* * p * * * *
p * * * * * *
* * * p * * *
* * * * * p *
p * * * * * *
* * p * * * *
* * * * p * *
* * * * * * p
* p * * * * *
* * * p * * *
* * * * * p *
* p * * * * *
* * * * p * *
p * * * * * *
* * * p * * *
* * * * * * p
* * p * * * *
* * * * * p *
* * p * * * *
p * * * * * *
* * * p * * *
* * * * * * p
* * * * p * *
* p * * * * *
* * * * * p *
* * p * * * *
* * * * p * *
* * * * * * p
p * * * * * *
* * * p * * *
* p * * * * *
* * * * * p *
* * p * * * *
* * * * * * p
* * * p * * *
p * * * * * *
* * * * p * *
* p * * * * *
* * * * * p *
* * * p * * *
* p * * * * *
* * * * * * p
* * * * p * *
* * p * * * *
p * * * * * *
* * * * * p *
* * * p * * *
* * * * * * p
p * * * * * *
* * p * * * *
* * * * p * *
* p * * * * *
* * * * * * p
* p * * * * *
* * * p * * *
* * * * * p *
p * * * * * *
* * p * * * *
* * * * p * *
* * * * * * p
* * p * * * *
* * * * * p *
* p * * * * *
* * * * p * *
p * * * * * *
* * * p * * *
* * * * * * p
* * * p * * *
p * * * * * *
* * * * p * *
* p * * * * *
* * * * * p *
* * p * * * *
* * * * * * p
* * * * p * *
* * p * * * *
p * * * * * *
* * * * * p *
* * * p * * *
* p * * * * *
if no is increasing the no of possible outcomes are increasing.so some cases it ends up with
infiniteloop.no problem if no is greaterthan 4.

More Related Content

More from seasonsnu

In anova, what is represented by the letter N a.) The sum of the .pdf
In anova, what is represented by the letter N a.) The sum of the .pdfIn anova, what is represented by the letter N a.) The sum of the .pdf
In anova, what is represented by the letter N a.) The sum of the .pdfseasonsnu
 
In a Davis U-tube experiment, you add a different strain of auxotroph.pdf
In a Davis U-tube experiment, you add a different strain of auxotroph.pdfIn a Davis U-tube experiment, you add a different strain of auxotroph.pdf
In a Davis U-tube experiment, you add a different strain of auxotroph.pdfseasonsnu
 
If HDL levels were given in the dataset as a continuous (quantitativ.pdf
If HDL levels were given in the dataset as a continuous (quantitativ.pdfIf HDL levels were given in the dataset as a continuous (quantitativ.pdf
If HDL levels were given in the dataset as a continuous (quantitativ.pdfseasonsnu
 
I dont really know what two factor analysis is All my study guid sa.pdf
I dont really know what two factor analysis is All my study guid sa.pdfI dont really know what two factor analysis is All my study guid sa.pdf
I dont really know what two factor analysis is All my study guid sa.pdfseasonsnu
 
During transcription, which of the following is NOT found associated .pdf
During transcription, which of the following is NOT found associated .pdfDuring transcription, which of the following is NOT found associated .pdf
During transcription, which of the following is NOT found associated .pdfseasonsnu
 
essay on elderly patients in rural areas having problems meeting hea.pdf
essay on elderly patients in rural areas having problems meeting hea.pdfessay on elderly patients in rural areas having problems meeting hea.pdf
essay on elderly patients in rural areas having problems meeting hea.pdfseasonsnu
 
Denatured protein has gone bad and should not be eaten has lost its.pdf
Denatured protein  has gone bad and should not be eaten  has lost its.pdfDenatured protein  has gone bad and should not be eaten  has lost its.pdf
Denatured protein has gone bad and should not be eaten has lost its.pdfseasonsnu
 
Define liquidity and why money is said to be the most liquid of all .pdf
Define liquidity and why money is said to be the most liquid of all .pdfDefine liquidity and why money is said to be the most liquid of all .pdf
Define liquidity and why money is said to be the most liquid of all .pdfseasonsnu
 
Compare and contrast eukaryotic and prokaryotic cells. Why do you th.pdf
Compare and contrast eukaryotic and prokaryotic cells. Why do you th.pdfCompare and contrast eukaryotic and prokaryotic cells. Why do you th.pdf
Compare and contrast eukaryotic and prokaryotic cells. Why do you th.pdfseasonsnu
 
CHAP 13    OIS 3440-STATQuestionA chi-square test for goodness.pdf
CHAP 13    OIS 3440-STATQuestionA chi-square test for goodness.pdfCHAP 13    OIS 3440-STATQuestionA chi-square test for goodness.pdf
CHAP 13    OIS 3440-STATQuestionA chi-square test for goodness.pdfseasonsnu
 
bind two or more ions or molecules and tras Rot them ORRosi e divecti.pdf
bind two or more ions or molecules and tras Rot them ORRosi e divecti.pdfbind two or more ions or molecules and tras Rot them ORRosi e divecti.pdf
bind two or more ions or molecules and tras Rot them ORRosi e divecti.pdfseasonsnu
 
26. On July 1, Telstar Corporation declared a $1.00 per share cash d.pdf
26. On July 1, Telstar Corporation declared a $1.00 per share cash d.pdf26. On July 1, Telstar Corporation declared a $1.00 per share cash d.pdf
26. On July 1, Telstar Corporation declared a $1.00 per share cash d.pdfseasonsnu
 
write a research paper about happy family novel by Wendy lee ..pdf
write a research paper about happy family novel by Wendy lee ..pdfwrite a research paper about happy family novel by Wendy lee ..pdf
write a research paper about happy family novel by Wendy lee ..pdfseasonsnu
 
why is it important to establish patient ownership of healthcare rec.pdf
why is it important to establish patient ownership of healthcare rec.pdfwhy is it important to establish patient ownership of healthcare rec.pdf
why is it important to establish patient ownership of healthcare rec.pdfseasonsnu
 
Which of the following is true about mitotic recombinationA. It o.pdf
Which of the following is true about mitotic recombinationA. It o.pdfWhich of the following is true about mitotic recombinationA. It o.pdf
Which of the following is true about mitotic recombinationA. It o.pdfseasonsnu
 
Which of the following explains how someone can have the genotype fo.pdf
Which of the following explains how someone can have the genotype fo.pdfWhich of the following explains how someone can have the genotype fo.pdf
Which of the following explains how someone can have the genotype fo.pdfseasonsnu
 
Which intermolecular porcess primarly drives the formation of a bila.pdf
Which intermolecular porcess primarly drives the formation of a bila.pdfWhich intermolecular porcess primarly drives the formation of a bila.pdf
Which intermolecular porcess primarly drives the formation of a bila.pdfseasonsnu
 
Which of the following is an example of a translocation a chromosom.pdf
Which of the following is an example of a translocation  a chromosom.pdfWhich of the following is an example of a translocation  a chromosom.pdf
Which of the following is an example of a translocation a chromosom.pdfseasonsnu
 
What is simplicity and how does it bear on the acceptability of a sc.pdf
What is simplicity and how does it bear on the acceptability of a sc.pdfWhat is simplicity and how does it bear on the acceptability of a sc.pdf
What is simplicity and how does it bear on the acceptability of a sc.pdfseasonsnu
 
What’s the difference between soluble and insoluble fiberWhy is f.pdf
What’s the difference between soluble and insoluble fiberWhy is f.pdfWhat’s the difference between soluble and insoluble fiberWhy is f.pdf
What’s the difference between soluble and insoluble fiberWhy is f.pdfseasonsnu
 

More from seasonsnu (20)

In anova, what is represented by the letter N a.) The sum of the .pdf
In anova, what is represented by the letter N a.) The sum of the .pdfIn anova, what is represented by the letter N a.) The sum of the .pdf
In anova, what is represented by the letter N a.) The sum of the .pdf
 
In a Davis U-tube experiment, you add a different strain of auxotroph.pdf
In a Davis U-tube experiment, you add a different strain of auxotroph.pdfIn a Davis U-tube experiment, you add a different strain of auxotroph.pdf
In a Davis U-tube experiment, you add a different strain of auxotroph.pdf
 
If HDL levels were given in the dataset as a continuous (quantitativ.pdf
If HDL levels were given in the dataset as a continuous (quantitativ.pdfIf HDL levels were given in the dataset as a continuous (quantitativ.pdf
If HDL levels were given in the dataset as a continuous (quantitativ.pdf
 
I dont really know what two factor analysis is All my study guid sa.pdf
I dont really know what two factor analysis is All my study guid sa.pdfI dont really know what two factor analysis is All my study guid sa.pdf
I dont really know what two factor analysis is All my study guid sa.pdf
 
During transcription, which of the following is NOT found associated .pdf
During transcription, which of the following is NOT found associated .pdfDuring transcription, which of the following is NOT found associated .pdf
During transcription, which of the following is NOT found associated .pdf
 
essay on elderly patients in rural areas having problems meeting hea.pdf
essay on elderly patients in rural areas having problems meeting hea.pdfessay on elderly patients in rural areas having problems meeting hea.pdf
essay on elderly patients in rural areas having problems meeting hea.pdf
 
Denatured protein has gone bad and should not be eaten has lost its.pdf
Denatured protein  has gone bad and should not be eaten  has lost its.pdfDenatured protein  has gone bad and should not be eaten  has lost its.pdf
Denatured protein has gone bad and should not be eaten has lost its.pdf
 
Define liquidity and why money is said to be the most liquid of all .pdf
Define liquidity and why money is said to be the most liquid of all .pdfDefine liquidity and why money is said to be the most liquid of all .pdf
Define liquidity and why money is said to be the most liquid of all .pdf
 
Compare and contrast eukaryotic and prokaryotic cells. Why do you th.pdf
Compare and contrast eukaryotic and prokaryotic cells. Why do you th.pdfCompare and contrast eukaryotic and prokaryotic cells. Why do you th.pdf
Compare and contrast eukaryotic and prokaryotic cells. Why do you th.pdf
 
CHAP 13    OIS 3440-STATQuestionA chi-square test for goodness.pdf
CHAP 13    OIS 3440-STATQuestionA chi-square test for goodness.pdfCHAP 13    OIS 3440-STATQuestionA chi-square test for goodness.pdf
CHAP 13    OIS 3440-STATQuestionA chi-square test for goodness.pdf
 
bind two or more ions or molecules and tras Rot them ORRosi e divecti.pdf
bind two or more ions or molecules and tras Rot them ORRosi e divecti.pdfbind two or more ions or molecules and tras Rot them ORRosi e divecti.pdf
bind two or more ions or molecules and tras Rot them ORRosi e divecti.pdf
 
26. On July 1, Telstar Corporation declared a $1.00 per share cash d.pdf
26. On July 1, Telstar Corporation declared a $1.00 per share cash d.pdf26. On July 1, Telstar Corporation declared a $1.00 per share cash d.pdf
26. On July 1, Telstar Corporation declared a $1.00 per share cash d.pdf
 
write a research paper about happy family novel by Wendy lee ..pdf
write a research paper about happy family novel by Wendy lee ..pdfwrite a research paper about happy family novel by Wendy lee ..pdf
write a research paper about happy family novel by Wendy lee ..pdf
 
why is it important to establish patient ownership of healthcare rec.pdf
why is it important to establish patient ownership of healthcare rec.pdfwhy is it important to establish patient ownership of healthcare rec.pdf
why is it important to establish patient ownership of healthcare rec.pdf
 
Which of the following is true about mitotic recombinationA. It o.pdf
Which of the following is true about mitotic recombinationA. It o.pdfWhich of the following is true about mitotic recombinationA. It o.pdf
Which of the following is true about mitotic recombinationA. It o.pdf
 
Which of the following explains how someone can have the genotype fo.pdf
Which of the following explains how someone can have the genotype fo.pdfWhich of the following explains how someone can have the genotype fo.pdf
Which of the following explains how someone can have the genotype fo.pdf
 
Which intermolecular porcess primarly drives the formation of a bila.pdf
Which intermolecular porcess primarly drives the formation of a bila.pdfWhich intermolecular porcess primarly drives the formation of a bila.pdf
Which intermolecular porcess primarly drives the formation of a bila.pdf
 
Which of the following is an example of a translocation a chromosom.pdf
Which of the following is an example of a translocation  a chromosom.pdfWhich of the following is an example of a translocation  a chromosom.pdf
Which of the following is an example of a translocation a chromosom.pdf
 
What is simplicity and how does it bear on the acceptability of a sc.pdf
What is simplicity and how does it bear on the acceptability of a sc.pdfWhat is simplicity and how does it bear on the acceptability of a sc.pdf
What is simplicity and how does it bear on the acceptability of a sc.pdf
 
What’s the difference between soluble and insoluble fiberWhy is f.pdf
What’s the difference between soluble and insoluble fiberWhy is f.pdfWhat’s the difference between soluble and insoluble fiberWhy is f.pdf
What’s the difference between soluble and insoluble fiberWhy is f.pdf
 

Recently uploaded

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

Suppose we have a solution to the n_Queens problem instance in which.pdf

  • 1. Suppose we have a solution to the n_Queens problem instance in which n = 4. Can we extend the solution to find a solution to the problem instance n = 5? Can we then use the solution for n = 4 and n = 5 to construct a solution to the instance n = 6 and continue this dynamic programming approach to find a solution to any instance in which n > 4? Justify your answer. Solution solution package com.qp.queens; import java.util.Scanner; public class QueensProblem { public static void possibleChoices(int k) { int[] choices = new int[k]; possibleChoices (choices, 0); } public static void possibleChoices(int[] queen, int no) { int n = queen.length; if (no == n) display(queen); else { for (int i = 0; i < n; i++) { queen[no] = i; if (isConsistentApproach(queen, no)) possibleChoices(queen, no+1); } } } public static void display(int[] queen) { int z = queen.length; for (int k = 0; k < z; k++) { for (int j = 0; j < z; j++) { if (queen[k] == j) System.out.print("p "); else System.out.print("* ");
  • 2. } System.out.println(); } System.out.println(); } public static boolean isConsistentApproach(int[] queen, int no) { for (int k = 0; k < no; k++) { if (queen[k] == queen[no]) return false; // same column if ((queen[k] - queen[no]) == (no - k)) return false; // same major diagonal if ((queen[no] - queen[k]) == (no - k)) return false; // same minor diagonal } return true; } public static void main(String... arguments) { Scanner scan=new Scanner(System.in); System.out.println("enter the instance you want"); int choice=scan.nextInt(); QueensProblem.possibleChoices(choice); } } output enter the instance you want 5 p * * * * * * p * * * * * * p * p * * * * * * p *
  • 3. p * * * * * * * p * * p * * * * * * * p * * p * * * p * * * * * * p * p * * * * * * p * * * * * * p * p * * * * * * * p * * p * * p * * * * * * * p * * * p * * p * * * * * * * p * * p * * * * * * * p * * p * * * * * * p * p * * * * * * p * p * * * * * * * p * p * * * * * * p * * * * * * p * p * * * * * * p * * p * * * * * * * p * * p * * p * * * * * * * * p
  • 4. * p * * * * * * p * p * * * * * * p * * * * * * p * * p * * p * * * * * * * p * * p * * * enter the instance you want 6 * p * * * * * * * p * * * * * * * p p * * * * * * * p * * * * * * * p * * * p * * * * * * * * p * p * * * * * * * * p * p * * * * * * * * p * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * p * * * p * * * p * * * * * * * * * * p * * * p * * * p * * * * enter the instance you want
  • 5. 7 p * * * * * * * * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * p * * * p * * * * * * * * * * * p * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * p * * * * * p * * * * * * * * p * * * p * * * * * * * * * * * * p * * * * p * * * * p * * * * * * * * * p *
  • 6. * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * * p * * p * * * * * * * * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * * * p * * * * p * * * * * * * * * * * * p * * * p * * * * * * * * p * * p * * * * * * * * * p * * * * * * * * p * * * p * * * p * * * * * * * * p * * * * * * * * * p * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * p * * * p * * * * * * * * * * p * * * p * * * * *
  • 7. * * * * * * p * * * * p * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * * p * * * * p * * * * * * * * * * * p * * p * * * * * * * * * p * * * * * * * * p * * * p * * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * * * * * * * * p * * p * * * * * * * * * * p
  • 8. * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * * * p * * * * p * * * * * * * * * * p * * * p * * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * * p * * * p * * * * * * * * p * * * * * * * * * p * * p * * * * * * * * * * * p * * * * p * * * * * p * * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * * * * * p * p * * * * * *
  • 9. * * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * * * * * * * * p * * * * p * * * p * * * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * * p * * * * * * * * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * * * p * * * * p * * * * p * * * * * * * * * * * p * * p * * * * * * * * p * * * p * * * * * * * * * * p * * * p * * * *
  • 10. * * * * * * p * * * p * * * p * * * * * * * * * * p * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * * * p * * * p * * * * p * * * * * * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * *
  • 11. * * * * * * p * * p * * * * * * * * * p * * * p * * * * p * * * * * * * * * p * * * * * * * * * p * * * * p * * * p * * * * * * * * * * p * * * p * * * * * * * * p * * * * * * * * p p * * * * * * * * * p * * * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * p * * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * * * * * * p p * * * * * * * * p * * * * * * * * p * *
  • 12. * p * * * * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * p * * * * * * * * p * * * * * * * * p * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * p * * * * * * * * * p * * * * * * * * * p * * * p * * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * p * * * * * * * * * * p * * * * p * * * * p * * * * p * * * * * * * * * * * p * * * * p * * * * p * * * * * if no is increasing the no of possible outcomes are increasing.so some cases it ends up with infiniteloop.no problem if no is greaterthan 4.