HACKER RANK Algorithms - Implementation - Cavity Map - Solution.'C++'
Algorithms - Implementation - Cavity Map - Solution
Problem Statement
Problem Statement
You are given a square map of size n×n . Each cell of the map has a value denoting its depth. We will call a cell of the map a cavity if and only if this cell is not on the border of the map and each cell adjacent to it has strictly smaller depth. Two cells are adjacent if they have a common side (edge).
You need to find all the cavities on the map and depict them with the uppercase character X.
Input Format
The first line contains an integer, n , denoting the size of the map. Each of the following n lines contains n positive digits without spaces. Each digit (1-9) denotes the depth of the appropriate area.
Constraints
1≤n≤100
Output Format
Output n lines, denoting the resulting map. Each cavity should be replaced with character
X
.
Sample Input
4
1112
1912
1892
1234
Sample Output
1112
1X12
18X2
1234
Explanation
The two cells with the depth of 9 fulfill all the conditions of the Cavity definition and have been replaced by X.
Solution:
#include<iostream>
using namespace std;
int main(){
int n,i,j;
cin>>n;
string inp[n];
for(i=0;i<n;i++)
{
cin>>inp[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i == 0 || j == 0 || i == n-1 || j == n-1 )
{
cout<<inp[i][j];
}
else if(inp[i][j] > inp[i][j-1] && inp[i][j] > inp[i][j+1] && inp[i][j] >inp[i-1][j] && inp[i][j] > inp[i+1][j])
{
cout<<"X" ;
}
else
{
cout<<inp[i][j];
}
}
cout<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
int n,i,j;
cin>>n;
string inp[n];
for(i=0;i<n;i++)
{
cin>>inp[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i == 0 || j == 0 || i == n-1 || j == n-1 )
{
cout<<inp[i][j];
}
else if(inp[i][j] > inp[i][j-1] && inp[i][j] > inp[i][j+1] && inp[i][j] >inp[i-1][j] && inp[i][j] > inp[i+1][j])
{
cout<<"X" ;
}
else
{
cout<<inp[i][j];
}
}
cout<<endl;
}
return 0;
}
Comments
Post a Comment