Advertisement
gha890826

mouse walking map

Oct 25th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1.  
  2. // 迷宮.cpp : 定義主控台應用程式的進入點。
  3. //
  4. #include "stdafx.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8.  
  9. #define mapsize 10
  10.  
  11. int map[mapsize][mapsize] = {
  12.     { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  13.     { 1, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
  14.     { 1, 0, 1, 1, 0, 1, 1, 0, 0, 1 },
  15.     { 1, 0, 1, 0, 0, 1, 0, 0, 1, 1 },
  16.     { 1, 0, 1, 0, 1, 0, 1, 0, 0, 1 },
  17.     { 1, 0, 0, 0, 0, 0, 1, 0, 1, 1 },
  18.     { 1, 1, 0, 1, 1, 0, 0, 0, 0, 1 },
  19.     { 1, 0, 0, 1, 1, 1, 0, 1, 0, 1 },
  20.     { 1, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
  21.     { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, };
  22.  
  23. class mouse
  24. {
  25. public:
  26.     int startx, starty, endx, endy;
  27.     void setstart(int x, int y)
  28.     {
  29.         startx = x; starty = y;
  30.     }
  31.     void setend(int x, int y)
  32.     {
  33.         endx = x; endy = y;
  34.     }
  35.     void printmap();
  36.     void go(){ cout << "start from: " << startx << ' ' << starty << endl << "to :" << endx << ' ' << endy << endl; visit(startx, starty); }
  37. private:
  38.     void visit(int, int);
  39. };
  40.  
  41. void mouse::visit(int x, int y)
  42. {
  43.     //cout << endl;
  44.     //printmap();
  45.     map[x][y] = 2;
  46.     if (x == endx&&y == endy)
  47.     {
  48.         cout << "到達終點\n";
  49.         printmap();
  50.         //system("PAUSE");
  51.     }
  52.     if (map[x][y + 1] == 0)//right
  53.     {
  54.         visit(x, y + 1);
  55.         map[x][y + 1] = 0;
  56.     }
  57.     if (map[x + 1][y] == 0)//down
  58.     {
  59.         visit(x + 1, y);
  60.         map[x + 1][y] = 0;
  61.     }
  62.     if (map[x][y - 1] == 0)//left
  63.     {
  64.         visit(x, y - 1);
  65.         map[x][y - 1] = 0;
  66.     }
  67.     if (map[x - 1][y] == 0)//up
  68.     {
  69.         visit(x - 1, y);
  70.         map[x - 1][y] = 0;
  71.     }
  72.     map[x][y] = 0;
  73. }
  74.  
  75. void mouse::printmap()
  76. {
  77.     for (int i = 0; i < mapsize; i++)
  78.     {
  79.         printf("\t");
  80.         for (int j = 0; j < mapsize; j++)
  81.         {
  82.             if (map[i][j] == 1)
  83.             {
  84.                 printf("■");
  85.             }
  86.             else if (map[i][j] == 2)
  87.             {
  88.                 printf("◆");
  89.             }
  90.             else
  91.             {
  92.                 printf(" ");
  93.             }
  94.         }
  95.         printf("\n");
  96.     }
  97.     cout << endl;
  98. }
  99.  
  100. int main()
  101. {
  102.     mouse mouse1;
  103.     mouse1.setstart(1, 1);
  104.     mouse1.setend(mapsize - 2, mapsize - 2);
  105.     mouse1.go();
  106.     //mouse1.printmap();
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement