QuestionQuestion

Transcribed TextTranscribed Text

PROBLEM: Given rectangular grid and the 3 types of pieces shown below, the object of Stretch is to place pieces in the grid so that they form connected path from the left side to the right side. . o c A piece cannot be rotated or flipped A piece can only connect to the last piece that was placed and one column to the right of that piece. A piece can connect only at tile with a circle and the tiles with the circles are the only tiles that are allowed to touch. All tiles of the connecting piece must be to the right of all previously placed pieces. A piece cannot be placed in the grid such that it would cover any part of another piece, cover a blocked cell, or extend beyond the grid. The first piece can only touch the starting side at one circle tile. The final piece can only touch the right side at one circle tile. Pieces are placed in alphabetical order. If piece does not fit, skip it and use the next piece that fits. When Piece Cis either used or skipped, then begin again with Piece A. Grid cells are numbered consecutively starting with in the upper left corner as shown We guarantee that ifa piece can be placed, then that will be the only location it can be placed. EXAMPLE: The following example is 6x 10 grid with starting cell of 11. There are blocked cells at cells 23 and 37. Piece A is placed at 11. Piece B can only be placed at 14. The next piece must connect at 26. Piece C cannot be placed at 26 because there is blocked cell at 37. Therefore, Piece C is skipped Piece A is placed at Location 26. The next piece must connect at 29. Piece B is placed at 29 and touches the right side at 40. Therefore, the path is ABAB. 1 2 3 4 5 6 7 8 9 10 o A o . 15 16 17 18 19 20 21 22 B o o A o o 30 31 32 33 34 35 36 38 B . 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 INPUT: There will be 5 lines of data Each line will contain the numbers r. c. s. n. followed by " numbers. r indicates the number of rows in the grid. c indicates the number of columns in the grid. indicates the starting cell number for the first piece. n indicates the number of blocked cells, and the next " numbers are the cells that are designated as blocked OUTPUT: Form a path from the starting cell on the left side of the grid to a single cell on the opposite edge of the grid using the algorithm above. Print the sequence of pieces that were used to form the path. SAMPLE INPUT 6 10 11 1 37 4 9 1 3 2 13 24 4 10 0 6 11 1 1 42 4 8 17 1 18 SAMPLE OUTPUT 1. ABAB 2. BBBA 3. ABCA 4. ABCBB 5. BAA TEST INPUT: 7 12 25 2 40 55 5 10 14214: 36 39 6 9 10 2 22 34 5 15 16 2 17 49 6 10 21 o TEST OUTPUT: 1. ACBAB 2. BBAA 3. ACBB 4. BABABA 5. ABCA

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Program {

    int currRow = -1;
    int currCol = -1;
    char currPiece = ' ';
    int[][] grid = null;

    public static void main(String[] args) {

       Program program = new Program();

       //String inputFile = "input1.txt";
       String inputFile = "input2.txt";

       Scanner scanner = null;
       try {
            scanner = new Scanner(new File(inputFile));

            int lineCount = 0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String[] split = line.split(" +");
                List<Integer> data = new ArrayList<>();
                for (String str : split) {
                   if (str != null && !str.trim().isEmpty()) {
                        data.add(Integer.parseInt(str.trim()));
                   }
                }
                lineCount++;
                System.out.println("" + lineCount + ". " + program.findPath(data));
            }
       } catch (FileNotFoundException e) {
            e.printStackTrace();
       }
    }

    int getRowIndex(int num, int r, int c) {
       return num / c;
    }

    int getColIndex(int num...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$23.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats