write a program that solves magic square puzzles using recursive backtracking.A
ID: 3628583 • Letter: W
Question
write a program that solves magic square puzzles using recursive backtracking.A magic square of order n is an n x n grid that is filled with the integers from 1 to n^2 such that all rows and columns add up to the same value.For a magic square of order n, the rows and columns should all sum to the value obtained by the following formula:magicsquare= n^3 + n / 2. For example, in a magic square of order 3, the rows and colums should sum to ( 3^3 + 3) / 2 = 15. For this problem only require that the rows and columns sum to this value.
I already wrote some method of this problem.Here is that.....
/*
* MagicSquare.java
*
*/
import java.util.*;
public class MagicSquare {
// the current contents of the cells of the puzzle values[r][c]
// gives the value in the cell at row r, column c
private int[][] values;
// the order (i.e., the dimension) of the puzzle
private int order;
/**
* Creates a MagicSquare object for a puzzle with the specified
* dimension/order.
*/
public MagicSquare(int order) {
values = new int[order][order];
this.order = order;
// Add code to this constructor as needed to initialize
// the fields that you add to the object.
}
/**
* This method should call the separate recursive-backtracking method
* that you will write, passing it the appropriate initial parameter(s).
* It should return true if a solution is found, and false otherwise.
*/
public boolean solve() {
// Replace the line below with your implementation of this method.
// REMEMBER: The recursive-backtracking code should NOT go here.
// See the comments above.
return false;
}
/**
* Displays the current state of the puzzle.
* You should not change this method.
*/
public void display() {
for (int r = 0; r < order; r++) {
printRowSeparator();
for (int c = 0; c < order; c++) {
System.out.print("|");
if (values[r][c] == 0)
System.out.print(" ");
else {
if (values[r][c] < 10) {
System.out.print(" ");
}
System.out.print(" " + values[r][c] + " ");
}
}
System.out.println("|");
}
printRowSeparator();
}
// A private helper method used by display()
// to print a line separating two rows of the puzzle.
private void printRowSeparator() {
for (int i = 0; i < order; i++)
System.out.print("-----");
System.out.println("-");
}
public static void main(String[] args) {
/*******************************************************
******************************************************/
Scanner console = new Scanner(System.in);
System.out.print("What order Magic Square would you like to solve? ");
int order = console.nextInt();
MagicSquare puzzle = new MagicSquare(order);
if (puzzle.solve()) {
System.out.println("Here's the solution:");
puzzle.display();
} else {
System.out.println("No solution found.");
}
}
}
Explanation / Answer
please rate - thanks
import java.util.*;
import java.text.*;
public class Magicsquare
{public static void main(String[] args)
{DecimalFormat threedigit = new DecimalFormat("#00 ");
String formattednumber;
int x=0,y,n,i,j,k=1,tot;
Scanner in=new Scanner(System.in);
do{
System.out.println("How large is your square? ");
n=in.nextInt();
if(n%2==0)
System.out.println("Must be an odd number and > 0! - retry");
}while(n%2!=1);
int [][]square = new int[n][n];
y=(n+1)/2-1;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{square[x][y]=k++;
if(k%n==1)
x=++x%n;
else
{y=++y%n;
if(--x<0)
x=n-1;
}
}
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{formattednumber= threedigit.format(square[i][j]);
System.out.print(formattednumber);
}
System.out.println();
}
System.out.println();
System.out.println("Row Totals: ");
for(i=0;i<n;i++)
{tot=0;
for(j=0;j<n;j++)
tot+=square[i][j];
System.out.println("Row "+i+" sum: "+tot);
}
System.out.println("Column Totals: ");
for(j=0;j<n;j++)
{tot=0;
for(i=0;i<n;i++)
tot+=square[i][j];
System.out.println("Column "+j+" sum: "+tot);
}
System.out.print("Main diagnol Total: ");
tot=0;
for(i=0;i<n;i++)
tot+=square[i][i];
System.out.println(tot);
System.out.print("Anti diagnol Total: ");
tot=0;
for(i=0;i<n;i++)
tot+=square[i][n-i-1];
System.out.println(tot);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.