ZOJ people counting

Source: Internet
Author: User

The 13th session of Zhejiang University student Program Design contest I, a simulation problem.

ZOJ 3944 http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3944

In a BG (dinner gathering) for Zju ICPC team, the coaches wanted to count the number of people present at the BG. They did, and the waitress take a photo for them. Everyone is in the photo and no one were completely blocked. Each person in the photo has the same posture. After some preprocessing, the photo is converted into a H×W character matrix with the background represented by ".". T Hus a person in this photo are 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 are partly blocked in the photo, only part of the above diagram would be presented in the character mat Rix.

Input

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

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

Output

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

Sample Input

3.o./| (.) 3 4OOO (/|\ ()))

Sample Output

14

/////////////////////////////////////////////////////////////////////////
just finished reading the question I was extremely ignorant of the force ...
calm down and find that there are a few points to solve the problem is still useful.
1 At least one part of everyone leaks out.
2 Everyone's stance is the same, that is to say, the relative position of each part of each body is definite. If you find a hand, the other hand of this person may appear in a certain place ...

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <stdlib.h>
# define MAXN 110
typedef long long int LL;
using namespace Std;


Char IN[MAXN][MAXN];
int T, p, W;
int head[5][2] = {{1,-1}, {1,0}, {n}, {2,-1}, {2,1}};
Char Headwing[6] = {"/|\\ ()"};
int leftarm[5][2] = {{ -1,1}, {0,1}, {0,2}, {1,0}, {$}};
Char Leftarmwing[6] = {"o|\\ ()"};
int mid[5][2] = {{ -1,0}, {0,-1}, {0,1}, {1,-1}, {n}};
Char Midwing[6] = {"o/\\ ()"};
int rightarm[5][2] = {{ -1,-1}, {0,-2}, {0,-1}, {1,-2}, {1,0}};
Char Rightarmwing[6] = {"o/| ()"};
int leftleg[5][2] = {{ -2,1}, { -1,0}, { -1,1}, { -1,2}, {0,2}};
Char Leftlegwing[6] = {"o/|\\)"};
int rightleg[5][2] = {{ -2,-1}, { -1,-2}, { -1,-1}, { -1,0}, {0,-2}};
Char Rightlegwing[6] = {"O/|\\ ("};


void check (int i, int j) {

int k = 0, x, y;
if (in[i][j] = = ' O ') {

for (k=0; k<5; ++k) {
x = i + head[k][0];
y = j + head[k][1];


if (x < 0 | | x>=p | | y<0 | | y>=w) {
Continue
}else{
if (in[x][y] = = Headwing[k]) {
In[x][y] = '. ';
}
}
}

}else if (in[i][j] = = '/') {

for (k=0; k<5; ++k) {
x = i + leftarm[k][0];
y = j + leftarm[k][1];
if (x < 0 | | x>=p | | y<0 | | y>=w) {
Continue
}else{
if (in[x][y] = = Leftarmwing[k]) {
In[x][y] = '. ';
}
}
}

}else if (in[i][j] = = ' | ') {


for (k=0; k<5; ++k) {
x = i + mid[k][0];
y = j + mid[k][1];
if (x < 0 | | x>=p | | y<0 | | y>=w) {
Continue
}else{
if (in[x][y] = = Midwing[k]) {
In[x][y] = '. ';
}
}
}

}else if (in[i][j] = = ' \ \ ') {

for (k=0; k<5; ++k) {
x = i + rightarm[k][0];
y = j + rightarm[k][1];
if (x < 0 | | x>=p | | y<0 | | y>=w) {
Continue
}else{
if (in[x][y] = = Rightarmwing[k]) {
In[x][y] = '. ';
}
}
}


}else if (in[i][j] = = ' (') {

for (k=0; k<5; ++k) {
x = i + leftleg[k][0];
y = j + leftleg[k][1];
if (x < 0 | | x>=p | | y<0 | | y>=w) {
Continue
}else{
if (in[x][y] = = Leftlegwing[k]) {
In[x][y] = '. ';
}
}
}

}else if (in[i][j] = = ') ') {

for (k=0; k<5; ++k) {
x = i + rightleg[k][0];
y = j + rightleg[k][1];
if (x < 0 | | x>=p | | y<0 | | y>=w) {
Continue
}else{
if (in[x][y] = = Rightlegwing[k]) {
In[x][y] = '. ';
}
}
}

}


return;
}
int main () {

Freopen ("In.txt", "R", stdin);
scanf ("%d", &t);

while (t--) {
scanf ("%d%d", &p, &w);
GetChar ();

memset (in, 0, sizeof (in));
for (int i=0; i<p; ++i) {
for (int j=0; j<w; ++j) {
scanf ("%c", &in[i][j]);
}
GetChar ();
}


int ans = 0;
for (int i=0; i<p; ++i) {
for (int j=0; j<w; ++j) {

if (in[i][j] = = '. ') {
Continue
}else{

Ans + +;
Check (I,J);
IN[I][J] = '. ';
}

}
}

cout << ans << endl;
}

return 0;
}

The code is so long, I'm really sorry: (.



ZOJ people counting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.