In this HackerEarth Image Smoothing, problem-solution Smoothing is used to reduce noise within an image or to produce a less pixelated image. You have been given an image G of resolution N x N. Image will be represented as a 2D grid G of size N x N where Gij will denote intensity of color in a grayscale image of pixel (i,j).

You have been given a filter mask F of size (2 * M + 1) x (2 * M + 1). Using this filter mask, you have to perform a smoothing operation on the G and output the final image NewG. Smoothing operation for any particular pixel (i,j) can be described in the formula.


HackerEarth Image Smoothing problem solution


HackerEarth Image Smoothing problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fre freopen("in.txt","r",stdin)
#define pii pair<pair<int,int>, int>
#define f first
#define s second
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
int I[111][111];
int Ip[111][111];
int N,M;
bool valid(int i,int j) {
    if(i>=1 and i<=N and j>=1 and j<=N) return 1;
    return 0;
}
int main() {
    freopen("in04.txt","r",stdin);
    freopen("out04.txt","w",stdout);
    int x;
    cin >> N >> M;
    map<pair<int,int>,int >F;
    for(int i=-M;i<=M;i++) {
        for(int j=-M;j<=M;j++) {
            cin >> x;
            F[{i,j}] = x;
        }
    }
    for(int i=1;i<=N;i++) {
        for(int j=1;j<=N;j++) {
            cin >> I[i][j];
        }
    }
    for(int i=1;i<=N;i++) {
        for(int j=1;j<=N;j++) {
            for(int p=-M;p<=M;p++) {
                for(int q=-M;q<=M;q++) {
                    if(valid(i+p,j+q))
                        Ip[i][j]+=I[i+p][j+q]*F[{p,q}];
                }
            }
        }
    }
    for(int i=1;i<=N;i++) {
        for(int j=1;j<=N;j++) {
            cout << Ip[i][j] << " ";
        }
        cout << "\n";
    }
}


Second solution

#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

#define y0 sdkfaslhagaklsldk

#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define have adsgagshdshfhds
#define ends asdgahhfdsfshdshfd

#define eps 1e-11
#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512

#define ldouble long double

using namespace std;

long long INF = 1e9;
const int N = 1001;

int n,m,board[N][N],coef[N][N];

int get(int a,int b)
{
  if (a<0||b<0)
    return 0;
  return board[a][b];
}

int main(){
  ios_base::sync_with_stdio(0);
  //cin.tie(0);

  cin>>n>>m;

  for (int i=0;i<=m*2;i++)
  {
    for (int j=0;j<=m*2;j++)
    {
      assert(cin>>coef[i][j]);
    }
  }

  for (int i=1;i<=n;i++)
  {
    for (int j=1;j<=n;j++)
    {
      assert(cin>>board[i][j]);
    }
  }

  for (int i=1;i<=n;i++)
  {
    for (int j=1;j<=n;j++)
    {
      int here=0;
      for (int q=0;q<=m*2;q++)
      {
        for (int w=0;w<=m*2;w++)
        {
          here+=get(i-m+q,j-m+w)*coef[q][w];
        }
      }
      if (j>1)
        cout<<" ";
      cout<<here;
    }
    cout<<endl;
  }

  cin.get(); cin.get();
  return 0;
}