X Total Shapes
PROBLEM :
Given N * M string array of O's and X's
Return the number of 'X' total shapes. 'X' shape consists of one or more adjacent X's (diagonals not included).
Example (1):
OOOXOOO
OXXXXXO
OXOOOXO
answer is 1 , shapes are :
(i) X
X X X X
X X
Example (2):
XXX
OOO
XXX
answer is 2, shapes are
(i) XXX
(ii) XXX
Input:
The first line of input takes the number of test cases, T.
Then T test cases follow. Each of the T test cases takes 2 input lines.
The first line of each test case have two integers N and M.The second line of N space separated strings follow which indicate the elements in the array.
Output:
Print number of shapes.
Constraints:
1<=T<=100
1<=N,M<=50
Example:
Input:
2
4 7
OOOOXXO OXOXOOX XXXXOXO OXXXOOO
10 3
XXO OOX OXO OOO XOX XOX OXO XXO XXX OOO
Output:
4
6
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std ;
#include <string.h>
#define MAX 50
int XTotalShapes(char [][MAX],int ,int ) ;
void DFSvisitAll(char [][MAX],int ,int ,bool [][MAX],int ,int ) ;
bool IsConnected(char [][MAX],int ,int ,bool [][MAX],int ,int ) ;
int main()
{
int t ;
cin>>t ;
while(t--)
{
char mat[MAX][MAX] ;
int row,col ;
cin>>row>>col ;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>mat[i][j] ;
cout<<XTotalShapes(mat,row,col)<<endl ;
}
return 0 ;
}
int XTotalShapes(char mat[][MAX],int row,int col)
{
bool visited[MAX][MAX] ;
memset(visited,0, sizeof(visited));
int count=0 ;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
if(!visited[i][j]&&mat[i][j]=='X')
{
count++ ;
DFSvisitAll(mat,row,col,visited,i,j) ;
}
return count ;
}
void DFSvisitAll(char mat[][MAX],int row,int col,bool visited[][MAX],int currR,int currC)
{
visited[currR][currC]=true ;
int t1[]={-1,0,0,1} ;
int t2[]={0,-1,1,0} ;
for(int k=0;k<4;k++)
{
if(IsConnected(mat,row,col,visited,currR+t1[k],currC+t2[k]))
DFSvisitAll(mat,row,col,visited,currR+t1[k],currC+t2[k]) ;
}
}
bool IsConnected(char mat[][MAX],int row,int col,bool visited[][MAX],int currR,int currC)
{
return (currR>=0&&currC>=0)&&(currR<row&&currC<col)&&(mat[currR][currC]=='X'&&!visited[currR][currC]) ;
}
--------------------------------------------------------------------------------
Given N * M string array of O's and X's
Return the number of 'X' total shapes. 'X' shape consists of one or more adjacent X's (diagonals not included).
Example (1):
OOOXOOO
OXXXXXO
OXOOOXO
answer is 1 , shapes are :
(i) X
X X X X
X X
Example (2):
XXX
OOO
XXX
answer is 2, shapes are
(i) XXX
(ii) XXX
Input:
The first line of input takes the number of test cases, T.
Then T test cases follow. Each of the T test cases takes 2 input lines.
The first line of each test case have two integers N and M.The second line of N space separated strings follow which indicate the elements in the array.
Output:
Print number of shapes.
Constraints:
1<=T<=100
1<=N,M<=50
Example:
Input:
2
4 7
OOOOXXO OXOXOOX XXXXOXO OXXXOOO
10 3
XXO OOX OXO OOO XOX XOX OXO XXO XXX OOO
Output:
4
6
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std ;
#include <string.h>
#define MAX 50
int XTotalShapes(char [][MAX],int ,int ) ;
void DFSvisitAll(char [][MAX],int ,int ,bool [][MAX],int ,int ) ;
bool IsConnected(char [][MAX],int ,int ,bool [][MAX],int ,int ) ;
int main()
{
int t ;
cin>>t ;
while(t--)
{
char mat[MAX][MAX] ;
int row,col ;
cin>>row>>col ;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>mat[i][j] ;
cout<<XTotalShapes(mat,row,col)<<endl ;
}
return 0 ;
}
int XTotalShapes(char mat[][MAX],int row,int col)
{
bool visited[MAX][MAX] ;
memset(visited,0, sizeof(visited));
int count=0 ;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
if(!visited[i][j]&&mat[i][j]=='X')
{
count++ ;
DFSvisitAll(mat,row,col,visited,i,j) ;
}
return count ;
}
void DFSvisitAll(char mat[][MAX],int row,int col,bool visited[][MAX],int currR,int currC)
{
visited[currR][currC]=true ;
int t1[]={-1,0,0,1} ;
int t2[]={0,-1,1,0} ;
for(int k=0;k<4;k++)
{
if(IsConnected(mat,row,col,visited,currR+t1[k],currC+t2[k]))
DFSvisitAll(mat,row,col,visited,currR+t1[k],currC+t2[k]) ;
}
}
bool IsConnected(char mat[][MAX],int row,int col,bool visited[][MAX],int currR,int currC)
{
return (currR>=0&&currC>=0)&&(currR<row&&currC<col)&&(mat[currR][currC]=='X'&&!visited[currR][currC]) ;
}
--------------------------------------------------------------------------------
Comments
Post a Comment