hdu-5706 GirlCat

题目地址

hdu5706

题目大意

找出给出的矩阵中cat和girl的数量,单词可以拐弯。

解题思路

很明显是一个bfs的题目,这里我对cat和girl写了两个bfs。
我觉得深搜也是可以做的,因为深度并不会太大。

AC代码

#include <iostream>
#include <queue>

using namespace std;

char s[1100][1100];
int book[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, m;

struct node
{
	int x, y;
	char c;
	
	node (int xx, int yy, char cc)
	{
		x = xx;
		y = yy;
		c = cc;
	}
};

int bfs(int x, int y, char c)
{
	queue <node> que;
	int num = 0;
	
	que.push(node(x, y, c));
	
	while (!que.empty())
	{
		node pNode = que.front();
		que.pop();
		
		if (pNode.c == 'l')
		{
			num++;
			continue;
		}
		
		for (int i=0; i<4; i++)
		{
			int tx = pNode.x + book[i][0];
			int ty = pNode.y + book[i][1];
			
			if (tx < 0 || ty < 0 || tx >= n || ty >= m)
				continue;
			
			if (pNode.c == 'g' && s[tx][ty] == 'i')
				que.push(node(tx, ty, 'i'));
			else if (pNode.c == 'i' && s[tx][ty] == 'r')
				que.push(node(tx, ty, 'r'));
			else if (pNode.c == 'r' && s[tx][ty] == 'l')
				que.push(node(tx, ty, 'l'));
		}
	}
	
	return num;
}

int bfs1(int x, int y, char c)
{
	queue <node> que;
	int num = 0;
	
	que.push(node(x, y, c));
	
	while (!que.empty())
	{
		node pNode = que.front();
		que.pop();
		
		if (pNode.c == 't')
		{
			num++;
			continue;
		}	
		
		for (int i=0; i<4; i++)
		{
			int tx = pNode.x + book[i][0];
			int ty = pNode.y + book[i][1];
			
			if (tx < 0 || ty < 0 || tx >= n || ty >= m)
				continue;
			
			if (pNode.c == 'c' && s[tx][ty] == 'a')
				que.push(node(tx, ty, 'a'));
			else if (pNode.c == 'a' && s[tx][ty] == 't')
				que.push(node(tx, ty, 't'));
		}
	}
	
	return num;
}

int main()
{
	int t;
	
	cin >> t;
	
	while (t--)
	{
		int numg = 0, numc = 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 (s[i][j] == 'g')
					numg += bfs(i, j, 'g');
				else if (s[i][j] == 'c')
					numc += bfs1(i, j, 'c');
			}
		}
		
		cout << numg << " " << numc << endl;
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值