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 Tindicating 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
思路:直接模拟即可,每读到一个人的片段就深搜它其他位置,做标记即可。
//#include<bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp make_pair
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 1e2+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
typedef vector<ll> vec;
typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
int t;
int n, m;
char mp[maxn][maxn];
int cnt[maxn][maxn];
int ans;
bool ismp(int x, int y){
if(0<=x&&x<n && 0<=y&&y<m)
return true;
return false;
}
void check(int x, int y, char c){
if(c == 'O')
{
cnt[x][y] = ans;
if(ismp(x+1,y) && mp[x+1][y] == '|'){
cnt[x+1][y] = ans;
}
if(ismp(x+1,y-1) && mp[x+1][y-1] == '/'){
cnt[x+1][y-1] = ans;
}
if(ismp(x+1,y+1) && mp[x+1][y+1] == '\\'){
cnt[x+1][y+1] = ans;
}
if(ismp(x+2,y-1) && mp[x+2][y-1] == '('){
cnt[x+2][y-1] = ans;
}
if(ismp(x+2,y+1) && mp[x+2][y+1] == ')'){
cnt[x+2][y+1] = ans;
}
ans++;
}
else if(c == '/')
{
cnt[x][y] = ans;
if(ismp(x,y+1) && mp[x][y+1] == '|'){
cnt[x][y+1] = ans;
}
if(ismp(x,y+2) && mp[x][y+2] == '\\'){
cnt[x][y+2] = ans;
}
if(ismp(x-1,y+1) && mp[x-1][y+1] == 'O'){
cnt[x-1][y+1] = ans;
}
if(ismp(x+1,y) && mp[x+1][y] == '('){
cnt[x+1][y] = ans;
}
if(ismp(x+1,y+2) && mp[x+1][y+2] == ')'){
cnt[x+1][y+2] = ans;
}
ans++;
}
else if(c == '|')
{
cnt[x][y] = ans;
if(ismp(x,y+1) && mp[x][y+1] == '\\'){
cnt[x][y+1] = ans;
}
if(ismp(x,y-1) && mp[x][y-1] == '/'){
cnt[x][y-1] = ans;
}
if(ismp(x-1,y) && mp[x-1][y] == 'O'){
cnt[x-1][y] = ans;
}
if(ismp(x+1,y-1) && mp[x+1][y-1] == '('){
cnt[x+1][y-1] = ans;
}
if(ismp(x+1,y+1) && mp[x+1][y+1] == ')'){
cnt[x+1][y+1] = ans;
}
ans++;
}
else if(c == '\\')
{
cnt[x][y] = ans;
if(ismp(x,y-1) && mp[x][y-1] == '|'){
cnt[x][y-1] = ans;
}
if(ismp(x,y-2) && mp[x][y-2] == '/'){
cnt[x][y-2] = ans;
}
if(ismp(x-1,y-1) && mp[x-1][y-1] == 'O'){
cnt[x-1][y-1] = ans;
}
if(ismp(x+1,y) && mp[x+1][y] == ')'){
cnt[x+1][y] = ans;
}
if(ismp(x+1,y-2) && mp[x+1][y-2] == '('){
cnt[x+1][y-2] = ans;
}
ans++;
}
else if(c == '(')
{
cnt[x][y] = ans;
if(ismp(x,y+2) && mp[x][y+2] == ')'){
cnt[x][y+2] = ans;
}
if(ismp(x-1,y) && mp[x-1][y] == '/'){
cnt[x-1][y] = ans;
}
if(ismp(x-1,y+1) && mp[x-1][y+1] == '|'){
cnt[x-1][y+1] = ans;
}
if(ismp(x-1,y+2) && mp[x-1][y+2] == '\\'){
cnt[x-1][y+2] = ans;
}
if(ismp(x-2,y+1) && mp[x-2][y+1] == 'O'){
cnt[x-2][y+1] = ans;
}
ans++;
}
else if(c == ')')
{
cnt[x][y] = ans;
if(ismp(x,y-2) && mp[x][y-2] == '('){
cnt[x][y-2] = ans;
}
if(ismp(x-1,y) && mp[x-1][y] == '\\'){
cnt[x-1][y] = ans;
}
if(ismp(x-1,y-1) && mp[x-1][y-1] == '|'){
cnt[x-1][y-1] = ans;
}
if(ismp(x-1,y-2) && mp[x-1][y-2] == '/'){
cnt[x-1][y-2] = ans;
}
if(ismp(x-2,y-1) && mp[x-2][y-1] == 'O'){
cnt[x-2][y-1] = ans;
}
ans++;
}
}
int main()
{
//std::ios::sync_with_stdio(false);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
sd(t);
getchar();
while(t--)
{
ans = 0;
ms(cnt, -1);
sdd(n, m);
getchar();
rep(i, 0, n-1)
gets(mp[i]);
rep(i, 0, n-1){
rep(j, 0, m-1){
if(mp[i][j] != '.' && cnt[i][j] == -1){
check(i, j, mp[i][j]);
}
}
}
printf("%d\n", ans);
}
return 0;
}