初级农民-请到新手上路获取积分
- 积分
- 7
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2019-3-27
- 最后登录
- 1970-1-1
|
本帖最后由 ChenXFRY 于 2022-11-15 15:58 编辑
- #include <iostream>
- #include <numeric>
- #include <vector>
- using namespace std;
- /*
- double p = 0.9; // caught when passing vertically to adjacent rows
- double q = 0.2; // caught when passing horizontally to adjacent cols
- */
- double caughtPossibility(vector<pair<int, int>> &students, double p, double q) {
- // source: students[0]
- // dest: students.back()
-
- // 1 - (not caught by teacher) * ... * (not caught by teacher)
- // we find the product of posiblity of all edges, then 1-X is the answer
- /*
- int r = 1;
- double pToNextCol = p * pow(0.5, r);
- double pToNextRow = q * pow(0.5, r);
- */
- // move from [sr,sc] to [dr, dc]
- auto probablityToMove = [&](pair<int, int>& from, pair<int, int>& to) {
- auto [sr, sc] = from;
- auto [dr, dc] = to;
-
- // not adjacent cells...
- if (abs(dr - sr) > 1 && abs(dc - sc) > 1) {
- return 0.0;
- }
- // same position, do not need calculate.
- if (dr == sc && dc == sc) {
- return 1.0;
- }
-
- // move on the same colume
- if (abs(dr - sr) == 1) {
- return p * pow(0.5, dr > sr ? sr : dr);
- }
- // move on the same row
- if (abs(dc - sc) == 1) {
- return q * pow(0.5, sr);
- }
-
- // not reachable
- return 0.0;
- };
-
- // given the path
- double notByCaught = 1.0;
- for (int i = 1; i < students.size(); i++) {
- notByCaught *= 1 - probablityToMove(students[i - 1], students[i]);
- }
- return 1 - notByCaught;
- }
- int main(int argc, char *argv[]) {
- double p = 0.5, q = 0.9;
- vector<pair<int, int>> path = {
- {0, 1},
- {0, 2},
- {0, 3}
- };
- cout << caughtPossibility(path, p, q);
- }
复制代码 |
|