Description
Ok, now I will introduce this game to you...
Isaac is trapped in a maze which has many common rooms…
Like this…There are 9 common rooms on the map.
And there is only one super-secret room. We can’t see it on the map. The super-secret room always has many special items in it. Isaac wants to find it but he doesn’t know where it is. Bob tells him that the super-secret room is located in an empty place which is adjacent to only one common rooms. Two rooms are called adjacent only if they share an edge. But there will be many possible places.
Now Isaac wants you to help him to find how many places may be the super-secret room.
Input
Multiple test cases. The first line contains an integer T (T<=3000), indicating the number of test case.
Each test case begins with a line containing two integers N and M (N<=100, M<=100) indicating the number of rows and columns. N lines follow, “#” represent a common room. “.” represent an empty place.
Common rooms maybe not connect. Don’t worry, Isaac can teleport.
Output
One line per case. The number of places which may be the super-secret room.
Sample Input
2
5 3
..#
.##
##.
.##
##.
1 1
#
Sample Output
8
4
思路:关键的一句话“super-secret room is located in an empty place which is adjacent to only one common rooms.“
代码
#include<bits/stdc++.h>
#define MAX 105
using namespace std;
char maze[MAX][MAX];
int Find(int n,int m)
{
int i,j,sum=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(maze[i][j]=='#')
{
if(maze[i+1][j]=='0')
sum++;
if(maze[i-1][j]=='0')
sum++;
if(maze[i][j+1]=='0')
sum++;
if(maze[i][j-1]=='0')
sum++;
}
if(maze[i][j]=='.')
{
if(maze[i+1][j]=='#'&&maze[i-1][j]!='#'&&maze[i][j-1]!='#'&&maze[i][j+1]!='#')
sum++;
if(maze[i+1][j]!='#'&&maze[i-1][j]=='#'&&maze[i][j-1]!='#'&&maze[i][j+1]!='#')
sum++;
if(maze[i+1][j]!='#'&&maze[i-1][j]!='#'&&maze[i][j-1]=='#'&&maze[i][j+1]!='#')
sum++;
if(maze[i+1][j]!='#'&&maze[i-1][j]!='#'&&maze[i][j-1]!='#'&&maze[i][j+1]=='#')
sum++;
}
}
}
return sum;
}
int main()
{
int T,n,m,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(maze,'0',sizeof(maze));
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>maze[i][j];
}
}
int re=Find(n,m);
printf("%d\n",re);
}
return 0;
}