Question

The following gives the backtracking algorithm in pseudo-code for a constraint satisfaction problem, where U is a set of unassigned variables, and A is the current partial assignment search(U,A) if (U) return A; remove a variable X from U; for (each value a in Xs domain) if (X-a is consistent with A) add X=a to A; res - search(U,A) if res !# false return res remove X=a from A; return false; A magic square of size N is an N x N square grid filled with distinct numbers from 1 to N2 such that the numbers in each row, in each column, as wel as the numbers in the main and secondary diagonals, all add up to the same value. Based on the above algorithm, write a program in C++ or Java to find a 3 × 3 magic square

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As per your requirement i have written code which fulfill all your requirements please follow it step by step.

import java.util.*;

import java.io.*;

class SquaresMatrixClass

{

int elementN;

int magicObject[][];

SquaresMatrixClass()

{

String filename = "nums.txt";

try

{

//Here we will create Scanner object to read from file

  

Scanner scannerObject= new Scanner(new File(filename));

  

//Actually here we will read first integer from file

elementN = scannerObject.nextInt();

magicObject = new int[elementN][elementN];

  

for(int w=0; w<elementN; w++)

for(int q=0; q<elementN; q++)

{

magicObject[w][q]=scannerObject.nextInt();

  

}

}

catch(Exception e)

{

System.out.println(e);

}

}

SquaresMatrixClass(int elementAObject[])

{

elementN= (int)Math.sqrt(elementAObject.length);

  

magicObject = new int[elementN][elementN];

  

int s=0;

  

for(int w=0;w<elementN;w++)

  

for(int q=0; q<elementN ;q++)

{

  

magicObject[w][q] = elementAObject[s];

s++;

}

}

int SumrightLeftDiagonal()

{

int elementSum=0;

  

for(int w=elementN-1;w>=0;w--)   

{

elementSum=elementSum+magicObject[w][elementN-1-w];

}

return elementSum;

}

int leftRightDiagonalSum()

{

int elementSum=0;

for(int w=elementN-1;w>=0;w--)   

{

elementSum=elementSum+magicObject[w][w];

}

return elementSum;

}

int Sumcolumn()

{

int elementSum=0;

for(int q=0;q<elementN;q++)

elementSum=elementSum+magicObject[0][q];

  

return elementSum;

}

int Sumrow()

{

int elementSum=0;

  

for(int q=0;q<elementN;q++)

  

elementSum=elementSum+magicObject[q][0];

  

return elementSum;

}

  

boolean correctnumber()

{

for(int w=0; w<elementN; w++)

for(int q=0; q<elementN;q++)

if(magicObject[w][q] < 1 || magicObject[w][q] > elementN*elementN) return false;

return true;

  

}

  

boolean validMagicSquare()

{

if(correctnumber() && Sumrow()==Sumcolumn() && Sumcolumn()== leftRightDiagonalSum() && leftRightDiagonalSum()== SumrightLeftDiagonal() ) return true;

  

else return false;

}

public String toString()

{

String stringContent="";

  

for(int w=0; w<elementN; w++)

{

for(int q=0; q<elementN; q++)

stringContent=stringContent+ magicObject[w][q]+" ";

stringContent=stringContent+"\elementN";

}

  

return stringContent;

}

}

public class MagicSquaresMatrix

{

public static void main(String elementAObject[])

{

SquaresMatrixClass matrixObject;

Scanner scannerObject= new Scanner(System.in);

int characterFlag=0;

while(characterFlag!=3)

{

  

System.out.println("1. Create Magic square from file ");

System.out.println("2. Create Magic square from input ");

System.out.println("3. Exit the program ");

characterFlag=scannerObject.nextInt();

if(characterFlag==1)

{

matrixObject= new SquaresMatrixClass();

System.out.println(matrixObject);

if(matrixObject.validMagicSquare())System.out.println("It is elementAObject magicObject Square ");

else System.out.println("It is not elementAObject magicObject Square ");

}

else if(characterFlag==2)

{

int elementN;

System.out.println("Enter size of square matrix");

elementN=scannerObject.nextInt();

int objectArray[] = new int[elementN*elementN];

  

System.out.println("Enter "+elementN*elementN+" elements ");

for(int w=0;w<elementN*elementN; w++)

objectArray[w]=scannerObject.nextInt();

matrixObject= new SquaresMatrixClass(objectArray);

System.out.println(matrixObject);

  

if(matrixObject.validMagicSquare())System.out.println("It is elementAObject magicObject Square ");

else System.out.println("It is not elementAObject magicObject Square ");

}

else if(characterFlag==3)

break;

}

}

}

Add a comment
Know the answer?
Add Answer to:
The following gives the backtracking algorithm in pseudo-code for a constraint satisfaction problem, where U is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an algorithm in to solve a problem of your interest and evaluate your algorithm's goodness....

    Write an algorithm in to solve a problem of your interest and evaluate your algorithm's goodness. (Python Language) 1. Clearly state what is the problem of your interest 2. Represent your algorithm in pseudo code. Note that each step in the algorithm must be a primitive with a clear definition without ambiguity for a computer (machine or human) to execute. For example, you can assume the step to \click on a html hyperlink" a primitive in your algorithm. You can...

  • Need help in c++ programming to output the lines in my code: if word if found:...

    Need help in c++ programming to output the lines in my code: if word if found:            cout << "'" << word << "' was found in the grid" << endl;            cout << "'" << word << "' was not found in the grid" << endl; The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board: Q W E R T A S D F G Z X C V B Y U A...

  • In this question, you will test, using a backtracking algorithm, if a mouse can escape from...

    In this question, you will test, using a backtracking algorithm, if a mouse can escape from a rectangular maze. To ensure consistency of design, start your solution with maze_start.c. The backtracking algorithm helps the mouse by systematically trying all the routes through the maze until it either finds the escape hatch or exhausts all possible routes (and concludes that the mouse is trapped in the maze). If the backtracking algorithm finds a dead end, it retraces its path until it...

  • i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transf...

    i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transformation of a sequence (or collection) of coloured disks. Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, cl. For example, the colour mapping might be: O-red, 1-yellow, 2-blue, 3-pink,. c-black For a given sequence of coloured disks eg.,0,1,2,3,4), at each time step (or iteration) you are only...

  • in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the...

    in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide missing logic for the class Queens that will enable it to create a two-dimensional array that...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • I need help as quick as possible, thanks beforehand. Please provide the test output The Lo...

    I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...

  • In terms of the programming language required, the algorithm needs to be written in pseudocode Dynamic Programming Consider the following problem based on the transformation of a sequence (or collect...

    In terms of the programming language required, the algorithm needs to be written in pseudocode Dynamic Programming Consider the following problem based on the transformation of a sequence (or collection) of coloured disks Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, c. For example, the colour mapping might be 0-red. 1-yellow, 2-blue, 3-pink. , c-black For a given sequence of coloured disks e.g., ( 0,1,2,3,4...

  • Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak...

    Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) {     p = new node;     p->key=x;     p->left=NULL;     p->right=NULL; } else {     //Cheak if the element to be inserted is smaller than the root.     if (x < p->key)     {       //call the function itself with new parameters.       insert(x,p->left);     }     //cheak if the alement to be inserted...

  • 18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one...

    18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one part to lab lesson 11. The entire lab will be worth 100 points. Lab lesson 11 part 1 is worth 100 points For part 1 you will have 80 points if you enter the program and successfully run the program tests. An additional 20 points will be based on the style and formatting of your C++ code. Style points The 20 points for coding...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT