Please help to make the program working. Can not compile. import java.io.*; impo
ID: 3759692 • Letter: P
Question
Please help to make the program working. Can not compile.
import java.io.*;
import java .util.*;
public class Puzzlee
{
static int NN = 9; // Grid Size
// sample input
static int myGrid[][] = { {0,0,0,1,0,5,0,6,8},
{0,0,0,0,0,0,7,0,1},
{9,0,1,0,0,0,0,3,0},
{0,0,7,0,2,6,0,0,0},
{5,0,0,0,0,0,0,0,3},
{0,0,0,8,7,0,4,0,0},
{0,3,0,0,0,0,8,0,5},
{1,0,5,0,0,0,0,0,0},
{7,9,0,4,0,1,0,0,0}
};
static class myCell
{
int myRow, myColumn;
public myCell(int myRow, int myColumn)
{
super();
this.myRow = myRow;
this.myColumn = myColumn;
}
@Override
public String mytoString()
{
return "myCell [myRow=" + myRow + ", myColumn=" + myColumn + "]";
}
};
static boolean ValidorNot(myCell cell, int value)
{
if (myGrid[cell.myRow][cell.myColumn] != 0)
{
throw new RuntimeException("Already Cell Value is present");
}
for (int c1 = 0; c1 < 9; c1++)
{
if (myGrid[cell.myRow][c1] == value)
//Return not true
return false;
}
// if present in myColumn, not true
for (int r1 = 0; r1 < 9; r1++)
{
if (myGrid[r1][cell.myColumn] == value)
//Return not true
return false;
}
// Grid Calculation
int ValueofX1 = 3 * (cell.myRow / 3);
int ValueofY1 = 3 * (cell.myColumn / 3);
int ValueofX2 = ValueofX1 + 2;
int ValueofY2 = ValueofY1 + 2;
for (int x = ValueofX1; x <= ValueofX2; x++)
for (int y = ValueofY1; y <= ValueofY2; y++)
if (myGrid[x][y] == value)
return false;
// Returns true if value isn't present
return true;
}
// simple function to_get the next cell
static myCell getCellValue(myCell CurrentValue)
{
int myRow = CurrentValue.myRow;
int myColumn = CurrentValue.myColumn;
// next cell => myColumn++
myColumn++;
//If myColumn is greater than 8 go to next myRow
if (myColumn > 8)
{
myColumn = 0;
myRow++;
}
if (myRow > 8)
//greater than eight go to next row
return null;
myCell next = new myCell(myRow, myColumn);
return next;
}
static boolean solv(myCell CurrentValue)
{
if (CurrentValue == null)
return true;
if (myGrid[CurrentValue.myRow][CurrentValue.myColumn] != 0)
{
return solv(getCellValue(CurrentValue));
}
// value is assigned to cell and checks for the solution
for (int i = 1; i <= 9; i++)
{
// Check for valid number
boolean valid = ValidorNot(CurrentValue, i);
//if number is not valid check for other numbers.
if (!valid)
continue;
//Value assigned in the myGrid
myGrid[CurrentValue.myRow][CurrentValue.myColumn] = i;
boolean Solution = solv(getCellValue(CurrentValue));
// check for the value
if (Solution)
return true;
else
//values are resetted
myGrid[CurrentValue.myRow][CurrentValue.myColumn] = 0;
}
return false;
}
public static void main(String[] args)
{
boolean Solution = solv(new myCell(0, 0));
if (!Solution)
{
System.out.println("Solution does not exist for the puzzle.");
return;
}
System.out.println("Puzzle Solution ");
DispayGrid(myGrid);
}
static void DispayGrid(int myGrid[][])
{
for (int myRow = 0; myRow < NN; myRow++)
{
for (int myColumn = 0; myColumn < NN; myColumn++)
System.out.print(myGrid[myRow][myColumn]);
System.out.println();
}
}
}
Explanation / Answer
import java.io.*;
import java .util.*;
public class Puzzlee
{
static int NN = 9; // Grid Size
// sample input
static int myGrid[][] = { {0,0,0,1,0,5,0,6,8},
{0,0,0,0,0,0,7,0,1},
{9,0,1,0,0,0,0,3,0},
{0,0,7,0,2,6,0,0,0},
{5,0,0,0,0,0,0,0,3},
{0,0,0,8,7,0,4,0,0},
{0,3,0,0,0,0,8,0,5},
{1,0,5,0,0,0,0,0,0},
{7,9,0,4,0,1,0,0,0}
};
static class myCell
{
int myRow, myColumn;
public myCell(int myRow, int myColumn)
{
super();
this.myRow = myRow;
this.myColumn = myColumn;
}
@Override
public String mytoString()
{
return "myCell [myRow=" + myRow + ", myColumn=" + myColumn + "]";
}
};
static boolean ValidorNot(myCell cell, int value)
{
if (myGrid[cell.myRow][cell.myColumn] != 0)
{
throw new RuntimeException("Already Cell Value is present");
}
for (int c1 = 0; c1 < 9; c1++)
{
if (myGrid[cell.myRow][c1] == value)
//Return not true
return false;
}
// if present in myColumn, not true
for (int r1 = 0; r1 < 9; r1++)
{
if (myGrid[r1][cell.myColumn] == value)
//Return not true
return false;
}
// Grid Calculation
int ValueofX1 = 3 * (cell.myRow / 3);
int ValueofY1 = 3 * (cell.myColumn / 3);
int ValueofX2 = ValueofX1 + 2;
int ValueofY2 = ValueofY1 + 2;
for (int x = ValueofX1; x <= ValueofX2; x++)
for (int y = ValueofY1; y <= ValueofY2; y++)
if (myGrid[x][y] == value)
return false;
// Returns true if value isn't present
return true;
}
// simple function to_get the next cell
static myCell getCellValue(myCell CurrentValue)
{
int myRow = CurrentValue.myRow;
int myColumn = CurrentValue.myColumn;
// next cell => myColumn++
myColumn++;
//If myColumn is greater than 8 go to next myRow
if (myColumn > 8)
{
myColumn = 0;
myRow++;
}
if (myRow > 8)
//greater than eight go to next row
return null;
myCell next = new myCell(myRow, myColumn);
return next;
}
static boolean solv(myCell CurrentValue)
{
if (CurrentValue == null)
return true;
if (myGrid[CurrentValue.myRow][CurrentValue.myColumn] != 0)
{
return solv(getCellValue(CurrentValue));
}
// value is assigned to cell and checks for the solution
for (int i = 1; i <= 9; i++)
{
// Check for valid number
boolean valid = ValidorNot(CurrentValue, i);
//if number is not valid check for other numbers.
if (!valid)
continue;
//Value assigned in the myGrid
myGrid[CurrentValue.myRow][CurrentValue.myColumn] = i;
boolean Solution = solv(getCellValue(CurrentValue));
// check for the value
if (Solution)
return true;
else
//values are resetted
myGrid[CurrentValue.myRow][CurrentValue.myColumn] = 0;
}
return false;
}
public static void main(String[] args)
{
boolean Solution = solv(new myCell(0, 0));
if (!Solution)
{
System.out.println("Solution does not exist for the puzzle.");
return;
}
System.out.println("Puzzle Solution ");
DispayGrid(myGrid);
}
static void DispayGrid(int myGrid[][])
{
for (int myRow = 0; myRow < NN; myRow++)
{
for (int myColumn = 0; myColumn < NN; myColumn++)
System.out.print(myGrid[myRow][myColumn]);
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.