-
- #include
- #include "SDXL.h"
- using namespace std;
-
- int map[20][20] = {
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
- {3,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,4},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
- {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
- {0,0,0,0,1,0,1,1,1,1,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,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}
- };
-
- int main()
- {
- SDXL a(0, 10, 19, 10, 20, 20, &map[0][0]);
- //a.setmap();
- a.xunlu();
- a.shuchu();
- return 0;
- }
- #pragma once
- #include
- #include
- #include
- #include
- using namespace std;
- typedef struct dian
- {
- int y;
- int x;
- struct dian* befront;
- }Dian;
- class SDXL
- {
- private:
-
- list
listdian; - list
shifang; - public:
- int StarX;
- int StarY;
- int EndX;
- int EndY;
- int X;
- int Y;
- int* map;
- SDXL(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 "SDXL.h"
-
- SDXL::SDXL(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 SDXL::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 SDXL::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 SDXL::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;
- }
- int j = 0;
- t = listdian.back();
- t_x = t->x;
- t_y = t->y;
- if (t_x == EndX && t_y == EndY)
- {
- break;
- }
- while (true)
- {
- //顺时针
- if (isCanMove(t_x, t_y - 1))
- {
- if (t_x == EndX && t_y - 1 == EndY)
- {
- j = 1;
- break;
- }
- *(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_x = listdian.back()->x;
- t_y = listdian.back()->y;
- t = listdian.back();
- continue;
- }
- else if (isCanMove(t_x + 1, t_y))
- {
- if (t_x+1 == EndX && t_y == EndY)
- {
- j = 1;
- break;
- }
- *(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;
- t_x = listdian.back()->x;
- t_y = listdian.back()->y;
- t = listdian.back();
- continue;
- }
- else if (isCanMove(t_x, t_y + 1))
- {
- if (t_x == EndX && t_y + 1 == EndY)
- {
- j = 1;
- break;
- }
- *(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_x = listdian.back()->x;
- t_y = listdian.back()->y;
- t = listdian.back();
- continue;
- }
- else if (isCanMove(t_x - 1, t_y))
- {
- if (t_x-1 == EndX && t_y == EndY)
- {
- j = 1;
- break;
- }
- *(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;
- t_x = listdian.back()->x;
- t_y = listdian.back()->y;
- t = listdian.back();
- continue;
- }
- else
- {
- break;
- }
-
- }
- t = NULL;
- if (j == 1)
- {
- break;
- }
-
-
- listdian.pop_back();
-
- //();
- if (listdian.empty())
- {
- cout << "无路可走" << endl;
- }
- //Sleep(10);
- } while (true);
- }
-
- void SDXL::shuchu()
- {
- //system("cls");
- Dian* luxian = NULL;
- if (!listdian.empty())
- {
- luxian = listdian.back();
- }
- 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;
- }
