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×mn×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.
InputThe first line is an integer TT which represents the case number.
As for each case, the first line are two integers nn and mm, which are the height and the width of the photo.
Then there are nn lines followed, and there are mm characters of each line, which are the the details of the photo.
It is guaranteed that:
TT is about 50.
1≤n≤10001≤n≤1000.
1≤m≤10001≤m≤1000.
∑(n×m)≤2×106∑(n×m)≤2×106.
OutputAs 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题目大意:根据输入的字符矩阵,分别找到girl和cat字符串的数量。
解题思路:找到一个起始点,直接进行搜索,查找接下去的字母。
#include<stdio.h> #include<string.h> #include<string> #include<iostream> #include<string.h> #include<algorithm> #include<vector> #include<stdio.h> #include<cstdio> #include<time.h> #include<stack> #include<queue> #include<deque> #include<map> #define inf 0x3f3f3f3f #define ll long long using namespace std; char a[1005][1005]; int d[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; struct node { int x,y; char c; }; queue<node>q; int main() { int t; cin>>t; while(t--) { int n,m; cin>>n>>m; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { cin>>a[i][j]; } } while(!q.empty ()) q.pop(); int sg=0,sc=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(a[i][j]==‘g‘) { node p; p.x=i; p.y=j; p.c=‘g‘; q.push(p); } if(a[i][j]==‘c‘) { node p; p.x=i; p.y=j; p.c=‘c‘; q.push(p); } } } while(!q.empty()) { node p=q.front (); q.pop(); char c=p.c; for(int i=0;i<4;i++) { int xx=p.x+d[i][0]; int yy=p.y+d[i][1]; if(xx<1||yy<1||xx>n||yy>m) continue; char cc=a[xx][yy]; if((c==‘g‘&&cc==‘i‘)||(c==‘i‘&&cc==‘r‘)||(c==‘c‘&&cc==‘a‘)) { node pp; pp.x=xx; pp.y=yy; pp.c=cc; q.push(pp); } if(c==‘r‘&&cc==‘l‘) { sg++; } if(c==‘a‘&&cc==‘t‘) { sc++; } } } cout<<sg<<" "<<sc<<endl; } return 0; }
原文地址:https://www.cnblogs.com/caiyishuai/p/9033153.html