Header Ads Widget

The Pattern

Problem:-

Joey is always in 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 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. At the end Joey wants to know what will be on the top of that cell (dot (.)or star()).
See the below image for more explanation.
img
When star() come over dot(.) it converted to dot(.)
When star() come over star(), it remains star()
When dot(.) come over dot(.), it remains dot(.)
When dot(.) come over star(), it converted to star()
Input:
First line of Input contains an integer T, denoting no of testcases.
First line of each testcase contains two integers N and M, no of rows and columns in the grid(respectively).
Each of next N lines contains a string of length M.

Output:
For each testcase print a single line, the character on the top of folded mat.
Constraints:
    1T100
    1N,M103
SAMPLE INPUT
 
1
5 5
*.***
.**..
.*.*.
*...*
..*.*
SAMPLE OUTPUT
 
*
Time Limit:1.0 sec(s) for each input file.
Memory Limit:256 MB
Source Limit:1024 KB

solution:-

#include<stdio.h>
void main()
{
   int t,n,m;
   char s[1000][1000];
   scanf("%d",&t);
   for(int i=0;i<t;i++)
   {
     scanf("%d%d",&n,&m);
     for(int j=0;j<n;j++)
     scanf("%s",s[j]);
     printf("%c\n",s[n-1][m-1]);
   }
}

Post a Comment

0 Comments