GirlCat
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7 Accepted Submission(s): 6
Problem Description
As a cute girl, Kotori likes playing ``Hide and Seek'' with cats particularly.
Under the influence of Kotori, many girls and cats are playing ``Hide and Seek'' together.
Koroti shots a photo. The size of this photo is n×m , each pixel of the photo is a character of the lowercase(from `a' to `z').
Kotori wants to know how many girls and how many cats are there in the photo.
We define a girl as -- we choose a point as the start, passing by 4 different connected points continuously, and the four characters are exactly ``girl'' in the order.
We define two girls are different if there is at least a point of the two girls are different.
We define a cat as -- we choose a point as the start, passing by 3 different connected points continuously, and the three characters are exactly ``cat'' in the order.
We define two cats are different if there is at least a point of the two cats are different.
Two points are regarded to be connected if and only if they share a common edge.
Under the influence of Kotori, many girls and cats are playing ``Hide and Seek'' together.
Koroti shots a photo. The size of this photo is n×m , each pixel of the photo is a character of the lowercase(from `a' to `z').
Kotori wants to know how many girls and how many cats are there in the photo.
We define a girl as -- we choose a point as the start, passing by 4 different connected points continuously, and the four characters are exactly ``girl'' in the order.
We define two girls are different if there is at least a point of the two girls are different.
We define a cat as -- we choose a point as the start, passing by 3 different connected points continuously, and the three characters are exactly ``cat'' in the order.
We define two cats are different if there is at least a point of the two cats are different.
Two points are regarded to be connected if and only if they share a common edge.
Input
The first line is an integer
T
which represents the case number.
As for each case, the first line are two integers n and m , which are the height and the width of the photo.
Then there are n lines followed, and there are m characters of each line, which are the the details of the photo.
It is guaranteed that:
T is about 50.
1≤n≤1000 .
1≤m≤1000 .
∑(n×m)≤2×106 .
As for each case, the first line are two integers n and m , which are the height and the width of the photo.
Then there are n lines followed, and there are m characters of each line, which are the the details of the photo.
It is guaranteed that:
T is about 50.
1≤n≤1000 .
1≤m≤1000 .
∑(n×m)≤2×106 .
Output
As for each case, you need to output a single line.
There should be 2 integers in the line with a blank between them representing the number of girls and cats respectively.
Please make sure that there is no extra blank.
There should be 2 integers in the line with a blank between them representing the number of girls and cats respectively.
Please make sure that there is no extra blank.
Sample Input
3 1 4 girl 2 3 oto cat 3 4 girl hrlt hlca
Sample Output
1 0 0 2 4 1
Source
Recommend
水搜索。
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
//void printf(int begin,int end,int mid)
//{
// int i;
// if(begin>end)
// return;
// for(i=begin; i<=end; i++)
// if(b[i]==a[mid])
// break;
// printf(begin,i-1,mid+1); //中序,先左,后跟,再右。此时i为跟节点,左子树必定在begin到i-1里面。
// printf(i+1,end,mid-begin+i+1);//中序,先左,后跟,再右。此时i为跟节点,右子树必定在i+1到end里面。
// cout<<a[mid];
// if(mid==1)
// cout<<endl;
// else
// cout<<" ";
//}
//struct node
//{
// int id;
// char s[1000];
//};
//bool cmp(node a,node b)
//{
// return a.id>b.id;
//}
char s[1005][1005];
int dir[4][2]= {1,0,-1,0,0,1,0,-1};
char g[5]= {"girl"};
char c[5]= {"cat"};
int n,m;
int dfs1(int x,int y,int cnt)
{
int i;
if(x<0 ||y<0 ||x>=n ||y>=m)
return 0;
if(s[x][y]!=g[cnt])
return 0;
if(cnt==3)
return 1;
int res=0;
for(i=0; i<4; i++)
{
int dx=x+dir[i][0];
int dy=y+dir[i][1];
res+=dfs1(dx,dy,cnt+1);
}
return res;
}
int dfs2(int x,int y,int cnt)
{
int i;
if(x<0 ||y<0 ||x>=n ||y>=m)
return 0;
if(s[x][y]!=c[cnt])
return 0;
if(cnt==2)
return 1;
int res=0;
for(i=0; i<4; i++)
{
int dx=x+dir[i][0];
int dy=y+dir[i][1];
res+=dfs2(dx,dy,cnt+1);
}
return res;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j;
cin>>n>>m;
for(i=0; i<n; i++)
cin>>s[i];
int ans1=0;
int ans2=0;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
{
ans1+=dfs1(i,j,0);
ans2+=dfs2(i,j,0);
}
cout<<ans1<<" "<<ans2<<endl;
}
}