输入o后不显示最短径,显示地图中也无8

stack<point> sta;
int Endx, Endy;
int Min = 1000;
int v[100][100] = { 0 };//访问数组0未访问7访问
int v1[100][100] = { 0 };
void DFS(int x, int y, int step)//x,y表示当前坐标点
{
if (x == Endx - 1 && y == Endy - 1)//到达终点
{
if (step < Min)
{
Min = step;
for (int i = 0; i < Min; i++)
{
//for (int j = 0; j < Min; j++)
{
map[key][sta.top().i][sta.top().j] = 8;
}
sta.pop();
}
}
for (int i = 0; i < sta.size(); i++)
sta.pop();
return;
}
else//x,y不是终点:则开始试探
{
//顺时针右下左上试探
if (map[key][x][y + 1] == 1 && v[x][y + 1] == 0)//向右
{
v[x][y + 1] = 7;//访问了
point temp;
temp.i = x;
temp.j = y + 1;
sta.push(temp);
DFS(x, y + 1, step + 1);
v[x][y + 1] = 0;//回溯时设置为0未访问
//sta.pop();
}
if (map[key][x + 1][y] == 1 && v[x + 1][y] == 0)//向下
{
v[x + 1][y] = 7;//访问了
point temp;
temp.i = x + 1;
temp.j = y;
sta.push(temp);
DFS(x + 1, y, step + 1);
v[x + 1][y] = 0;//回溯时设置为0未访问
//sta.pop();
}
if (map[key][x][y - 1] == 1 && v[x][y - 1] == 0)//向左
{
v[x][y - 1] = 7;//访问了
point temp;
temp.i = x;
temp.j = y - 1;
sta.push(temp);
DFS(x, y - 1, step + 1);
v[x][y - 1] = 0;//回溯时设置为0未访问
//sta.pop();
}
if (map[key][x - 1][y] == 1 && v[x - 1][y] == 0)//向上
{
v[x - 1][y] = 7;//访问了
point temp;
temp.i = x - 1;
temp.j = y;
sta.push(temp);
DFS(x - 1, y, step + 1);
v[x - 1][y] = 0;//回溯时设置为0未访问
//sta.pop();
}
return;
}
}
void FindShortPath()
{
const int m = 21, n = 21;
for (int i = 0; i < bound; i++)
{
for (int j = 0; j < bound; j++)
{
copyMap[i][j] = map[key][i][j];//克隆数组
}
}
for (int i = 0; i < bound; i++)
{
for (int j = 0; j < bound; j++)
{
if (map[key][i][j] == 9)
{
player.i = i;
player.j = j;
map[key][i][j] = 1;//将初始位置覆盖
}
if (map[key][i][j] == 4)
{//放置粮仓
granarys.i = i;
granarys.j = j;
}
}
}
int Beginx = player.i;
int Beginy = player.j;
int Endx = granarys.i;
int Endy = granarys.j;
memset(v, 0, sizeof(v));
memset(v1, 0, sizeof(v1));
Min = INT_MAX; // 重置最短路径
v[Beginx][Beginy] = 8;
v1[Beginx][Beginy] = 8;
DFS(Beginx, Beginy, 0);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << map[key][i][j] << ' ';
}
cout << endl;
}
return;
}
void Show()
{
int i, j;
cleardevice();
for (i = 0; i < bound; i++)
{
for (j = 0; j < bound; j++)
{
if (map[key][i][j] == 0)//绘制墙体1
putimagePng(j * imSize, i * imSize, &wall);
if (map[key][i][j] == 3)//绘制墙体2
putimagePng(j * imSize, i * imSize, &wall_2);
if (map[key][i][j] == 4)//绘制粮仓
putimagePng(j * imSize, i * imSize, &granary_Png);
if (map[key][i][j] == 7)//显示全部路径
{
setfillcolor(GREEN);
fillrectangle(j * imSize, i * imSize, (j + 1) * imSize, (i + 1) * imSize);
}
if (map[key][i][j] == 8)//显示最短路径
{
setfillcolor(RED);
fillrectangle(j * imSize, i * imSize, (j + 1) * imSize, (i + 1) * imSize);
}
if (map[key][i][j] == 1)
//通道
if (map[key][i][j] == 9)
{
if (direction == 4)
{
putimage(j * imSize, i * imSize, &mouse_down);
}
else if (direction == 3)
{
putimage(j * imSize, i * imSize, &mouse_right);
}
else if (direction == 2)
{
putimage(j * imSize, i * imSize, &mouse_up);
}
else if (direction == 1)
{
putimage(j * imSize, i * imSize, &mouse_left);
}
}
if (player.i == granarys.i && player.j == granarys.j)//到达终点
{
Texttip();
key++;
closegraph();
if (key != 3)
Start();
else
{
Texttip2();
closegraph();
return;
}
}
if (item == 3)//绘制编辑网格
{
setlinecolor(GREEN);
rectangle(j * imSize, i * imSize, (j + 1) * imSize, (i + 1) * imSize);
}
}
}
void Input()
{
while (!_kbhit()) //检查是否有输入,没输入时则更新时间
{
BeginBatchDraw();
if (!UpdateAndShowTime()) { //当该函数返回false,即超时
Texttip4();
key = 3;
return;
}
FlushBatchDraw();
}
char input = _getch();
if (input == 'a' || input == 's' || input == 'd' || input == 'w' || input == 'c' || input == 'i' || input == 'o')//按c编辑迷宫,i显示全部路径
{//有效按键
int goal_i = player.i;
int goal_j = player.j;
int goalNext_i = goal_i;
int goalNext_j = goal_j;
if (input == 'c')
{
item = 3;
Change();
}
if (input == 'i')
{
item = 5;
if (item == 5)
FindAllPath();
}
if (input == 'o')
{
item = 6;
if (item == 6)
FindShortPath();
}
if (input == 'a')
{
goal_j = player.j - 1;
goalNext_j = player.j - 1;
direction = 1;
}
else if (input == 'd')
{
goal_j = player.j + 1;
goalNext_j = player.j + 1;
direction = 3;
}
else if (input == 'w')
{
goal_i = player.i - 1;
goalNext_i = player.i - 1;
direction = 2;
}
else if (input == 's')
{
goal_i = player.i + 1;
goalNext_i = player.i + 1;
direction = 4;
}
if (map[key][goal_i][goal_j] == 1 || map[key][goal_i][goal_j] == 7 || map[key][goal_i][goal_j] == 8
|| map[key][goalNext_i][goalNext_j] == 1 || map[key][goal_i][goal_j] == 7 || map[key][goal_i][goal_j] == 8)
{
player.i = goal_i;
player.j = goal_j;
}
if (map[key][goal_i][goal_j] == 4)
{
player.i = goal_i;
player.j = goal_j;
player.j = goal_j;
}
}
else//其他情况不予考虑
return;
}