参考题解
//
// main.cpp
// shuati
//
// Created by 布屿 on 2019/4/5.
// Copyright © 2019 布屿. All rights reserved.
#include<iostream>
using namespace std;
bool map[102][102];
int x[9]={0,0,-1,-1,-1,0,1,1,1};
int y[9]={0,1,1,0,-1,-1,-1,0,1};
char le[200],chess[101][101];
int n;//n行迷宫,m行坐标。
bool search(int x0,int y0,char w,int p)
{
if(w=='g')
{
map[x0][y0]=1;
return 1;
}
int xx=x0+x[p],yy=y0+y[p];
if(xx>=1 && yy>=1 &&xx<=n &&yy<=n &&chess[xx][yy]==le[w])
if(search(xx,yy,le[w],p))
{
map[x0][y0]=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(search(i,j,'y',k))
map[i][j]=1;
}
for(int i=1;i<=n;i++)
{ for(int j=1;j<=n;j++)
if(map[i][j])
cout<<chess[i][j];
else cout<<'*';
cout<<endl;
}
return 0;
}