現在有 N 個格子,由左至右值分別為 x0, x1, x2, …, xN-1
每個格子上都有一個數字,代表其傳送點編號;
當你踏進格子 i 時,將會觸發其傳送點,並被傳送至編號 xi 的格子中;
之後繼續觸發下個傳送點,依序往復。
傳送點在發揮過一次效力後就會被破壞,
也就是傳送的過程,會在被傳送到「曾經走過」的格子時停止。
現在已知某些格子上放有好吃的布朗尼,
熱愛甜點的你,當然希望能夠吃到越多越多。
給定由編號 T 的格子出發,經過傳送點的傳送,請問最多可以吃到幾個布朗尼?
第一行有兩個正整數 N 和 T
代表總共有 N 個格子 ( 1 ≤ N ≤ 1000 ),並且將從編號 T ( 0 ≤ T ≤ N-1 ) 的格子開始
第二行由左至右有 N 個正整數 xi ( 0 ≤ xi ≤ N-1 )
兩兩之間以空格隔開,代表傳送點資訊
第三行由左至右有 N 個正整數 yi ( yi = 0 或 1 )
兩兩之間以空格隔開,代表該格有 (yi = 1) 或 沒有 (yi = 0) 布朗尼
最多可以吃到幾個布朗尼?
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N = 0, T = 0, tmp = 0, total = 0;
vector<int> v_tp, v_Brownie;
cin >> N >> T;
for (int i = 0; i < N; i++) {
cin >> tmp;
v_tp.push_back(tmp);
}
for (int i = 0; i < N; i++) {
cin >> tmp;
v_Brownie.push_back(tmp);
}
while (v_tp[T] != -1) {
if (v_Brownie[T] == 1) {
total++;
}
tmp = v_tp[T];
v_tp[T] = -1;
T = tmp;
}
cout << total << endl;
return 0;
}
APCS
C++
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing