The Pattern

Joey is always fond of playing with patterns. So on his birthday, his uncle gave him a mat.  Mat is a rectangular grid

(NM). i.e. there are N rows and M columns are drawn on the mat.  Each cell of this grid is filled with either dot(.) or star(). Now Joey starts to fold this mat.  Firstly he folds the mat along the rows (top to down) until it transforms into a (1M) grid (Neglect width of mat on each fold). After that, he starts folding this along the columns (left to right) and finally transforms into a single cell. In the end Joey wants to know what will be on the top of that cell (dot (.)or star()).

When star() come over dot(.) it converted to dot(.)
When star() come over star(), it remains the star()
When dot(.) come over dot(.), it remains dot(.)
When dot(.) come over star(), it converted to star()


Example:

Input:  n = 5, m = 5 , pattern={{"*.***"},{".**.."},{".*.*."},{"*...*"},{"..*.*"}}
Output: *

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void thePattern(vector<stringpatternint nint m)
{
    for (int i = 0i < n - 1i++)
    {
        for (int j = 0j < mj++)
        {
            if (pattern[i][j] == '.' && 
pattern[i + 1][j] == '*')
                pattern[i + 1][j] = '*';
            else if (pattern[i][j] == '*' &&
 pattern[i + 1][j] == '.')
                pattern[i + 1][j] = '.';
        }
    }

    for (int j = 0j < m - 1j++)
    {
        if (pattern[n - 1][j] == '.' && 
pattern[n - 1][j + 1] == '*')
            pattern[n - 1][j + 1] = '*';
        else if (pattern[n - 1][j] == '*' &&
 pattern[n - 1][j + 1] == '.')
            pattern[n - 1][j + 1] = '.';
    }
    cout << pattern[n - 1][m - 1] << "\n";
}
int main()
{
    int n = 5m = 5;

    vector<stringpattern = {{"*.***"},
                              {".**.."},
                              {".*.*."},
                              {"*...*"},
                              {"..*.*"}};

    thePattern(patternnm);

    return 0;
}


No comments:

Post a Comment