Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <windows.h>
- #define ROW 7
- #define COL 15
- using namespace std;
- // 堆疊結構的宣告
- class stack_node
- {
- friend class Maze;
- public:
- stack_node(int x_, int y_);
- private:
- int x;
- int y;
- stack_node *next;
- };
- typedef class stack_node node;
- class Maze{
- public:
- Maze(int mazeCopy[][COL]);
- void print();
- void push(int x, int y);
- void pop(int& x, int& y);
- void clearStack(); // not Done yet
- bool goLeft(int x, int y);
- bool goRight(int x, int y);
- bool goUp(int x, int y);
- bool goDown(int x, int y);
- void walk(int x, int y, int xEnd, int yEnd);
- private:
- int maze[ROW][COL];
- node* head;
- enum Status{PASSED = 2,DEADLOCK = 3, CLEAR = 0, BLOCK = 1};;
- };
- // 主程式
- int main()
- {
- // 輸入
- int mazeCopy[ROW][COL] = //
- {
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1,
- 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1,
- 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1,
- 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1,
- 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
- };
- Maze myMaze(mazeCopy);
- myMaze.walk(13,5,1,1);// (xStart, yStart, xEnd, yEnd)
- myMaze.print(); cout << endl;
- return 0;
- }
- stack_node::stack_node(int x_, int y_):x(0),y(0),next(NULL){
- x = x_;
- y = y_;
- }
- Maze::Maze(int mazeCopy[][COL]){
- for (int i = 0; i < ROW; i++){
- for (int j = 0; j < COL; j++){
- maze[i][j] = mazeCopy[i][j];
- }
- }
- }
- void Maze::print(){
- for (int i = 0; i < ROW; i++){
- for (int j = 0; j < COL; j++){
- cout << maze[i][j] << " ";
- }
- cout << endl;
- }
- }
- void Maze::push(int x, int y){
- node* new_node = new node(x,y);
- if (!new_node) exit(0); // 記憶體配置失敗!
- new_node->next = head;
- head = new_node;
- }
- void Maze::pop(int& x, int& y){
- node *top;
- if (head != NULL){
- top = head;
- head = head->next;
- x = top->x;
- y = top->y;
- delete top;
- }else{
- x = -1;
- y = -1;
- }
- }
- bool Maze::goLeft(int x, int y){
- return maze[y][x-1] <= 0;
- }
- bool Maze::goRight(int x, int y){
- return maze[y][x+1] <= 0;
- }
- bool Maze::goUp(int x, int y){
- return maze[y-1][x] <= 0; // 注意:往上是y-1
- }
- bool Maze::goDown(int x, int y){
- return maze[y+1][x] <= 0; // 注意:往下是y+1
- }
- // 重點:之後還有有路走時,才會把現在的位置放入堆疊中!
- void Maze::walk(int x, int y, int xEnd, int yEnd)
- {
- while(1)
- {
- if(x == xEnd && y == yEnd) // 是否為出口
- {
- push(x, y);
- maze[y][x] = PASSED; // 標示最後一點
- break;
- }
- maze[y][x] = PASSED; // 標示為走過的路
- if( goUp(x,y) ) // 往上方走
- push(x, y--);
- else if( goDown(x,y) ) // 往下方走
- push(x, y++);
- else if( goLeft(x,y)) // 往左方走
- push(x--, y);
- else if( goRight(x,y) ) // 往右方走
- push(x++, y);
- else // 無路可走 => 回朔
- {
- maze[y][x] = DEADLOCK;
- pop(x, y); // 退回一步
- }
- print(); cout << endl;
- //system("pause");
- Sleep(1000);
- system("cls");
- }// end while
- }
- void Maze::clearStack(){
- node* ptr = head;
- while (!head){
- ptr = head;
- head = head->next;
- cout << "[" << ptr->x << "," << ptr->y << "]" << endl;
- delete ptr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement