洛谷P1101 单词方阵

题目链接:https://www.luogu.com.cn/problem/P1101

简单说下思路。我们要先确定搜索的方向才能往下进行搜索,所以搜索部分只需要按照一个方向进行就可以,而方向的确定则是:

for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(str[i][j]=='y')//if find the first number 
			{
				for(int k=0; k<8; k++)
				{
					x=i+dir[k][0];
					y=j+dir[k][1];
					if(str[x][y]=='i')//to ensure the direction and maybe many ways

以下是代码:

#include <bits/stdc++.h>
#define MAXN 450
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int n;
char str[MAXN][MAXN];
int path[MAXN][MAXN];
char match[]="yizhong";
int dir[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; 
struct node
{
	int x;
	int y;
}cur[MAXN];

void dfs(int x,int y,int k,int step)//step is xiabiao
{
	if(step==7)//this is ok 
	{
	//	cout<<"ok"<<endl;
		//save path
		for(int i=0; i<7; i++)
		{
			path[cur[i].x][cur[i].y]=1;
		}
	
	}else
	{
		int xx=x+dir[k][0];
		int yy=y+dir[k][1];
		if(str[xx][yy]==match[step+1]||step==6)
		{
			cur[step].x=x;
			cur[step].y=y;
			dfs(xx,yy,k,step+1);
		}
	}

}

int main(int argc, char** argv)
{
	cin>>n;
	memset(path,0,sizeof(path));
	for(int i=0; i<n; i++)
	{
		cin>>str[i];
	}
	int x,y;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(str[i][j]=='y')//if find the first number 
			{
				for(int k=0; k<8; k++)
				{
					x=i+dir[k][0];
					y=j+dir[k][1];
					if(str[x][y]=='i')//to ensure the direction and maybe many ways
					{
						dfs(i,j,k,0);
			//			cout<<i<<" "<<j<<endl;
					}
				}
			}
		}
	}
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(path[i][j])
			{
				cout<<str[i][j];
			}else
			{
				cout<<"*";
			}
		}
		cout<<endl;
	}
	return 0; 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

九久呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值