蓝桥杯备考-》暴力枚举之单词方阵

本题,数据范围较小,我们可以选择四层for循环,也可以选择dfs

这里,我们就用四层for循环来写一下吧

我们的思路就是输入完n*n的格子之后,我们遍历格子,如果搜到了恰好是目标字符的第一个字符,就把这个字符向所有方向遍历,如果有符合要求的单词,我们就把他打上标记

好的,我们来写一下代码

#include <iostream>
using namespace std;
const int N = 110;
int n;
char a[N][N];
string s = "yizhong";
int dx[] = { -1,1,0,0,-1,1,-1,1 };
int dy[] = { 0,0,1,-1,-1,-1,1,1 };
bool st[N][N];
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			cin >> a[i][j];
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (a[i][j] == s[0])
			{
				for (int k = 0; k < 8; k++)
				{
					int x = i, y = j;
					int pos = 0;
					int cnt = 0;
					while (x<n && x>=0 && y<n && y>=0 &&a[x][y] == s[pos])
					{
						cnt++;
						x = x + dx[k];
						y = y + dy[k];
						pos++;
						if (x > n || x<0 || y>n || y < 0) break;
					}
					if (cnt == s.size())
					{
						int x = i, y = j;
						int t = cnt;
						while (t--)
						{
							st[x][y] = true;
							x = x + dx[k];
							y = y + dy[k];
						}
					}
				}
			}
			





		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (!st[i][j]) cout << "*";
			else cout << a[i][j];
		}
		cout << endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无敌大饺子 dot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值