链接:戳这里
People Counting
Time 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
思路:统计出人6个地方所对应的格子对应的部位++,然后取所有格子最大的部位的值
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int T;
int n,m;
struct node{
int head,l,r,z,lt,rt;
}s[1010][1010];
char a[1010][1010];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
mst(s,0);
for(int i=2;i<=n+1;i++){
getchar();
for(int j=2;j<=m+1;j++){
scanf("%c",&a[i][j]);
}
}
for(int i=2;i<=n+1;i++){
for(int j=2;j<=m+1;j++){
if(a[i][j]=='.') continue;
if(a[i][j]=='O'){
s[i][j].head++;
s[i+1][j-1].l++;
s[i+1][j].z++;
s[i+1][j+1].r++;
s[i+2][j-1].lt++;
s[i+2][j+1].rt++;
} else if(a[i][j]=='|'){
s[i-1][j].head++;
s[i][j-1].l++;
s[i][j].z++;
s[i][j+1].r++;
s[i+1][j-1].lt++;
s[i+1][j+1].rt++;
} else if(a[i][j]=='/'){
s[i-1][j+1].head++;
s[i][j].l++;
s[i][j+1].z++;
s[i][j+2].r++;
s[i+1][j].lt++;
s[i+1][j+2].rt++;
} else if(a[i][j]=='\\'){
s[i-1][j-1].head++;
s[i][j-2].l++;
s[i][j-1].z++;
s[i][j].r++;
s[i+1][j-2].lt++;
s[i+1][j].rt++;
} else if(a[i][j]=='('){
s[i-2][j+1].head++;
s[i-1][j].l++;
s[i-1][j+1].z++;
s[i-1][j+2].r++;
s[i][j].lt++;
s[i][j+2].rt++;
} else if(a[i][j]==')'){
s[i-2][j-1].head++;
s[i-1][j-2].l++;
s[i-1][j-1].z++;
s[i-1][j].r++;
s[i][j-2].lt++;
s[i][j].rt++;
}
}
}
int ans=0;
int head=0,l=0,r=0,z=0,lt=0,rt=0;
for(int i=0;i<=n+3;i++){
for(int j=0;j<=m+3;j++){
if(s[i][j].head) head++;
if(s[i][j].l) l++;
if(s[i][j].z) z++;
if(s[i][j].r) r++;
if(s[i][j].lt) lt++;
if(s[i][j].rt) rt++;
}
ans=max(head,ans);
ans=max(l,ans);
ans=max(z,ans);
ans=max(r,ans);
ans=max(lt,ans);
ans=max(rt,ans);
}
cout<<ans<<endl;
}
return 0;
}

本文介绍了一种通过分析字符矩阵来计算照片中人数的方法,该方法能够处理部分遮挡的情况,并提供了一个具体的实现示例。
1527

被折叠的 条评论
为什么被折叠?



