标签:comm span lan rac div sha output least com
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; }
标签:comm span lan rac div sha output least com
原文地址:https://www.cnblogs.com/caiyishuai/p/9033153.html