Q20

#include<bits/stdc++.h>
#define MAXIMUM 500
#define N 3
#define M 4
using namespace std;
 
// Print the distance of nearest cell
// having 1 for each cell.
void printDistance(int mat[N][M])
{
    int ans[N][M];
 
    // Initialize the answer matrix with INT_MAX.
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            ans[i][j] = INT_MAX;
 
    // For each cell
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
        {
            // Traversing the whole matrix
            // to find the minimum distance.
            for (int k = 0; k < N; k++)
                for (int l = 0; l < M; l++)
                {
                    // If cell contain 1, check
                    // for minimum distance.
                    if (mat[k][l] == 1)
                        ans[i][j] = min(ans[i][j],
                             abs(i-k) + abs(j-l));
                }
        }
 
    // Printing the answer.
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
            cout << ans[i][j] << " ";
 
        cout << endl;
    }
}
 
// Driven Program
int main()
{
    int mat[N][M];
  for(int i=0;i<N;i++)
  {
    for(int j=0;j<M;j++)
    {
      cin>>mat[i][j];
    }
  }
 
    printDistance(mat);
 
    return 0;

1 comment:

SRM ELAB SOLUTUONS   DATA-STRUCTURE                                                                             **IF THE PROGRAM DON...