第七届ACM山东省赛-E The Binding of Isaac

Time Limit: 2000MS Memory limit: 65536K

题目描述

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.
第七届ACM山东省赛-E The Binding of Isaac_第1张图片
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.
第七届ACM山东省赛-E The Binding of Isaac_第2张图片
Now Isaac wants you to help him to find how many places may be the super-secret room.

输入

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.

输出

One line per case. The number of places which may be the super-secret room.

示例输入
2

5 3
..#
.##
##.
.##
##.

1 1
#

示例输出
8
4

题意:给你一个地图,判断super-secret room 的数量,规定超秘房间的四周只有一个common room.

思路:首先在边缘的common room的旁边一定有一个super-secret room ,然后再地图中对于空的房间四个方向搜索,判断是不是只有一个common room .

#include <bits/stdc++.h>

using namespace std;

char M[110][110];

int main()
{
    int T;

    int n,m;

    scanf("%d",&T);

    while(T--)
    {
        int ans = 0;
        scanf("%d %d",&n,&m);

        for(int i = 0;i<n;i++)
            scanf("%s",M[i]);

        for(int i = 0;i<n;i++)
        {

            for(int j = 0;j<m;j++)
            {
                if(M[i][j] == '#') continue;

                int num  =0;
                if(i>0&&M[i-1][j] =='#') num++;

                if(j>0&&M[i][j-1] == '#') num++;

                if(i<n-1&&M[i+1][j] =='#') num++;

                if(j<m-1&&M[i][j+1] == '#') num++;

                if(num == 1) ans++;
            }
        }
        for(int i = 0;i<m;i++)
        {
            if(M[0][i] == '#') ans++;

            if(M[n-1][i] == '#') ans++;
        }

        for(int i = 0;i<n;i++)
        {
            if(M[i][0] == '#') ans++;

            if(M[i][m-1] == '#') ans++;
        }

        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(第七届ACM山东省赛-E The Binding of Isaac)