GirlCat
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11 Accepted Submission(s): 10
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
题意:给出一个长n,宽m的照片,要求计数里面出现“girl","cat"各自出现的次数,同一个单词如果有一个字母位置不同,则认为是两个单词。
题目链接:GirlCat
解题思路:一开始做的时候以为是像上次浙江省赛那道数人头的题目(要写好多函数才AC),我就暂时先放弃了。结果再次做的时候仔细一想,这两个单词很短,可以用DFS,搜过的地方不需要改变(数人头就必须把搜过的位置区别出来),也不能改变,因为两个单词会有同一段相同的部分单词,然后直接DFS四个方向即可。
总结:DFS很基础的方法,不可忽视,要多加训练!
代码:
//GirlCat
#include
#include
#define N 1005
using namespace std;
int n, m;
char a[N][N];
int dy[4]={ -1,0,0,1 }, dx[4]={ 0,-1,1,0 }; //上下左右四个方向
char girl[5]={"girl"};
char cat[4]={"cat"};
int find_g(int y,int x,int p) //DFS girl,x,y代表搜索的位置,p表示要搜索的单词部位
{
if (y<0 || y>=n || x<0 || x>=m) return 0 ; //边界控制
if (a[y][x] != girl[p]) return 0; //该位置不是正确位置,就是没对上单词的顺序
if (p == 3) return 1; //找完一个完整 girl单词
int cnt=0; //初始化
for (int i = 0; i < 4; ++i)
cnt += find_g(y + dy[i], x + dx[i], p + 1); //把四个方向找到的单词都加起来
return cnt;
}
int find_c(int y, int x, int p) //DFS cat
{
if ( y<0 || y>=n || x<0 || x>=m ) return 0; //类似上面
if (a[y][x] != cat[p]) return 0;
if (p == 2) return 1;
int cnt = 0;
for (int i = 0; i < 4; ++i)
cnt += find_c(y + dy[i], x + dx[i], p + 1);
return cnt ;
}
int main()
{
int T,i,j,cntg,cntc;
cin>>T;
while(T--){
cin>>n>>m;
for(i=0;i