Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 迷宮.cpp : 定義主控台應用程式的進入點。
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- #define mapsize 10
- int map[mapsize][mapsize] = {
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
- { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
- { 1, 0, 1, 1, 0, 1, 1, 0, 0, 1 },
- { 1, 0, 1, 0, 0, 1, 0, 0, 1, 1 },
- { 1, 0, 1, 0, 1, 0, 1, 0, 0, 1 },
- { 1, 0, 0, 0, 0, 0, 1, 0, 1, 1 },
- { 1, 1, 0, 1, 1, 0, 0, 0, 0, 1 },
- { 1, 0, 0, 1, 1, 1, 0, 1, 0, 1 },
- { 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
- { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, };
- class mouse
- {
- public:
- int startx, starty, endx, endy;
- void setstart(int x, int y)
- {
- startx = x; starty = y;
- }
- void setend(int x, int y)
- {
- endx = x; endy = y;
- }
- void printmap();
- void go(){ cout << "start from: " << startx << ' ' << starty << endl << "to :" << endx << ' ' << endy << endl; visit(startx, starty); }
- private:
- void visit(int, int);
- };
- void mouse::visit(int x, int y)
- {
- //cout << endl;
- //printmap();
- map[x][y] = 2;
- if (x == endx&&y == endy)
- {
- cout << "到達終點\n";
- printmap();
- //system("PAUSE");
- }
- if (map[x][y + 1] == 0)//right
- {
- visit(x, y + 1);
- map[x][y + 1] = 0;
- }
- if (map[x + 1][y] == 0)//down
- {
- visit(x + 1, y);
- map[x + 1][y] = 0;
- }
- if (map[x][y - 1] == 0)//left
- {
- visit(x, y - 1);
- map[x][y - 1] = 0;
- }
- if (map[x - 1][y] == 0)//up
- {
- visit(x - 1, y);
- map[x - 1][y] = 0;
- }
- map[x][y] = 0;
- }
- void mouse::printmap()
- {
- for (int i = 0; i < mapsize; i++)
- {
- printf("\t");
- for (int j = 0; j < mapsize; j++)
- {
- if (map[i][j] == 1)
- {
- printf("■");
- }
- else if (map[i][j] == 2)
- {
- printf("◆");
- }
- else
- {
- printf(" ");
- }
- }
- printf("\n");
- }
- cout << endl;
- }
- int main()
- {
- mouse mouse1;
- mouse1.setstart(1, 1);
- mouse1.setend(mapsize - 2, mapsize - 2);
- mouse1.go();
- //mouse1.printmap();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement