洛谷链接:https://www.luogu.com.cn/problem/P1101
import java.util.Scanner;
public class Main {
static int n=0;
static int[] u= {0,-1,-1,-1,0,1,1,1}; //8个方向
static int[] v= {1,1,0,-1,-1,-1,0,1}; //右,右上,上,左上,左,左下,下,右下
static char[] le=new char[200];
static char[][] chess=new char[101][101];
static boolean[][] ma=new boolean[101][101];
public static boolean dfs(int x,int y,char w,int p) {
if(w=='g') { //最后一个字母判断成功,标记,返回
ma[x][y]=true;
return true;
}
//改变方向
int xx=x+u[p];
int 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]=true;
return true;
}
}
return false;
}
public static void main(String[] args) {
le['y']='i'; //把单词连在一起
le['i']='z';
le['z']='h';
le['h']='o';
le['o']='n';
le['n']='g';
Scanner in=new Scanner(System.in);
n=in.nextInt();
//输入
for(int i=1;i<=n;i++) {
String s=in.next();
for(int j=1;j<=n;j++) {
chess[i][j]=s.charAt(j-1);
}
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
if(chess[i][j]=='y') { //如果是单词开头
for(int k=0;k<=7;k++) { ///8个方向搜索
if(dfs(i,j,'y',k)) { //判断是否成单词
ma[i][j]=true;
}
}
}
}
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
if(ma[i][j]) {
System.out.print(chess[i][j]);
}else {
System.out.print("*");
}
}
System.out.println();
}
}
}