HDU - 5706 GirlCat(dfs)

62人阅读 评论(0) 收藏 举报
分类:

GirlCat

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


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.
 

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.
1n1000.
1m1000.
(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
 

Source
 

Recommend
liuyiding


            就是让你在这个字符数组里找到有几个girl和cat~~用dfs一搜索就行了;不过要注意用getchar接收回车键;

#include<iostream>
#include<cstdio>
using namespace std;
char m[1005][1005];
int ans1 = 0;
int ans2 = 0;
int a, b;
int fx[4][2] = { 1,0,0,1,-1,0,0,-1 };
bool check1(int x, int y, int cnt)
{
	char w;

	if (cnt == 1)
	{
		w = 'i';
	}
	else
	{
		w = cnt == 2 ? 'r' : 'l';
	}	
//	cout << m[x][y] << " " << w <<" "<<x<<" "<<y<< endl;
	if (m[x][y] == w)
	{
		return 1;
	}
	return 0;
}
bool check2(int x, int y, int cnt)
{
	char w;
	w = cnt == 1 ? 'a' : 't';
	if (m[x][y] == w)
	{
		return 1;
	}
	return 0;
}
void bfs1(int x, int y, int cnt)
{
	if (cnt == 3)
	{
		ans1++;
		return;
	}
	for (int s = 0; s < 4; s++)
	{
		int xx = x + fx[s][0];
		int yy = y + fx[s][1];
		if (xx == 0 || yy == 0 || xx > a || yy > b)
		{
			continue;
		}
		if (check1(xx, yy, cnt + 1))
		{
		//	cout << "1" << endl;
			bfs1(xx, yy, cnt + 1);
		}
	}
}
void bfs2(int x, int y, int cnt)
{
	if (cnt == 2)
	{
		ans2++;
		return;
	}
	for (int s = 0; s < 4; s++)
	{
		int xx = x + fx[s][0];
		int yy = y + fx[s][1];
		if (xx == 0 || yy == 0 || xx > a || yy > b)
		{
			continue;
		}
		if (check2(xx, yy, cnt + 1))
		{
			bfs2(xx, yy, cnt + 1);
		}
	}
}
int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		ans1 = 0;
		ans2 = 0;
		scanf("%d%d", &a, &b);
		for (int s = 1; s <= a; s++)
		{		
			getchar();
			for (int e = 1; e <= b; e++)
			{
		//		cout << s << " " << e << endl;
				scanf("%c", &m[s][e]);
			}
		}
		for (int s = 1; s <= a; s++)
		{
			for (int e = 1; e <= b; e++)
			{
				if (m[s][e] == 'g')
				{
					bfs1(s, e, 0);
				}
				if (m[s][e] == 'c')
				{
					bfs2(s, e, 0);
				}
			}
		}
		cout << ans1 << " " << ans2 << endl;
	}
	return 0;
}




查看评论

hdu 5706 GirlCat(深搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5706 ——报考杭州电子科技大学! GirlCat T...
  • qiqi_skystar
  • qiqi_skystar
  • 2016-07-03 13:44:46
  • 1806

hdu 5706 GirlCat(DFS)

Problem Description As a cute girl, Kotori likes playing Hide and Seek'' with cats particularly. U...
  • xiaosshhaa
  • xiaosshhaa
  • 2017-05-03 19:15:07
  • 121

hdu 5706 GirlCat(dfs)

GirlCat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...
  • qq_22522375
  • qq_22522375
  • 2016-06-10 19:02:57
  • 343

HDU - 5706 GirlCat (dfs)

GirlCat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...
  • NEET_Champloo
  • NEET_Champloo
  • 2017-08-16 18:23:11
  • 132

HDU 5706 GirlCat DFS解决

HDU 5706 GirlCat DFS
  • qq_26071477
  • qq_26071477
  • 2016-08-24 19:52:14
  • 349

hdu 5706 GirlCat【暴力DFS】

GirlCat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...
  • mengxiang000000
  • mengxiang000000
  • 2016-05-30 18:01:39
  • 660

hdu 5706 GirlCat

题目来源:HDU 5706 简要题目分析: 在n*m的矩阵中,每个点用小写字母表示,让你求矩阵中所包含girl和cat单词的总个数。 其中girl单词如下定义:从某个代表‘g’字母的点出发,向与...
  • polanwind
  • polanwind
  • 2017-05-01 10:52:30
  • 242

hdu 5706 GirlCat 暴力

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submissio...
  • qq_30591245
  • qq_30591245
  • 2017-08-16 18:40:45
  • 52

HDU 5706 GirlCat

搜索题,简单DFS #include #include #include #include #include #include #include #include #include #include...
  • Kim0403
  • Kim0403
  • 2016-07-30 01:31:15
  • 179

Hdoj 5706 GirlCat

Problem Description As a cute girl, Kotori likes playing Hide and Seek'' with cats particularly. U...
  • Sentiment_logos
  • Sentiment_logos
  • 2017-12-27 11:29:32
  • 102
    个人资料
    持之以恒
    等级:
    访问量: 3608
    积分: 705
    排名: 7万+
    文章分类
    文章存档