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!
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...
In java please Question: You are given a string s. Your task is to count the...
In java please Question: You are given a string s. Your task is to count the number of ways of splitting s into three non-empty parts a, b and c (s = a + b + c) in such a way that a + b, b + c and c + a are all different strings. For s = "xzxzx", the output should be countWaysToSplit(s) = 5. Consider all the ways to split s into three non-empty parts:
Two data sets are given below, each representing times (in minutes) to complete a given task...
Two data sets are given below, each representing times (in minutes) to complete a given task for two different samples of students. Data Set A: 3.1, 4.7, 7.3, 8.1, 8.1, 8.7, 8.8, 9.1, 9.1, 9.2 Data Set B: 5.2, 5.6, 5.8, 6.9, 7.0, 7.7, 7.9, 8.5, 8.8, 12.5 Complete the back-to-back stem plot for data set B by adding the numbers that belong in each class below, using a class width of 1.  Do not enter spaces between the numbers. If...
Suppose you are given a string containing only the characters ( and ). In this problem,...
Suppose you are given a string containing only the characters ( and ). In this problem, you will write a function to determine whether the string has balanced parentheses. Your algorithm should use no more than O (1) space beyond the input; any algorithms that use space not in O (1) will receive half credit at most. Any solutions that always return true (or always return false) or otherwise try to game the distribution of test cases will receive zero...
Mammoths payoffs (profit) are shown in the lower half of each cell in the payoff matrix...
Mammoths payoffs (profit) are shown in the lower half of each cell in the payoff matrix below. Is this a zero-, negative-, or positive-sum game? What is each airline’s profit if Mammoth discount and Colossal doesn’t? If the airlines cooperate, which strategy is each likely to choose? In the absence of cooperation, does each player have a dominant strategy? Explain. What effect would it have on each player’s likely strategy that the game is to be repeated many times? Mammoth...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT