GirlCat

GirlCat

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1909    Accepted Submission(s): 1128


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×mGirlCat , 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 TGirlCat which represents the case number.

As for each case, the first line are two integers nGirlCat and mGirlCat , which are the height and the width of the photo.
Then there are nGirlCat lines followed, and there are mGirlCat characters of each line, which are the the details of the photo.

It is guaranteed that:
TGirlCat is about 50.
1≤n≤1000GirlCat .
1≤m≤1000GirlCat .
∑(n×m)≤2×10GirlCat6GirlCatGirlCat .
 

 

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.

 

 

Sample Input 3 1 4 girl 2 3 oto cat 3 4 girl hrlt hlca  

 

Sample Output 1 0 0 2 4 1 思路:找到g和c开头的dfs就好了。
#include<bits/stdc++.h>
#define REP(i, a, b) for(int i = (a); i <= (b); ++ i)
#define REP(j, a, b) for(int j = (a); j <= (b); ++ j)
#define PER(i, a, b) for(int i = (a); i >= (b); -- i)
using namespace std;
const int maxn=1e5+5;
template <class T>
inline void rd(T &ret){
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9'){
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}
char str[5][10]={"remove","girl","cat"};
char p[1005][1005];
int T,q[5],m,n;
void dfs(int x,int y,int cnt,int cur){
      if(x<1||y<1||x>n||y>m||p[x][y]!=str[cur][cnt])return;
      if((cur==1&&cnt==3)||(cur==2&&cnt==2)){
             q[cur]++;
             return;
      }
      dfs(x+1,y,cnt+1,cur);
      dfs(x,y+1,cnt+1,cur);
      dfs(x-1,y,cnt+1,cur);
      dfs(x,y-1,cnt+1,cur);
}
int main()
{
    rd(T);
    while(T--){
        rd(n),rd(m);
        memset(q,0,sizeof(q));
        for(int i=1;i<=n;i++)scanf("%s",p[i]+1);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(p[i][j]=='g')dfs(i,j,0,1);
                if(p[i][j]=='c')dfs(i,j,0,2);
            }
        }
        cout<<q[1]<<' '<<q[2]<<endl;
    }
    return 0;
}

 

 
上一篇:Get IP Address for different Adpater


下一篇:LC 992. Subarrays with K Different Integers