#include <iostream>
#include "GDXL.h"
using namespace std;
int map[20][20] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{3,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,4},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
int main()
{
GDXL a(0, 10, 19, 10, 20, 20, &map[0][0]);
//a.setmap();
a.xunlu();
a.shuchu();
return 0;
}
#pragma once
#include <iostream>
#include <list>
#include <cmath>
#include <Windows.h>
using namespace std;
typedef struct dian
{
int y;
int x;
struct dian* befront;
}Dian;
class GDXL
{
private:
list <Dian*> listdian;
list <Dian*> shifang;
public:
int StarX;
int StarY;
int EndX;
int EndY;
int X;
int Y;
int* map;
GDXL(int sx, int sy, int ex, int ey, int x, int y, int* pp);
void setmap();
bool isCanMove(int l, int r);
void xunlu();
void shuchu();
};
#include "GDXL.h"
GDXL::GDXL(int sx, int sy, int ex, int ey, int x, int y, int* pp)
{
this->StarX = sx;
this->StarY = sy;
this->EndX = ex;
this->EndY = ey;
this->X = x;
this->Y = y;
this->map = pp;
}
void GDXL::setmap()
{
system("cls");
for (size_t i = 0; i < Y; i++)
{
for (size_t j = 0; j < X; j++)
{
cout << *(map + (i * X) + j);
}
cout << endl;
}
cout << endl;
}
bool GDXL::isCanMove(int x, int y)
{
if (x<0 || x>(this->X - 1) || y<0 || y>(this->Y - 1))
{
return false;
}
if (*(map + (y * this->X) + x) == 1 || *(map + (y * (this->X)) + x) == 2 || *(map + (y * this->X) + x) == 3)
{
return false;
}
return true;
}
void GDXL::xunlu()
{
int t_x;
int t_y;
Dian* t = new Dian;
t->x = this->StarX;
t->y = this->StarY;
t->befront = NULL;
listdian.push_back(t);
shifang.push_back(t);
do
{
if (listdian.empty())
{
break;
}
t = listdian.front();
t_x = t->x;
t_y = t->y;
if (t_x == EndX && t_y == EndY)
{
break;
}
//左
if (isCanMove(t_x - 1, t_y))
{
*(map + (t_y * (this->X)) + t_x - 1) = 2;
Dian* nks = new Dian();
nks->x = t_x - 1;
nks->y = t_y;
nks->befront = t;
listdian.push_back(nks);
shifang.push_back(nks);
nks = NULL;
}
//下
if (isCanMove(t_x, t_y + 1))
{
*(map + ((t_y + 1) * this->X) + t_x) = 2;
Dian* nks = new Dian();
nks->x = t_x;
nks->y = t_y + 1;
nks->befront = t;
listdian.push_back(nks);
shifang.push_back(nks);
nks = NULL;
}
//右
if (isCanMove(t_x + 1, t_y))
{
*(map + (t_y * this->X) + t_x + 1) = 2;
Dian* nks = new Dian();
nks->x = t_x + 1;
nks->y = t_y;
nks->befront = t;
listdian.push_back(nks);
shifang.push_back(nks);
nks = NULL;
}
//上
if (isCanMove(t_x, t_y - 1))
{
*(map + ((t_y - 1) * this->X) + t_x) = 2;
Dian* nks = new Dian();
nks->x = t_x;
nks->y = t_y - 1;
nks->befront = t;
listdian.push_back(nks);
shifang.push_back(nks);
nks = NULL;
}
t = NULL;
listdian.pop_front();
//setmap();
if (listdian.empty())
{
cout << "无路可走" << endl;
}
//Sleep(10);
} while (true);
}
void GDXL::shuchu()
{
//system("cls");
Dian* luxian = NULL;
if (!listdian.empty())
{
luxian = listdian.front();
}
int i = 0;
while (luxian != NULL)
{
*(map + (luxian->y) * this->X + luxian->x) = 5;
luxian = luxian->befront;
i++;
}
while (!shifang.empty())
{
free(shifang.front());
shifang.pop_front();
}
listdian.clear();
shifang.clear();
for (size_t i = 0; i < Y; i++)
{
for (size_t j = 0; j < X; j++)
{
//cout << (*(map + (i * X) + j));
if (*(map + (i * X) + j) == 5)
{
cout << "*";
}
else if (*(map + (i * X) + j) == 1)
{
cout << "#";
}
else
{
cout << " ";
}
}
cout << endl;
}
cout << endl;
cout << i << endl;
}