Modify Submatrices Diagonally

 Modify Submatrices Diagonally 

PROBLEM STATEMENT :

The program must accept an integer matrix of size R*C and an integer K as the input. The values of R and C are always divisible by K. The given R*C matrix contains only 0s and 1s. The program must form an alphabet matrix of size (R/K)*(C/K) starting from "a" being present in the bottom left corner. Then the program must modify the submatrices of size K*K diagonally. In each integer submatrix of size K*K, all 1s must be replaced with the related upper case alphabet in the alphabet matrix and all 0s must be replaced with the related lower case alphabet in the alphabet matrix. If "z" has reached while filling the alphabet matrix then "a" must be considered as the next alphabet in circular fashion while forming the alphabet matrix. Finally, the program must print the integer matrix replaced with the corresponding alphabets as the output. 

Boundary Condition(s): 
2 <= R, C <= 50 

Input Format: 
The first line contains R and C separated by a space. 
The next R lines, each contains C integer values separated by a space. 
The (R+2)nd line contains K. 

Output Format: 
The first R lines, each contains C alphabets separated by a space. 

Example Input/Output 1: 
Input: () 
9 9 
0 0 1 0 0 0 0 1 1 
0 0 1 1 1 1 1 0 1 
0 1 1 1 1 1 0 1 1 
1 1 1 1 1 0 1 1 1 
0 1 1 0 0 0 1 1 0 
0 0 0 1 0 1 1 0 0 
0 0 1 1 0 1 1 1 1 
0 1 1 0 1 1 1 0 0 
0 0 1 1 1 1 0 1 1 
3 
Output: d d D g g g i I I d d D G G G I i I d D D G G G i I I B B B E E e H H H b B B e e e H H h b b b E e E H h h a a A C c C F F F a A A c C C F f f a a A C C C f F F

Explanation: 
Here R = 9, C = 9 and K = 3. 
So the size of the alphabet matrix to be formed is (9/3)*(9/3). 
The alphabet matrix of size 3*3 is given below. 
d g i 
b e h 
a c f 
In the above alphabet matrix, the alphabets starting from 'a' are filled diagonally in the top-left to bottom-right diagonals starting the bottom-left corner. 
After replacing the submatrices with their corresponding alphabets based on the given conditions, the matrix becomes 
d d D g g g i I I 
d d D G G G I i I 
d D D G G G i I I 
B B B E E e H H H 
b B B e e e H H h 
b b b E e E H h h 
a a A C c C F F F 
a A A c C C F f f 
a a A C C C f F F 



Example Input/Output 2: Input: ()
6 8 
1 0 1 0 0 1 0 0 
0 1 1 1 1 1 0 1 
0 0 0 0 1 0 1 0 
1 1 1 0 0 0 1 1 
1 0 1 1 1 0 0 0 
1 1 1 0 0 1 1 0 
2 
Output: D d G g j J l l d D G G J J l L b b e e H h K k B B E e h h K K A a C C F f i i A A C c f F I i



SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { char ch='a'; int r,c,i,j,k,i1,j1; scanf("%d%d",&r,&c); int a[r][c]; for(i=0;i<r;++i) { for(j=0;j<c;++j) { scanf("%d",&a[i][j]); } } scanf("%d",&k); i=r-k; j=0; while(j<c) { i1=i; j1=j; while(i1<r&&j1<c) { for(int i=i1;i<i1+k;++i) { for(int j=j1;j<j1+k;++j) { if(a[i][j]==0) a[i][j]=ch; else a[i][j]=toupper(ch); } } i1+=k; j1+=k; if(ch=='z') ch='a'; else ch++; } if(i>0) i-=k; else j+=k; } for(i=0;i<r;++i) { for(j=0;j<c;++j) { printf("%c ",a[i][j]); } printf("\n"); } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { char ch='a'; int r,c,i,j,i1,j1,k; cin>>r>>c; char a[r][c]; for(i=0;i<r;++i) { for(j=0;j<c;++j) { cin>>a[i][j]; } } cin>>k; i=r-k; j=0; while(j<c) { i1=i; j1=j; while(i1<r&&j1<c) { for(int i=i1;i<i1+k;++i) { for(int j=j1;j<j1+k;++j) { if(a[i][j]=='0') a[i][j]=ch; else a[i][j]=toupper(ch); } } if(ch=='z') ch='a'; else ch++; i1+=k; j1+=k; } if(i>0) i-=k; else j+=k; } for(i=0;i<r;++i) { for(j=0;j<c;++j) { cout<<a[i][j]<<" "; } cout<<"\n"; } }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int r=sc.nextInt(),c=sc.nextInt(),i,j,i1,j1,k,x,y; char[][] a=new char[r][c]; char ch='a'; for(i=0;i<r;++i) { for(j=0;j<c;++j) { a[i][j]=sc.next().charAt(0); } } k=sc.nextInt(); i=r-k; j=0; while(j<c) { i1=i; j1=j; while(i1<r&&j1<c) { for(x=i1;x<i1+k;x++) { for(y=j1;y<j1+k;y++) { if(a[x][y]=='0') a[x][y]=ch; else a[x][y]=Character.toUpperCase(ch); } } if(ch=='z') ch='a'; else ch++; i1+=k; j1+=k; } if(i>0) i-=k; else j+=k; } for(i=0;i<r;++i) { for(j=0;j<c;++j) { System.out.print(a[i][j]+" "); } System.out.println(); } } }



PYTHON

       

r,c=map(int,input().split()) x=['a'] l=[list(map(int,input().split())) for i in range(r)] copied=[l[i][:] for i in range(r)] k=int(input()) i=r-k;j=0 while j<c: i1=i j1=j while i1<r and j1<c: for a in range(i1,i1+k): l[a][j1:j1+k]=x*k i1+=k;j1+=k if x[0]=='z': x[0]='a' else: x[0]=chr(ord(x[0])+1) if i>0: i-=k else: j+=k for i in range(r): for j in range(c): if copied[i][j]==1: l[i][j]=l[i][j].upper() for i in l: print(*i)



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right