DC [28/05/2021]

MATRIX - Remove Apples & Oranges

Boundary Condition(s):

2 <= R, C <= 50

1 <= T <= R


Input Format:

The first line contains R and C separated by a space.

The next R lines, each contains C characters separated by a space.

The (R+2)nd line contains T.


Output Format:

The first R lines, each contains C characters separated by a space.


Example Input/Output 1:

Input:

5 6

A O O A O O

O A O A A A

A A O A A O

O A O O A O

O A A A O A

3


Output:

* * * * * *

A * * * * *

O O O * O O

A A O A A A

O A O A A O


Explanation:

T = 1 (4 apples and 2 oranges in the last row). After removing the apples, the matrix becomes

A * * * O *

O O O A A O

A A O A A A

O A O A A O

O A O O O O

T = 2 (1 apple and 5 oranges in the last row). After removing the oranges, the matrix becomes

* * * * * *

A O * * O *

O A O A A O

A A O A A A

O A O A A O

T = 3 (3 apples and 3 oranges in the last row). After removing the apples, the matrix becomes

* * * * * *

A * * * * *

O O O * O O

A A O A A A

O A O A A O


Example Input/Output 2:

Input:

7 5

A O A A O

A A A A O

A O O O O

O O A O A

A A A A A

A A O O A

A A O O A

7


Output:

* * * * *

* * * * *

* * * * *

* * A * *

* * A A *

A * O A O

A * A O O


PROGRAM IN C:


#include<stdio.h>

#include<stdlib.h>


int main()

{int R,C,T;

scanf("%d %d\n",&R,&C);

char mat[R][C];

for(int i=0;i<R;i++)

{

    for(int j=0;j<C;j++)

    {

        scanf("%c ",&mat[i][j]);

    }

}

scanf("%d",&T);

while(T!=0)

{

    int apples=0,oranges=0;

    for(int j=0;j<C;j++)

    {

        if(mat[R-1][j]=='A')apples++;

        if(mat[R-1][j]=='O')oranges++;

    }

    char check=(apples>=oranges)?'A':'O';

    for(int j=0;j<C;j++)

    {

        if(mat[R-1][j]==check)

        {mat[R-1][j]='*';

        for(int i=R-1;i>=1;i--)

        {

            char t=mat[i-1][j];

            mat[i-1][j]=mat[i][j];

            mat[i][j]=t;

        }

        }

    }

    T--;

}


for(int i=0;i<R;i++)

{

    for(int j=0;j<C;j++)

    {

        printf("%c ",mat[i][j]);

    }

    printf("\n");

}

}

Comments

Popular posts from this blog

alphabet matrix minimum sum path

C--Starter-part007---skillrack