Provide an implementable code using “Backtracking” to produce the following output….

Provide an implementable code using “Backtracking” to produce the following output.

Java
SAMPLE INPUT SAMPLE OUTPUT YES 1ee ee1 1e00

SAMPLE INPUT SAMPLE OUTPUT YES 1ee ee1 1e00

Expert Answer

answers

This is basically seems to be nqueen problem with number of quuens 4

Don't use plagiarized sources. Get Your Custom Essay on
Provide an implementable code using “Backtracking” to produce the following output….
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay

And since there is solution of nqueen problem (where n>4) so

output is “YES” –> indicating solution exists for given n

and there is n*n matrix(here 4*4 matrix or chessboard) containing the position of queens( 1 means quuen) on chessboard or matrix.

Here is full implemented code on “Backtacking” solution of nqueen in Java.

/* IMPORTANT: Multiple classes and nested static classes are supported */

/*
* uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;

//import for Scanner and other utility classes
import java.util.*;
*/

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
import java.util.*;
public class TestClass {

/* Sample code to perform I/O:
* Use either of these methods for input

//BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine(); // Reading input from STDIN
System.out.println(“Hi, ” + name + “.”); // Writing output to STDOUT

//Scanner
Scanner s = new Scanner(System.in);
String name = s.nextLine(); // Reading input from STDIN
System.out.println(“Hi, ” + name + “.”); // Writing output to STDOUT

*/

// Write your code here

static boolean isAttacked(int x,int y,int arr[][],int n){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (arr[x][j] == 1 ||arr[i][y] == 1) {
return true;
}
if (((i + j) == (x + y) || (i – j) == (x – y)) && arr[i][j] == 1) {
return true;
}
}
}
return false;
}

static boolean isValid(int arr[][],int n,int q){
if(q==0)
return true;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(isAttacked(i,j,arr,n))
{
continue;
}
arr[i][j]=1;
if(isValid(arr,n,q-1))
return true;
arr[i][j]=0;
}
}
return false;

}

public static void main(String args[]){

Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int arr[][]=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
arr[i][j]=0;
}
}
boolean flag=isValid(arr,n,n);
if(flag==false)
System.out.println(“Not possible”);
else{
System.out.println(“YES”);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print( arr[i][j]+” “);
}
System.out.println();
}
}
}

}

Top Grade Homework
Order NOW For a 10% Discount!
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents