Advertisement
a5768549

Untitled

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