Header Ad

HackerEarth Special Matrix problem solution

In this HackerEarth Special Matrix problem solution, you are given a square matrix of size n (it will be an odd integer). Rows are indexed 0 to n-1 from top to bottom and columns are indexed 0 to n-1 from left to right. The matrix consists of only '*' and '.'. '*' appears only once in the matrix while all other positions are occupied by '.'

Your task is to convert this matrix to a special matrix by following any of two operations any number of times.

you can swap any two adjecent rows, i and i+1 (0<= i < n-1)

you can swap any two adjecent columns, j and j+1 (0<= j < n-1)

Special Matrix is one that contains '*' at the middle of the matrix. e.g following is a size 7 special matrix

    .......
    .......
    .......
    ...*...
    .......
    .......
    .......
Output no of steps to convert given matrix to special matrix.


HackerEarth Special Matrix problem solution


HackerEarth Special Matrix problem solution.

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<fstream>
#include<cstdlib>
#include<cassert>
#include<vector>
#include<algorithm>
#include<stack>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<ctime>
#define LL long long
#define ULL unsigned long long
#define F firs
#define S second
#define pb push_back
#define FOR(i,lb,ub) for(i=lb;i<=ub;i++)
#define RFOR(i,ub,lb) for(i=ub;i>=lb;i--)
#define FORS(it,v) for(it=v.begin();it!=v.end();it++)
#define st_clk double st=clock();
#define end_clk double en=clock();
#define show_time cout<<"\tTIME="<<(en-st)/CLOCKS_PER_SEC<<endl;
#define sc(n) scanf("%d",&n)
#define scst(n) scanf("%s",n)
#define f_in(st) freopen(st,"r",stdin);
#define f_out(st) freopen(st,"w",stdout);
LL gcd(LL a, LL b) { return b?gcd(b,a%b):a; }
using namespace std;
#ifndef ONLINE_JUDGE
inline int getchar_unlocked() { return getchar(); }
#endif
template <class T>
inline void r_f(T &p)
{
char c;
int neg=0;
while ((c=getchar_unlocked()) < 48 || c > 57)
if (c==45)
neg=1;
p=c-48;
while ((c=getchar_unlocked()) >= 48 && c <= 57) p=p*10+c-48;
if (neg) p*=-1;
}
int main()
{
#ifndef ONLINE_JUDGE
f_in("sample_in.txt");
//f_out("out.txt");
#endif
int t,i,j;
cin>>t;
while (t--)
{
int n,ans;
cin>>n;
int x , y;
char mat[100][100];
FOR(i,0,n-1)
{
cin>>mat[i];
FOR(j,0,n-1)
{
if (mat[i][j] == '*')
{
x = i;
y = j;
}
}
}
x = abs(x - n/2);
y = abs(y - n/2);
cout<<x+y<<endl;
}
return 0;
}

Post a Comment

0 Comments