题目描述
小 A 最近有了一个口头禅 “呵呵”,于是他给出了一个矩形,让你求出里面有几个 “hehe”(方向无所谓)。
输入格式
第一行两个数 n,m,表示这个矩形的大小。
接下来 n 行,每行 m 个字符,表示这个矩形。
输出格式
一行一个数,表示有几个 “hehe”。
输入输出样例
输入 #1
5 5 heheh heheh heheh heheh heheh输出 #1
10
说明/提示
1≤n,m≤1000。
题目难度
普及-
参考思路(避坑)
1.找的是hehe而不是he
2.斜着的hehe不算
3.正着的和反着的hehe都算,自上而下或自下而上的hehe也算。
hehe,eheh,
h e
e h
h e
e h
都是算的,所以上下左右都要搜
参考代码
#include<iostream>
using namespace std;
int n,m;
char map[1005][1005];
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++) cin>>map[i][j]; //读入方阵
int ans=0;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(map[i][j]=='h')//如果有h,就枚举搜i
{
if(map[i-1][j]=='e'&&map[i-2][j]=='h'&&map[i-3][j]=='e') ans++; //向上穷举
if(map[i+1][j]=='e'&&map[i+2][j]=='h'&&map[i+3][j]=='e') ans++; //向下穷举
if(map[i][j-1]=='e'&&map[i][j-2]=='h'&&map[i][j-3]=='e') ans++; //向左穷举
if(map[i][j+1]=='e'&&map[i][j+2]=='h'&&map[i][j+3]=='e') ans++; //向右穷举
}
cout<<ans<<endl; //输出答案
return 0;
}