Submatrix – Four Same Corners

Submatrix – Four Same Corners: The program must accept a character matrix of size R*C and an integer K as the input. The program must find the first occurring K*K submatrix where the four corner elements are the same. Then the program must replace the top-left to bottom-right diagonal and the top-right to bottom-left diagonal elements of the submatrix with the asterisks. Finally, the program must print the modified character matrix as the output.
Note: In the given matrix, at least one K*K submatrix always has the same element in all four corners.

Boundary Condition(s):
3 <= R, C <= 50
2 <= K <= Minimum value between R and C

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)th line contains K.

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

Example Input/Output 1:
Input:
7 6
a d z d o t
t g y e g b
y d c d i c
s y x v a l
b g s f g f
u a c r p c
k v z n i h
4

Output:
a d z d o t
t * y e * b
y d * * i c
s y * * a l
b * s f * f
u a c r p c
k v z n i h

Explanation:
Here K = 4.
The 4*4 submatrix with the same element in all four corners is highlighted below.
a d z d o t
g y e g b
d c d i c
y x v a l
g s f g f
u a c r p c
k v z n i h
After replacing the elements in the top-left to bottom-right and the top-right to bottom-left diagonals with the asterisks, the matrix becomes
a d z d o t
y e* b
y d * * i c
s y * *a l
s f *f
u a c r p c
k v z n i h

Example Input/Output 2:
Input:
5 7
z n z X z G z
x X a M t M E
O n T f W V e
S K G X c b F
z U z r R O z
5

Output:
z n * X z G *
x X a * t * E
O n T f * V e
S K G * c * F
z U * r R O *

r,c=map(int,input().split())
L=[input().strip().split() for i in range(r)]
n=int(input())
temp1=-2
for i in range(r-n+1):
    for j in range(c-n+1):
        x=[]
        for k in range(i,i+n): x.append(L[k][j:j+n])
        if x[0][0]==x[0][-1]==x[-1][0]==x[-1][-1]:
            temp1,temp2=j,j+n-1
            for m in range(i,i+n):
                L[m][temp1],L[m][temp2]='*','*'
                temp1+=1;temp2-=1
            for i in L: print(*i)
            exit()


Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.