Advertisement
a5768549

Untitled

Jun 15th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.81 KB | None | 0 0
  1. #include <stdio.h>  
  2. #include <windows.h>
  3. #include <string>
  4.  
  5. //設定全域數值  
  6. #define FILE_MAP "input_map.txt"
  7. #define FILE_OUTPUT "output_route.txt"      //人機輸出
  8. #define MAP_MAXSIZE_X 100          
  9. #define MAP_MAXSIZE_Y 100                  
  10. #define MAX_STACK (MAP_MAXSIZE_X*MAP_MAXSIZE_Y)/2  
  11. #define bool int  
  12. #define true 1  
  13. #define false 0                            //設定參數
  14.  
  15. int max_x=0, max_y=0;
  16. char map[MAP_MAXSIZE_X][MAP_MAXSIZE_Y]={1};  
  17. bool passed[MAP_MAXSIZE_X][MAP_MAXSIZE_Y]={0};    //設定初始值
  18.  
  19. struct _POSITION           // 設定座標結構
  20. {                  
  21.     int x;  
  22.     int y;  
  23. };  
  24.  
  25. typedef struct _POSITION POSITION;  
  26. POSITION stack[MAX_STACK];         //堆疊數據
  27. int top = -1;                      //最頂堆疊數據
  28.  
  29. //如果堆疊是空的
  30. bool Empty()  
  31. {  
  32.     if(top == -1)
  33.         return true;
  34.     return false;
  35. }  
  36.  
  37. // 如果堆疊是滿的
  38. bool Full()  
  39. {  
  40.     if(top < MAX_STACK-1)
  41.         return false;
  42.     return true;
  43. }  
  44.  
  45. // 從堆疊跳出
  46. POSITION Pop()
  47. {
  48.     return stack[top--];
  49. }
  50.  
  51. // 把數據寫入堆疊
  52. void Push(POSITION posData)
  53. {
  54.     stack[++top]=posData;
  55. }
  56.  
  57. void run(int x, int y)
  58. {
  59.     FILE *fptr;
  60.     POSITION last; //最後坐標資訊
  61.     unsigned int branch = 0; //分支碼
  62.     int direction = 1; // 1:東  2:南  3:西  4:北
  63.      
  64.     fptr = fopen(FILE_OUTPUT, "w");  
  65.        
  66.     while(map[x][y] != 'O') //達到目標前持續做
  67.     {  
  68.         branch = 0;         // 分支數據清零
  69.         passed[x][y] = 1;   // 設定此位置可通過
  70.          
  71.         // 指令:選一條路走  
  72.         if((y+1<max_y) && ((map[x][y+1] == 'O') || (map[x][y+1] == '0'))  // 假如東方可以過(尚未過去)
  73.             && (passed[x][y+1] == 0))  
  74.         {                    
  75.             branch++;         // 增加分支(假如這可以過)
  76.             direction = 1;  
  77.         }  
  78.         if((x+1<max_x) && ((map[x+1][y] == 'O') || (map[x+1][y] == '0'))  // 假如西方可以過(尚未過去)
  79.             && (passed[x+1][y] == 0))  
  80.         {  
  81.             branch++;         // 增加分支(假如這可以過)
  82.             direction = 2;  
  83.         }  
  84.         if((y-1>=0) && ((map[x][y-1] == 'O') || (map[x][y-1] == '0'))     // 假如南方可以過(尚未過去)
  85.             && (passed[x][y-1] == 0))  
  86.         {  
  87.             branch++;         // 增加分支(假如這可以過)
  88.             direction = 3;  
  89.         }  
  90.         if((x-1>=0) && ((map[x-1][y] == 'O') || (map[x-1][y] == '0'))     // 假如北方可以過(尚未過去)
  91.             && (passed[x-1][y] == 0))  
  92.         {  
  93.             branch++;         // 增加分支(假如這可以過)
  94.             direction = 4;  
  95.         }  
  96.          
  97.         // 指令:前進
  98.         if(branch > 1)       //假如分支不只一個
  99.         {  
  100.             if(!Full())      // 假如堆疊不是滿的(把分支寫入堆疊)
  101.             {
  102.                 last.x = x;  
  103.                 last.y = y;  
  104.                 Push(last);  
  105.                 printf("Push(%d,%d), ", last.x+1, last.y+1);  
  106.                 fprintf(fptr, "Push(%d,%d), ", last.x+1, last.y+1);  
  107.             }
  108.         }
  109.         else if(branch == 0) // 假如不能通過
  110.         {  
  111.             printf("遇到死路, ");  
  112.             fprintf(fptr, "遇到死路, ");  
  113.             if(!Empty())  
  114.             {  
  115.                 last = Pop();  
  116.                 x = last.x;  
  117.                 y = last.y;  
  118.                 printf("(%d,%d) = Pop(), ", last.x+1, last.y+1);  
  119.                 fprintf(fptr, "(%d,%d) = Pop(), ", last.x+1, last.y+1);  
  120.             }  
  121.             else  
  122.             {  
  123.                 printf("無法離開\n");  
  124.                 fprintf(fptr, "無法離開\n");  
  125.                 fclose(fptr);  
  126.                 return;  
  127.             }  
  128.             continue;  
  129.         }  
  130.          
  131.         //指令:在最後的方向選擇一種方式前進
  132.         switch(direction)     //fptr:輸出檔案用
  133.         {                     //printf:人機
  134.             case 1:  
  135.                 y = y + 1;  
  136.                 printf("右, ");  
  137.                 fprintf(fptr, "右, ");  
  138.                 break;  
  139.             case 2:  
  140.                 x = x + 1;  
  141.                 printf("下, ");  
  142.                 fprintf(fptr, "下, ");  
  143.                 break;  
  144.             case 3:  
  145.                 y = y - 1;  
  146.                 printf("左, ");  
  147.                 fprintf(fptr, "左, ");  
  148.                 break;  
  149.             case 4:  
  150.                 x = x - 1;  
  151.                 printf("上, ");  
  152.                 fprintf(fptr, "上, ");  
  153.                 break;  
  154.         }  
  155.     }  
  156.      
  157.     printf("到達\n");  
  158.     fprintf(fptr, "到達\n");  
  159.      
  160.     fclose(fptr);  
  161. }  
  162.  
  163. int main()  
  164. {  
  165.     FILE *fptr;  
  166.     int i,j,start_x,start_y;  
  167.      
  168.     // 指令:加載文件
  169.     fptr = fopen(FILE_MAP,"w");  
  170.     if(!fptr)  
  171.     {  
  172.         printf("File loaded error!\n");  
  173.         return 0;  
  174.     }  
  175.      
  176.     // 指令:讀取文件
  177.     fscanf(fptr, "%d %d\n",&max_x, &max_y);  
  178.     if(max_x==0 || max_y==0)  
  179.         return 0;  
  180.     for(i=0;i<max_x;i++)  
  181.     {  
  182.         for(j=0;j<max_y;j++)  
  183.         {  
  184.            fscanf(fptr, " %c", &map[i][j]);  
  185.            passed[i][j]=0;  
  186.            if(map[i][j]=='M')  
  187.            {  
  188.                start_y=i;  
  189.                start_x=j;  
  190.            }  
  191.         }  
  192.     }  
  193.     fclose(fptr);  
  194.      
  195.     // 指令:匯出迷宮圖
  196.     for(i=0;i<max_x;i++)  
  197.     {  
  198.         for(j=0;j<max_y;j++)  
  199.         {  
  200.            printf("%c ", map[i][j]);  
  201.         }  
  202.         printf("\n");  
  203.     }  
  204.      
  205.     run(start_y,start_x); // 跑迷宮
  206.      
  207.     system("pause");  
  208.     return 0;  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement