Question

In: Computer Science

In Java please You are given a matrix of characters representing a big box. Each cell...

In Java please You are given a matrix of characters representing a big box. Each cell of the matrix contains one of three characters: " / "which means that the cell is empty; '*', which means that the cell contains an obstacle; '+', which means that the cell contains a small box. You decide to rotate the big box clockwise to see how the small boxes will fall under the gravity. After rotating, each small box falls down until it lands on an obstacle, another small box, or the bottom of the big box. Given box, a matrix representation of the big box, your task is to return the state of the box after rotating it clockwise.

Solutions

Expert Solution

CODE:

package bigbox;

import java.util.Scanner;

public class BigBox
{
    public static void main(String[] args)
    {
        //variable to store number of rows and columns and taking it input from user
        int row,col;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        row = scan.nextInt();
        System.out.print("Enter the number of columns: ");
        col = scan.nextInt();
        //defining a char array of the given size
        char[][] arr = new char[row][col];
        for(int i=0;i<row;i++)
            for(int j=0;j<col;j++)
                arr[i][j]=scan.next().charAt(0);
        //calling the rotate big box function to rotate the box
        rotateBigBox(arr,row,col);
        //printing the new big box
        for(int i=0;i<row;i++)
        {
            System.out.println();
            for(int j=0;j<col;j++)
                System.out.print(arr[i][j]);
        }
    }
    //this function rotates the big box
    private static void rotateBigBox(char[][] arr, int row, int col)
    {
        //for loop to iterate rows
        for(int i=0;i<row;i++)
        {   //for loop to iterate column by colum value of a row from right to left
            for(int j=col-2;j>=0;j--)
            {
                //if we find a small box
                if(arr[i][j]=='+')
                {
                    // we define a variable 'k' which is 1 more than curent 'j'
                    int k=j+1;
                    //we iterate using k till we reach end of the big box and till we are finding '/' at the k-th place
                    while(k<col && arr[i][k]=='/')
                    {
                        //we swap if we find '/' in k-th place.
                        if(arr[i][k]=='/')
                        {
                            arr[i][k]='+';
                            arr[i][k-1]='/';
                        }
                        //we increment value of k
                        k++;
                    }
                }
            }
        }
        //rotating the box clockwise
        for (int i = 0; i < row / 2; i++)
        {
            for (int j = i; j < col - i - 1; j++)
            {
                char temp = arr[i][j];
                arr[i][j] = arr[row - 1 - j][i];
                arr[row - 1 - j][i] = arr[row - 1 - i][col - 1 - j];
                arr[row - 1 - i][row - 1 - j] = arr[j][col - 1 - i];
                arr[j][row - 1 - i] = temp;
            }
        }
    }  
}


OUTPUT:

Enter the number of rows: 4
Enter the number of columns: 4
+ / + /
+ / * +
* * + /
+ + + /

/ * / /
+ * + /
+ / * +
+ + + +

Ask any question if you have in comment section below.

Please rate the answer.


Related Solutions

Java. Given an input file with each line representing a record of data and the first...
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have...
(3) Write elimination matrix representing each step in elimination process. ( not augmented matrix) (a) 2x...
(3) Write elimination matrix representing each step in elimination process. ( not augmented matrix) (a) 2x + y − z = 3 x + 3y − z = 6 3x − y + 7z = 8 b) x+y−z−t=0 x + y + 2z − t = 3 2x + 2y + z + 3t = 13 x + 2y + 5z − 7t = 3
AVL trees in Java For a given adjacency matrix of a rooted tree you need to...
AVL trees in Java For a given adjacency matrix of a rooted tree you need to decide whether this tree can be labeled to become an avl tree. Vertices of the tree do not have keys but numbered from 0 to n − 1 in order to write the adjacency matrix. INPUT format: The input consists of blocks. Blocks are written one after another without empty lines between them. Every block starts with a new line and consists of several...
Find the matrix A representing the follow transformations T. In each case, check that Av =...
Find the matrix A representing the follow transformations T. In each case, check that Av = T(v) T(T(x,y,z)) where T(x,y,z)=(x-3y+4z, 6x-2z, 8x-y-4z)
Find the matrix A representing the follow transformations T. In each case, check that Av =...
Find the matrix A representing the follow transformations T. In each case, check that Av = T(v) Step by step please. A. T(x,y,z) = (x-3y+4z, 6x-2z, 8x-y-4z) B. T(x,y) = (x,y,y-x,x+y, 6x-9y) Thank you!
In Java Describe an algorithm that given a matrix described below determines whether matrix contains a...
In Java Describe an algorithm that given a matrix described below determines whether matrix contains a value k. The input matrix A[1...n, 1...n] has all rows and columns arranged in an non-descending order A[i, j] < A[i, j+1], A[j, i] < A[j + 1, i] for all 1 < i < n and 1 < j < n
java You are creating an array where each element is an (age, name) pair representing guests...
java You are creating an array where each element is an (age, name) pair representing guests at a dinner Party. You have been asked to print each guest at the party in ascending order of their ages, but if more than one guests have the same age, only the one who appears first in the array should be printed. Example input: (23, Matt)(2000, jack)(50, Sal)(47, Mark)(23, will)(83200, Andrew)(23, Lee)(47, Andy)(47, Sam)(150, Dayton) Example output: (23, Matt) (47, Mark) (50, Sal...
Given the following utility matrix, representing the ratings, on a 1–5 star scale, of eight items,...
Given the following utility matrix, representing the ratings, on a 1–5 star scale, of eight items, a through h, by three users A, B, and C: a b c d e f g h A 4 5 5 1 3 2 B 3 4 3 1 2 1 C 2 1 3 4 5 3 After we normalize the matrix by subtracting from each nonblank entry the average value for its user, what is the cosine distance between users A...
Encrypt the given plaintext with the key. Built the key matrix (big size). And show how...
Encrypt the given plaintext with the key. Built the key matrix (big size). And show how you get each letter ciphertext (you can do that by making shapes and arrows in key matrix). No need to draw key matric again and again.    Show all your work. If you just write cipher text and key matric without showing (shapes and arrow on key matrix) that how you get the ciphertext then your marks will be deducted. key :  alphabets plaintext :   smartness
Assume that the monthly profits of a company are given in an array, each entry representing...
Assume that the monthly profits of a company are given in an array, each entry representing the profits for the corresponding month. Propose an algorithm to find a group of consecutive months where the profit was the highest. For example, the profits, in millions, for a year Jan-Dec is given by A=[2 -5 3 4 0 -1 5 8 -9 1 -2 0]; The highest profit was from the period March-August and the value is 19 million. Assume that the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT