Incompatible Crops
Limits 1s, 512 MB
Did you know that certain crops cannot grow next to each other? Given the layout of a field as a grid, determine which spots are safe for new crops.
The layout of a field will be given as rows of asterisks (indicating a spot occupied by an incompatible crop) and dots (indicating a free spot).
.**...
*.....
....**
A free spot is safe only if the 4 adjacent spots (the spots immediately above, below, left, and right) are not occupied by an incompatible crop.
In the example above, there are 5 safe spots:
.**.XX
*..X..
.XX.**
Input
The input will contain two integers R (0 < R < 25) and C (0 < C < 25).
This will be followed by R lines, each with C characters.
Output
Print the number of free spots.
Sample
Input | Output |
---|---|
6 6 ****.. *****. ..*.** .*...* .*.**. ..**.. | 3 |
Solution:
#include<iostream> using namespace std; int main() { int n,m,i,j; cin>>n>>m; char s[n][m]; for(i=0;i<n;i++) for(j=0;j<m;j++) cin>>s[i][j]; int c=0; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(i==0&&j==0) { if(s[i][j]=='.'&&s[i][j+1]!='*'&&s[i+1][j]!='*') c++; } else if(j==0) { if(s[i][j]=='.'&&s[i][j+1]!='*'&&s[i+1][j]!='*'&&s[i-1][j]!='*') c++; } else if(j==m-1) { if(s[i][j]=='.'&&s[i][j-1]!='*'&&s[i+1][j]!='*'&&s[i-1][j]!='*') c++; } else if(i==0&&j==m-1) { if(s[i][j]=='.'&&s[i][j-1]!='*'&&s[i+1][j]!='*') c++; } else if(i==n-1&&j==0) { if(s[i][j]=='.'&&s[i][j+1]!='*'&&s[i-1][j]!='*') c++; } else if(i==n-1&&j==m-1) { if(s[i][j]=='.'&&s[i][j-1]!='*'&&s[i-1][j]!='*') c++; } else { if(s[i][j]=='.') { if(s[i][j+1]!='*'&&s[i][j-1]!='*'&&s[i-1][j]!='*'&&s[i+1][j]!='*') c++; } } } } cout<<c<<endl; }
0 Comments