- #include <iostream>
- #include <stack>
- #include <Windows.h>
- #include <conio.h>
- using namespace std;
- #define R 7
- #define C 9
- int map[R][C] = {
- {1, 1, 1, 1, 1, 1, 1, 1, 1},
- {1, -1, 1, 1, 0, 1, 1, 0, 1},
- {1, 0, 0, 1, 0, 1, 1, 0, 1},
- {1, 1, 0, 0, 0, 1, 1, 0, 1},
- {1, 1, 1, 0, 0, 0, 0, 0, 1},
- {1, 0, 0, 0, 1, 0, 1, 0, 1},
- {1, 1, 1, 1, 1, 1, 1, 1, 1},
- };
- struct Position
- {
- int x;
- int y;
- };
- struct Dir
- {
- int dx;
- int dy;
- };
- const Dir dir[] = {
- //up
- {0, -1},
- //down
- {0, 1},
- //left
- {-1, 0},
- //right
- {1, 0}
- };
- void printMaze()
- {
- for(int i = 0; i < R; i++)
- {
- for(int j = 0; j < C; j++)
- {
- if(map[i][j] == 1)
- {
- cout << "■";
- }
- else if(map[i][j] == -1)
- {
- cout << "@ ";
- }
- else
- {
- cout << " ";
- }
- }
- cout << endl;
- }
- }
- void hiddenCursor()//隐藏光标
- {
- HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cci;
- GetConsoleCursorInfo(hOut,&cci);
- cci.bVisible=0;//赋1为显示,赋0为隐藏
- SetConsoleCursorInfo(hOut,&cci);
- }
- int main()
- {
- hiddenCursor();
- cout << "Mouse start to research..." << endl;
- printMaze();
- stack<Position> stack;
- Position currPos = {1, 1};
- map[1][1] = -1;
- stack.push(currPos);
- int count = 0;
- while(currPos.x != 7 || currPos.y != 5)
- {
- system("cls");
- cout << "I'm just thinking" << '(' << count <<')' << ",don't disturb me!" << endl;
- printMaze();
- Sleep(1000);
- for(int i = 0; i < 4; i++)
- {
- int x = currPos.x + dir[i].dx;
- int y = currPos.y + dir[i].dy;
- if(map[y][x] == 0)
- {
- map[y][x] = -1;
- currPos.x = x;
- currPos.y = y;
- stack.push(currPos);
- int test = map[y][x];
- break;
- }
- if(i == 3)
- {
- map[currPos.y][currPos.x] = -2;
- stack.pop();
- currPos = stack.top();
- }
- }
- count++;
- }
- cout << "Aha, I find the exit.Nice!" << endl;
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1709201410460.html
来源: http://www.codesnippet.cn/detail/1709201410460.html