
hdu-5706 GirlCat
发布日期:2021-05-07 01:32:01
浏览次数:19
分类:原创文章
本文共 1915 字,大约阅读时间需要 6 分钟。
题目地址
题目大意
找出给出的矩阵中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;}
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年04月15日 17时50分29秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux上TCP的几个内核参数调优
2019-03-06
记一次讲故事机器人的开发-我有故事,让机器人来读
2019-03-06
高德算法工程一体化实践和思考
2019-03-06
判断一个数是否是2的幂
2019-03-06
js 闭包(新)
2019-03-06
vscode 编辑python 如何格式化
2019-03-06
seo 回忆录百度基本概念(一)
2019-03-06
重新整理数据结构与算法(c#)—— 算法套路二分法[二十四]
2019-03-06
用ThreadLocal来优化下代码吧
2019-03-06
netcore中使用session
2019-03-06
Android 开发学习进程0.25 自定义控件
2019-03-06
多媒体文件格式全解说(下)--图片
2019-03-06
淘宝WAP版小BUG分析
2019-03-06
NodeJS+Express+MongoDB
2019-03-06
(四十四)c#Winform自定义控件-水波-HZHControls
2019-03-06
c#winform主题实现的一个方法
2019-03-06
asp.net打印网页后自动关闭网页【无需插件】
2019-03-06
一个人开发的html整站源码分享网站就这么上线了
2019-03-06
SQLServer 查看耗时较多的SQL语句(转)
2019-03-06