Question

Please help to make the program working. Can not compile. import java.io.*; import java .util.*; public...

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\n");

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();

}

}

}

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

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\n");
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();
}
}
}

Add a comment
Know the answer?
Add Answer to:
Please help to make the program working. Can not compile. import java.io.*; import java .util.*; public...
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
  • Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of...

    Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of N-squared. I.E. No nested for loops in rowIsLatin and colIsLatin. Only nested loop allowed in goodSubsquare. public class Sudoku {               public String[][] makeSudoku(String s) {              int SIZE = 9;              int k = 0;              String[][] x = new String[SIZE][SIZE];              for (int i = 0; i < SIZE; i++) {                     for (int j = 0; j < SIZE; j++)...

  • JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths...

    JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths possible in this maze. The program uses recursion. Can someone please explain to me why the program will pick one path if multiple traversable paths are available? Why does the program choose one path over a different path? (I believe it has something to do with the recursion terminating when a solution is found but I'm not sure.) Thank you!!!! The codes: public class...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards...

    need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards in a hand. * * @author (your name) * @version (a version number or a date) */ public class CountRank { // instance variables - replace the example below with your own private int rankCount[]; private Hand hand; private int count; /** * Constructor for objects of class CountRank */ public CountRank(Hand h) { // initialise instance variables hand = h; rankCount = new...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

  • package week_3; import java.util.Scanner; import java.util.*; //import java.lang.*; //import java.io.*; /** Write a program that can...

    package week_3; import java.util.Scanner; import java.util.*; //import java.lang.*; //import java.io.*; /** Write a program that can help decide if a particular programming project is best solved using a Waterfall or Agile methodology. Your program should ask the user: • How many programmers will be on the team [ More than 30 programmers -> Waterfall ] • If there needs to be firm deadlines and a fixed schedule [ Yes - > Waterfall ] • If the programmers have experience in...

  • How can I split this program into two classes? import java.io.*; public class Quiz { static...

    How can I split this program into two classes? import java.io.*; public class Quiz { static LineNumberReader cin = new LineNumberReader(new InputStreamReader(System.in)); public static void main(String[] args) { int score = 0; final int NumberofQuestions = 5; int num;    System.out.println("\nWelcome to CSC 111 Java Quiz\n");    String[][] QandA = { {"All information is stored in the computer using binary numbers: ","true"}, {"The word \"Public\" is a reserved word: ","false"}, {"Variable names may begin with a number: ","false"}, {"Will the...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean...

    Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) ^ rowPuzzle.java:16: error: class, interface, or enum expected } //Java Program import java.util.*; // rowPuzzle helper function implementation // File: rowPuzzle.cpp // Header files section // function prototypes public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) { // base case // return true if the puzzle is solvable if (index == squares.size() - 1) {    return...

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