APCS 2024/06 實作題題解 — C++
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
此筆記為APCS 2024年06月實作題考試的題目詳解。每一題的題解都包含解題思路、C++範例程式碼。
第一題 特技表演 (ZeroJudge o076.)
有一個城鎮有 棟高樓,樓高分別為 1, 2, 3,市長想要在城鎮中心舉辦高空特技表演,該特技表演會從某棟大樓上朝右側滑翔至地面。為了表演人員的安全,滑翔的路徑樓高必須越來越低,請你找出一個最長的滑翔路徑。
輸入 / 輸出說明
輸入說明 |
輸出說明 |
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
|
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
|
解題思路
這題只需要儲存前一樓高 lh 與 當前樓高 h,並依據以下規則執行:
- 當前樓高 >= 前一樓高 => 不滿足條件,將路徑長度 t 設為 1
- 當前樓高 < 前一樓高 => 將路徑長度 t + 1
若當前路徑長度 t > 最大路徑長度 max,則將 max 設為 t。
範例程式碼
運行結果
AC (2ms, 344KB)
第二題 電子畫布 (ZeroJudge o077.)
有一個 的電子畫布,一開始數值都是 代表未填色,接下來請模擬 次畫筆操作。
每次畫筆操作為選一個座標 停留 秒,他會將曼哈頓距離 的區塊染上顏色 。若有多個顏色重複填到相同區塊,顏色的數值會累加起來。
請輸出 次操作後的畫布狀態。
輸入 / 輸出說明
輸入說明 |
輸出說明 |
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
|
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
|
解題思路
因為曼哈頓距離 <= t 的座標 (j, k) 一定滿足 ,因此我透過巢狀迴圈檢查 、 範圍內的座標是否滿足曼哈頓距離 ,滿足條件則將位置的顏色染上 ,即 canva[j][k] = canva[j][k] + x。
範例程式碼
運行結果
AC (3ms, 344KB)
第三題 缺字問題 (ZeroJudge o078.)
給定一個大小為 個字母的集合和字串 ,求出在使用該集合所組出長度為 字串中,不為字串 子字串的最小字典序字串為何。
例如字母集合 {a, c, m},其能組出長度為 的字串字典序由小到大排序為 。字串 為 ,因此 為不在 內的最小字典序字串。
輸入 / 輸出說明
輸入說明 |
輸出說明 |
 |
 |
解題思路
這個解法時間複雜度較高,請斟酌參考。
我這題使用遞迴的方法解。
透過範例測資#1為例,解釋遞迴邏輯:
- 第一次遞迴選擇 'a' 加入暫存字串 = "a",此時 t = 0
- 第二次遞迴選擇 'a' 加入暫存字串 = "aa",此時 t = 1
- 第三次遞迴的 t = l = 2,返回第二次遞迴
- 第二次遞迴選擇 'c' 加入暫存字串 = "ac",此時 t = 2
以此類推
t >= l 時(例如上述 3.)的字串為 ,在字串 中尋找與 相同的子字串,若無符合的子字串,輸出 並結束遞迴,即可得解答(題目說輸入的字串 由小到大排序,因此使用上述遞迴規則所得之暫存字串會由小到大)。
範例程式碼
運行結果
AC (0.3s, 432KB)