P1101 单词方阵【洛谷】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int n;
int u[]={0,1,1,1,-1,-1,-1,0,0};
int v[]={0,0,-1,1,0,1,-1,1,-1};
bool ma[101][101];
char le[200],chess[101][101];
bool dfs(int x,int y,char w,int p)
{
	if(w=='g')	{ma[x][y]=1;return 1;}
	int xx=x+u[p],yy=y+v[p];
	if(xx>=1&&yy>=1&&xx<=n&&yy<=n&&chess[xx][yy]==le[w])
		if(dfs(xx,yy,le[w],p))
		{
			ma[x][y]=1;
			return 1;
		}
	return 0;
}
int main()
{
	le['y']='i';le['i']='z';le['z']='h';le['h']='o';le['o']='n';le['n']='g';
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)	cin>>chess[i][j];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(chess[i][j]=='y')
				for(int k=1;k<=8;k++)
					if(dfs(i,j,'y',k))	ma[i][j]=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
			if(ma[i][j])	cout<<chess[i][j];
			else	cout<<"*";
		cout<<endl;
	}
}
发布了180 篇原创文章 · 获赞 22 · 访问量 9010

猜你喜欢

转载自blog.csdn.net/qq_44622401/article/details/104295897