2016藍橋杯C++A組路徑之謎參考代碼

之前在網上看到的多是Java 但是我沒學過Java..... 找了一篇C的,調了很久很久很久.......
題目如下:
小明冒充X星球的騎士,進入了一個奇怪的城堡。
城堡裏邊什麼都沒有,只有方形石頭鋪成的地面。
假設城堡地面是 n x n 個方格。【如圖1.png】所示。


按習俗,騎士要從西北角走到東南角。
可以橫向或縱向移動,但不能斜着走,也不能跳躍。
每走到一個新方格,就要向正北方和正西方各射一箭。
(城堡的西牆和北牆內各有 n 個靶子)
同一個方格只允許經過一次。但不必走完所有的方格。
如果只給出靶子上箭的數目,你能推斷出騎士的行走路線嗎?
有時是可以的,比如圖1.png中的例子。
本題的要求就是已知箭靶數字,求騎士的行走路徑(測試數據保證路徑唯一)
輸入:
第一行一個整數N(0<N<20),表示地面有 N x N 個方格
第二行N個整數,空格分開,表示北邊的箭靶上的數字(自西向東)
第三行N個整數,空格分開,表示西邊的箭靶上的數字(自北向南)
輸出:
一行若干個整數,表示騎士路徑。
爲了方便表示,我們約定每個小格子用一個數字代表,從西北角開始編號: 0,1,2,3....
比如,圖1.png中的方塊編號爲:
0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15
示例:
用戶輸入:
4
2 4 3 4
4 3 3 3
程序應該輸出:
0 4 5 1 2 3 7 11 10 9 13 14 15

代碼如下:

  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5.   
  6. int N;  
  7. const int max = 20;  
  8. int west[max];  
  9. int north[max];  
  10. int pic[max][max];  
  11. int iv[400];  
  12. int dicx[] = { 1,-1,0,0 };  
  13. int dicy[] = { 0,0,1,-1 };  
  14. int ans[400];  
  15. int num;  
  16. bool ok = false;  
  17.   
  18.   
  19. bool isin(int x,int y)  
  20. {  
  21.     if (x < N && x >= 0 && y < N && y >= 0)  
  22.     {  
  23.         return true;  
  24.     }  
  25.     else  
  26.     {  
  27.         return false;  
  28.     }  
  29. }  
  30. bool J()  
  31. {  
  32.     for (int i = 0; i < N; i++)  
  33.     {  
  34.         if (north[i] != 0)  
  35.             return false;  
  36.     }  
  37.     for (int i = 0; i < N; i++)  
  38.     {  
  39.         if (west[i] != 0)  
  40.             return false;  
  41.     }  
  42.     return true;  
  43. }  
  44. bool isr(int x, int y)  
  45. {  
  46.     if (west[y] >= 0 && north[x] >= 0)  
  47.     {  
  48.         return true;  
  49.     }  
  50.     else  
  51.     {  
  52.         return false;  
  53.     }  
  54. }  
  55. void dfs(int x,int y,int l)  
  56. {  
  57.     if (pic[y][x] == N * N - 1 )//終止條件  
  58.     {  
  59.         if (J())  
  60.         {  
  61.             ans[l] = pic[y][x];  
  62.             ok = true;  
  63.             num = l;  
  64.             return;  
  65.         }  
  66.         else   
  67.         {  
  68.             return;  
  69.         }  
  70.     }  
  71.     if (isin(x, y) && !(bool)iv[pic[y][x]] && isr(x,y))//判斷當前位置是否符合條件 1.在圖內 2.未經過 3.可以經過  
  72.     {  
  73.         iv[pic[y][x]] = 1;//嘗試  
  74.         ans[l] = pic[y][x];  
  75.         for (int i = 0; i < 4; i++)//判斷當前位置能否繼續求解  
  76.         {  
  77.             int y_tem = y + dicy[i];  
  78.             int x_tem = x + dicx[i];  
  79.             if (isin(x_tem, y_tem) && !(bool)iv[pic[y_tem][x_tem]] && north[x_tem] - 1 >=0 && west[y_tem] - 1 >= 0)  
  80.             {  
  81.                 north[x_tem] = north[x_tem] - 1;//嘗試  
  82.                 west[y_tem] = west[y_tem] - 1;  
  83.                 dfs(x_tem, y_tem, l + 1);  
  84.                 if (ok) return;  
  85.                 north[x_tem] = north[x_tem] + 1;//撤銷  
  86.                 west[y_tem] = west[y_tem] + 1;  
  87.             }  
  88.         }  
  89.         iv[pic[y][x]] = 0;//撤銷    
  90.         return;  
  91.     }  
  92.     else  
  93.         return;  
  94. }  
  95.   
  96. int main()  
  97. {  
  98.     cin >> N;  
  99.   
  100.     for (int i = 0; i < N; i++)  
  101.     {  
  102.         cin >> north[i];  
  103.     }  
  104.     for (int i = 0; i < N; i++)  
  105.     {  
  106.         cin >> west[i];  
  107.     }  
  108.     int k = 0;  
  109.     for (int i = 0; i < N; i++)  
  110.     {  
  111.         for (int n = 0; n < N; n++)  
  112.         {  
  113.   
  114.             pic[i][n] = k;  
  115.             k++;  
  116.         }  
  117.     }  
  118.     for (int i = 0;i < 400; i++)  
  119.     {  
  120.         iv[i] = 0;  
  121.     }  
  122.     west[0]--;  
  123.     north[0]--;  
  124.     dfs(0, 0, 0);  
  125.     for (int i = 0; i<= num; i++)  
  126.     {  
  127.         cout << ans[i] << " ";  
  128.     }  
  129.       
  130.   
  131.     system("pause");  
  132.     return 0;  
  133. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章