已在代码中注释问题,求一个大佬来一个详细的解释。求求了~
#include <iostream>
#include <cstdio>
#pragma warning(disable:4996)
using namespace std;
int n,m;
int dir[8][2] = { {1,0}, //↓
{1,1}, //↘
{0,1}, //→
{-1,1},//↗
{-1,0},
{-1,-1},
{0,-1},
{1,-1}
};
char mm[105][105]; // 存地图
bool vis[105][105]; // 标记是否访问过
string goal = "yizhong";
struct node {
int x, y;
}c[105];
void dfs(int x, int y,node c[],int k,int u)
{
if (u == 7) {
cout << "x: " << x << " y:" << y << " u:" << u << endl;
// 对目标字串进行标记
for (int i = 0; i < 7; i++) {
vis[c[i].x][c[i].y] = 1;
}
}
else {
cout << "x: " << x << " y:" << y << " u:" << u << endl;
int tx = x + dir[k][0];
int ty = y + dir[k][1];
// 问题1:为什么这里数组越界没问题
// 问题2:为什么这里一定要加上u==6
if ( u == 6 || mm[tx][ty] == goal[u + 1]) {
c[u].x = x;
c[u].y = y;
dfs(tx,ty,c,k,u + 1);
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> mm[i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mm[i][j] == 'y') {
for (int k = 0; k < 8; k++) {
int tx = i + dir[k][0];
int ty = j + dir[k][1];
if (mm[tx][ty] == 'i') {
dfs(i,j,c,k,0);
}
}
}
}
}
for (int i = 0; i < n; i++) {//输出结果
for (int j = 0; j < n; j++)
if (vis[i][j]) printf("%c", mm[i][j]);
else printf("*");
printf("\n");
}
return 0;//结束程序
}