Sort Digits in Submatrices: The program must accept an integer matrix of size R*C as the input. The matrix contains only digits from 1 to 9. The values of R and C are always divisible by 3. The program must find the 3*3 submatrices having all the digits from 1 to 9. Then the program must sort the digits within those submatrices in ascending order. Finally, the program must print the modified matrix as the output.
Boundary Condition(s):
3 <= R, C <= 48
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.
Output Format:
The first R lines, each contains C integer values separated by a space.
Example Input/Output 1:
Input:
6 6
8 8 4 1 8 5
4 9 7 9 7 2
5 3 3 6 3 4
1 7 2 6 2 3
8 6 3 7 6 4
5 9 4 3 5 1
Output:
8 8 4 1 2 3
4 9 7 4 5 6
5 3 3 7 8 9
1 2 3 6 2 3
4 5 6 7 6 4
7 8 9 3 5 1
Explanation:
In the given matrix, the 3*3 submatrices having all the digits from 1 to 9 are highlighted below
8 8 4 1 8 5
4 9 7 9 7 2
5 3 3 6 3 4
1 7 2 6 2 3
8 6 3 7 6 4
5 9 4 3 5 1
After sorting the digits within the submatrices in ascending order, the matrix becomes
8 8 4 1 2 3
4 9 7 4 5 6
5 3 3 7 8 9
1 2 3 6 2 3
4 5 6 7 6 4
7 8 9 3 5 1
Example Input/Output 2:
Input:
6 9
4 8 6 3 3 3 4 1 5
9 2 3 9 6 7 8 7 3
1 5 7 4 6 1 9 6 2
2 1 7 4 8 2 7 1 6
5 3 9 9 7 5 7 8 5
6 4 8 3 6 1 8 5 8
Output:
1 2 3 3 3 3 1 2 3
4 5 6 9 6 7 4 5 6
7 8 9 4 6 1 7 8 9
1 2 3 1 2 3 7 1 6
4 5 6 4 5 6 7 8 5
7 8 9 7 8 9 8 5 8
r,c=map(int,input().split())
m=[list(map(int,input().split()))for i in range(r)]
for i in range(0,r,3):
for j in range(0,c,3):
d=[]
for k in range(i,i+3):
for l in range(j,j+3):
d.append(m[k][l])
if len(set(d))==9:
d.sort()
r=0
for k in range(i,i+3):
for l in range(j,j+3):
m[k][l]=d[r]
r+=1
for i in m:
print(*i)
#include<stdio.h>
#include<stdlib.h>
int main() {
int R, C;
scanf("%d %dn", & R, & C);
int mat[R][C];
for (int row = 0; row < R; row++) {
for (int col = 0; col < C; col++) {
scanf("%d ", & mat[row][col]);
}
}
for (int row = 0; row < R; row += 3) {
for (int col = 0; col < C; col += 3) {
int digits[10] = {
0
};
for (int subRow = 0; subRow < 3; subRow++) {
for (int subCol = 0; subCol < 3; subCol++) {
int val = mat[subRow + row][subCol + col];
if (digits[val] >= 1) {
goto hasDigit;
}
digits[val]++;
}
}
for (int subRow = 0, dig = 1; subRow < 3; subRow++) {
for (int subCol = 0; subCol < 3; subCol++) {
mat[subRow + row][subCol + col] = dig++;
}
}
hasDigit: ;
}
}
for (int row = 0; row < R; row++) {
for (int col = 0; col < C; col++) {
printf("%d ", mat[row][col]);
}
puts("");
}
}
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(), m[][]=new int[r][c],a;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
m[i][j]=sc.nextInt();
for(int i=0;i<r;i+=3){
for(int j=0;j<c;j+=3){
Set<Integer> t=new HashSet<Integer>();
for(int ii=i;ii<i+3;ii++)
for(int jj=j;jj<j+3;jj++)
t.add(m[ii][jj]);
if (t.size()==9){
a=1;
for(int ii=i;ii<i+3;ii++)
for(int jj=j;jj<j+3;jj++)
m[ii][jj]=a++;
}
}
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
}
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int m,n;
std::cin>>m>>n;
int a[m][n];
int i,j,ind,jnd;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
std::cin>>a[i][j];
}
}
int p,flag;
for(ind=0;ind<m;ind+=3)
{
for(jnd=0;jnd<n;jnd+=3)
{
p=1;
flag=0;
std::unordered_map<int,int> out;
for(i=ind;i<ind+3;i++)
{
for(j=jnd;j<jnd+3;j++)
{
out[a[i][j]]++;
if(out[a[i][j]]==2)
{
flag++;
break;
}
}
if(flag==1)
{
break;
}
}
if(flag==0)
{
for(i=ind;i<ind+3;i++)
{
for(j=jnd;j<jnd+3;j++)
{
a[i][j]=p;
p++;
}
}
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
std::cout<<a[i][j]<<" ";
}
std::cout<<"n";
}
return 0;
}
Leave a Reply