Problem Description
As a cute girl, Kotori likes playing Hide and Seek'' with cats particularly.
Hide and Seek” together.
Under the influence of Kotori, many girls and cats are playing
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.
cat” 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
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.
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
题目链接:HDU-5706
题目大意:如图所示,分别找出图中完全等于‘girl’和‘cat’的有几种。
注意;
1.必须完全等于‘girl’和‘cat’
2.顺序不能反
题目思路:BFS,以girl为例,找到g进行bfs,旁边等于i的放入队列,以此,看看最后队列里面有几个l,就是答案
//这道题,比赛的时候看错题目意思,以为只要是girl或者irl,单独i也可以。算是一个女孩。代码写的很复杂到最后明白已经晚了,尽管A了,可是没时间写别的题目,感觉好可惜。。
以下是代码:
//
// 5706.cpp
// 2016-CCPC-GIRL
//
// Created by pro on 16/7/3.
// Copyright (c) 2016年 loy. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
using namespace std;
int n,m;
string s[1005];
int vis[1005][1005];
map <char,char> mp;
int dx[5] = {1,-1,0,0};
int dy[5] = {0,0,1,-1};
struct node
{
char ch;
int x,y;
};
int ans_girl,ans_cat;
void bfs(int r,int c,char ch)
{
queue <node> que;
//头结点
node zero;
zero.x = r; zero.y = c; zero.ch = ch;
que.push(zero);
while(!que.empty())
{
node front = que.front();
que.pop();
vis[front.x][front.y] = 1;
if (front.ch == 'l')
{
ans_girl++;
continue;
}
if (front.ch == 't')
{
ans_cat++;
continue;
}
for (int i = 0; i < 4; i++)
{
int x = dx[i] + front.x;
int y = dy[i] + front.y;
if (x < 0 || x >= n || y < 0 || y >= m) continue; //超出范围
if (s[x][y] != mp[front.ch]) continue; //不是接下来的
node tmp;
tmp.x = x; tmp.y = y; tmp.ch = s[x][y];
que.push(tmp);
}
}
}
int main()
{
int t;
cin >> t;
mp['g'] = 'i'; mp['i'] = 'r'; mp['r'] = 'l';
mp['c'] = 'a'; mp['a'] = 't';
while(t--)
{
//初始化
memset(vis,0,sizeof(vis));
ans_girl = 0;
ans_cat = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (!vis[i][j] && s[i][j] == 'g' || s[i][j] == 'c')
{
bfs(i,j,s[i][j]);
}
}
}
cout << ans_girl << " " << ans_cat << endl;
}
return 0;
}