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.
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.
For each test case, there should be a single line, containing an integer indicating the number of people from the photo.
2 3 3 .O. /|\ (.) 3 4 OOO( /|\\ ()))
14
题解:大模拟,纯暴力搜索,拿照片中的人去匹配其余的地方;当然人也可以在图外面
AC代码:
#include <bits/stdc++.h> #define maxn 1005 using namespace std; char dp[maxn][maxn]; int x,y,n,m; void gao() { if(dp[x][y]=='O') { if(dp[x+1][y]=='|')dp[x+1][y]='.'; if(dp[x+1][y-1]=='/')dp[x+1][y-1]='.'; if(dp[x+1][y+1]=='\\')dp[x+1][y+1]='.'; if(dp[x+2][y-1]=='(')dp[x+2][y-1]='.'; if(dp[x+2][y+1]==')')dp[x+2][y+1]='.'; } else if(dp[x][y]=='|') { if(dp[x-1][y]=='O')dp[x-1][y]='.'; if(dp[x][y-1]=='/')dp[x][y-1]='.'; if(dp[x][y+1]=='\\')dp[x][y+1]='.'; if(dp[x+1][y-1]=='(')dp[x+1][y-1]='.'; if(dp[x+1][y+1]==')')dp[x+1][y+1]='.'; } else if(dp[x][y]=='/') { if(dp[x-1][y+1]=='O')dp[x-1][y+1]='.'; if(dp[x][y+1]=='|')dp[x][y+1]='.'; if(dp[x][y+2]=='\\')dp[x][y+2]='.'; if(dp[x+1][y]=='(')dp[x+1][y]='.'; if(dp[x+1][y+2]==')')dp[x+1][y+2]='.'; } else if(dp[x][y]=='\\') { if(dp[x-1][y-1]=='O')dp[x-1][y-1]='.'; if(dp[x][y-1]=='|')dp[x][y-1]='.'; if(dp[x][y-2]=='/')dp[x][y-2]='.'; if(dp[x+1][y-2]=='(')dp[x+1][y-2]='.'; if(dp[x+1][y]==')')dp[x+1][y]='.'; } else if(dp[x][y]=='(') { if(dp[x-2][y+1]=='O')dp[x-2][y+1]='.'; if(dp[x-1][y]=='/')dp[x-1][y]='.'; if(dp[x-1][y+1]=='|')dp[x-1][y+1]='.'; if(dp[x-1][y+2]=='\\')dp[x-1][y+2]='.'; if(dp[x][y+2]==')')dp[x][y+2]='.'; } else if(dp[x][y]==')') { if(dp[x-2][y-1]=='O')dp[x-2][y-1]='.'; if(dp[x-1][y-2]=='/')dp[x-1][y-2]='.'; if(dp[x-1][y-1]=='|')dp[x-1][y-1]='.'; if(dp[x-1][y]=='\\')dp[x-1][y]='.'; if(dp[x][y-2]==')')dp[x][y-2]='.'; } dp[x][y]='.'; } int juge() { for(int i=1+5; i<=n+5; ++i) for(int j=1+5; j<=m+5; ++j) if(dp[i][j]!='.') { x=i; y=j; gao(); return 1; } return 0; } void init() { for(int i=0; i<maxn; ++i) for(int j=0; j<maxn; ++j) dp[i][j]='.'; } int main() { int loop; cin>>loop; while(loop--) { init(); cin>>n>>m; for(int i=1+5; i<=n+5; ++i) for(int j=1+5; j<=m+5; ++j) cin>>dp[i][j]; int ans=0; while(juge())ans++; printf("%d\n",ans); } return 0; }