Joey is always fond of playing with patterns. So on his birthday, his uncle gave him a mat. Mat is a rectangular grid
. i.e. there are rows and 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 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<string> pattern, int n, int m){for (int i = 0; i < n - 1; i++){for (int j = 0; j < m; j++){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 = 0; j < m - 1; j++){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 = 5, m = 5;vector<string> pattern = {{"*.***"},{".**.."},{".*.*."},{"*...*"},{"..*.*"}};thePattern(pattern, n, m);return 0;}
No comments:
Post a Comment