# [蜜蜂觀察](https://zerojudge.tw/ShowProblem?problemid=m932) 可以用遊戲的邏輯去解這題,假設人物是在一個建構好的地圖上移動,移動後踩到區塊就輸出區塊的英文字母。 先建構地圖,將題目給的提示用陣列表示(先不要管空間和編排) ## 地圖建構:(此時陣列有m行n列,並且人物會移動k次) ``` int m,n,k; cin>>m>>n>>k; char matrix[m][n]; for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ cin>>matrix[i][j]; } } ``` 之後先將人物放到地圖的左下角,也就是第m-1行的第0列(因為陣列是從0開始的) ## 人物定位: ``` int posX, posY; posX = 0; posY = m - 1; ``` 最後在移動時偵測是否可行就好了,移動後要記得把踩的區塊的英文字母加入最後要輸出的字串中。 ## 人物移動: ``` int input; string ans = ""; for (int i=0;i<k;i++){ cin>>input; if (input == 0){ if ((posY - 1)>=0){ posY -= 1; } } else if (input == 1){ if ((posX + 1)<=(n - 1)){ posX += 1; } } else if (input == 2){ if ((posX + 1)<=(n - 1) and (posY + 1)<=(m - 1)){ posX += 1; posY += 1; } } else if (input == 3){ if ((posY + 1)<=(m - 1)){ posY += 1; } } else if (input == 4){ if ((posX - 1)>=0){ posX -= 1; } } else if (input == 5){ if ((posX - 1)>=0 and (posY - 1)>=0){ posX -= 1; posY -= 1; } } ans = ans + matrix[posY][posX]; } ``` 種類計算的部分能在上方的程式最後加入以下程式來解決(int points=0要在for迴圈外) ## 種類計算: ``` int points = 0; bool repeat = false; for (int i = 0;i<ans.size();i++){ if (matrix[posY][posX] == ans[i]){ repeat = true; break; } } if (repeat == false){ points += 1; } ``` ## Code: ``` #include <bits/stdc++.h> #define it ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); using namespace std; int main(){ it int m,n,k; cin>>m>>n>>k; char matrix[m][n]; for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ cin>>matrix[i][j]; } } int posX, posY; posX = 0; posY = m - 1; int input; string ans = ""; int points = 0; for (int i=0;i<k;i++){ cin>>input; if (input == 0){ if ((posY - 1)>=0){ posY -= 1; } } else if (input == 1){ if ((posX + 1)<=(n - 1)){ posX += 1; } } else if (input == 2){ if ((posX + 1)<=(n - 1) and (posY + 1)<=(m - 1)){ posX += 1; posY += 1; } } else if (input == 3){ if ((posY + 1)<=(m - 1)){ posY += 1; } } else if (input == 4){ if ((posX - 1)>=0){ posX -= 1; } } else if (input == 5){ if ((posX - 1)>=0 and (posY - 1)>=0){ posX -= 1; posY -= 1; } } bool repeat = false; for (int i = 0;i<ans.size();i++){ if (matrix[posY][posX] == ans[i]){ repeat = true; break; } } if (repeat == false){ points += 1; } ans = ans + matrix[posY][posX]; } cout<<ans<<endl; cout<<points<<endl; return 0; } ``` (第一次寫這類詳解,有寫不好或寫錯的地方都能說)