Question

Need help with java In this program you will use a Stack to implement backtracking to solve Sudok...

need help with java

In this program you will use a Stack to implement backtracking to solve Sudoku puzzles.

Part I. Implement the stack class.

  •  Created a generic, singly-linked implementation of a Stack with a topPtr as the only instance variable. Implement the following methods only:

    public MyStack() //the constructor should simply set the topPtr to null public void push(E e)
    public E pop()

  •  Test your class thoroughly before using it within the soduku program

  • Part II. Create a class to keep track of a move

 Your move class will need to store the move (a digit 0-9) as well as the row and column of the move.

Part III. Write the program to solve the Sudoku puzzles.

  •  An input file, puzzles.txt, containing 10 unsolved puzzles is found on Pilot.

  •  Your program should use a 9 x 9 array of integers (NOT an arrayList) to store a puzzle.

  •  For each puzzle in the file:

    •  Read the puzzle name and the data for the puzzle from the file into this array. Blank spaces will be

      represented by 0’s in the file.

    •  Display the puzzle number that was read from the file and the unsolved puzzle. You may display the 0’s or display blanks, whichever you prefer. Use vertical and horizontal separators as shown on the sample output on the next page.

    •  Solve the puzzle. The basic algorithm is:

      •  Move to the first square containing a 0. Replace the 0 with a digit 1-9. Be sure the digit is not

        already in the current row, column, or 3x3 section of the grid. Use a stack to keep track of all moves. Once you have placed a digit in that square, move to the next square containing a 0 and do the same.

      •  If your program is unable to find a digit for a particular square, you must backtrack (use the stack). Go to the previous square and change the digit there. If there is no other digit that can go there, change it back to a 0 and backtrack to the square previous to that square.

      •  All puzzles in the file have a solution.

    •  Display the solution to an output file (all solutions to the same file).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class MyStack<T> {

        private class Node {
                T data;
                Node next;
        }

        private Node topPtr;
        
        public MyStack() {
                topPtr = null;
        }

        public void push(T item) {
                Node node = new Node();
                node.data = item;
                node.next = topPtr;
                topPtr = node;
        }

        public boolean isEmpty() {
                return topPtr == null;
        }

        public T peek() {
                return topPtr.data;
        }

        public T pop() {
                T t = peek();
                topPtr = topPtr.next;
                return t;
        }

        public String toString() {
                String result = "TOP: ";
                Node temp = topPtr;
                while (temp != null) {
                        result += temp.data + " | ";
                        temp = temp.next;
                }
                return result;
        }

        public void clear() {
                topPtr = null;
        }
}

This is a very big question, and would require solutions in part.. i am first coding a stack which has been asked in your first part of the question. i would request you to please post separate questions one by one by giving the code available from previous sections, so that we can solve it in the given time limit. Thanks!
Add a comment
Know the answer?
Add Answer to:
Need help with java In this program you will use a Stack to implement backtracking to solve Sudok...
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 a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as...

    Programming Language: JAVA Construct a program that uses an agent to solve a Sudoku puzzle as a Constraint Satisfaction Problem, with the following guidelines: 1. Since 3 x 3 puzzles are too trivial for a computer, your program should use 4 x 4 puzzles (also known as Super Sudoku puzzles; see Figure 2 for an example). 2. The program should read a Sudoku puzzle from a text file. The user should be able to browse the file system to select...

  • dont use a struct use the std::stack library In this assignment, you will finish the implementation...

    dont use a struct use the std::stack library In this assignment, you will finish the implementation of an iterative solution to the Towers of Hanoi puzzle. Specifically, you will implement the puzzle initialization and the move operation utilizing the stacks provided. Do not rename existing functions, nor add any additional functions nor member variables! REQUIREMENTS The program will read in a text file with containing a single integer: the number of disks for the puzzle. The program will output the...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • Implement the EasyStack interface with the MyStack class. You can use either a linked list or...

    Implement the EasyStack interface with the MyStack class. You can use either a linked list or a dynamic array to implement the data structure. A stack is a specialised form of list in which you can only get and remove the element most recently added to the stack. The class should be able to work with the following code: EasyStack stack = new MyStack(); NB: You cannot import anything from the standard library for this task. The data structure must...

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here }...

    Starting code: #include <stdio.h> #include <stdbool.h> #include <assert.h> bool checkSudoku(int sudoku[9][9]) { //code goes here } int main() { int sudoku[9][9] = {{7, 3, 5, 6, 1, 4, 8, 9, 2}, {8, 4, 2, 9, 7, 3, 5, 6, 1}, {9, 6, 1, 2, 8, 5, 3, 7, 4}, {2, 8, 6, 3, 4, 9, 1, 5, 7}, {4, 1, 3, 8, 5, 7, 9, 2, 6}, {5, 7, 9, 1, 2, 6, 4, 3, 8}, {1, 5, 7, 4,...

  • You will write the following files: mystack.h - contains the class definition for the mystack class....

    You will write the following files: mystack.h - contains the class definition for the mystack class. mystack.cpp - contains the definitions for member functions of the mystack class. inpost.cpp - contains your convert() function. inpost.h - contains the function prototype for convert() so that the main() can call it. Each of the files (with the exception of inpost.h) is described in more detail below. All header files should contain header guards to prevent them from being included multiple times in...

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