题目链接: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;
}