您的位置:首页 > 其它

The 13th Zhejiang Provincial Collegiate Programming Contest - I

2016-04-23 16:59 435 查看
People CountingTime Limit: 2 Seconds Memory Limit: 65536 KB
In a BG (dinner gathering) for ZJU ICPC team, the coaches wanted to count the number of people present at the BG. They did that by having the waitress take a photo for them. Everyone was in the photo and no one was completely blocked. Each person in the photo has the same posture. After some preprocessing, the photo was converted into a H×W character matrix, with the background represented by ".". Thus a person in this photo is represented by the diagram in the following three lines:

.O.
/|\
(.)

Given the character matrix, the coaches want you to count the number of people in the photo. Note that if someone is partly blocked in the photo, only part of the above diagram will be presented in the character matrix.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first contains two integers H, W (1 ≤ H, W ≤ 100) - as described above, followed by H lines, showing the matrix representation of the photo.

Output

For each test case, there should be a single line, containing an integer indicating the number of people from the photo.

Sample Input

2
3 3
.O. /|\ (.)3 4
OOO(
/|\\
()))

Sample Output

1 4

题意:给你一个H*W的矩阵 问其中有多少个人 (只要有一个人的身体的某一部分出现在矩阵中就算一个人)

.O.
/|\
(.) 如图表示一个人

题解:1.二维map存图

2.先统计矩阵中的‘O’

3.当遇到某一身体部位时(除了头部) 向上找到当前这个人的头部的位置 若找到的位置不是‘O’ 则改写为‘O’ 并答案自增1 若是‘O’则说明这个人已经统计过

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define LL __int64
#define pi acos(-1.0)
#define mod 1
#define maxn 10000
using namespace std;
int t;
map<int,map<int,char> > mp;
int n,m;
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=1;i<=t;i++)
{
scanf("%d %d",&n,&m);
int ans=0;
getchar();
mp.clear();
for(int j=1;j<=n;j++)
{
for(int k=1;k<=m;k++)
{
scanf("%c",&mp[j][k]);
if(mp[j][k]=='O')
ans++;
}
getchar();
}
for(int j=1;j<=n;j++)
{
for(int k=1;k<=m;k++)
{
int flag=0;
if(mp[j][k]=='O')
flag=0;
if(mp[j][k]=='/')
{
if(mp[j-1][k+1]=='O')
flag=0;
else
{
flag=1;
mp[j-1][k+1]='O';
}
}
if(mp[j][k]=='|')
{
if(mp[j-1][k]=='O')
flag=0;
else
{
flag=1;
mp[j-1][k]='O';
}
}
if (mp[j][k]=='\\')
{
if(mp[j-1][k-1]=='O')
flag=0;
else
{
flag=1;
mp[j-1][k-1]='O';
}
}
if(mp[j][k]=='(')
{
if(mp[j-2][k+1]=='O')
flag=0;
else
{
flag=1;
mp[j-2][k+1]='O';
}
}

if(mp[j][k]==')')
{
if(mp[j-2][k-1]=='O')
flag=0;
else
{
flag=1;
mp[j-2][k-1]='O';
}
}
if(flag)
ans++;
}
}
cout<<ans<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: