zoukankan      html  css  js  c++  java
  • dfs房间存活

    Caesar has been betrayed! He always thought his best friends were the senators of Rome, but it turns out they are trying to kill him. Even his best friend, Marcus Junius Brutus, is in on the plot. Luckily, he’s managed to get away, for now. If he can make it out of the senate he’ll be okay, and he can overthrow the senate and take over Rome. 
    Unfortunately, he’s a little rattled, and is not making smart decisions. Every minute, he is randomly choosing which room he enters and hides in for the next minute. In each room there is a p% chance that he will be captured. After m minutes the senators will give up the search and go back to their homes, allowing him to escape. Calculate the chance that he will escape if he chooses rooms at random.

    输入

    The first line of the input will be a single integer, n ≤ 1, 000. There will be n test cases that follow.
    Each test case will start with a line of 2 integers: the number of rooms 5 ≤ r ≤ 50 and the number of minutes Caesar must survive, m ≤ 10. The next r lines will consist of a floating point number p < 1 denoting the probability that Caesar survives for the next minute in this room and 4 integers between 1 and r (inclusive on both ends) denoting which rooms the current room is connected to. Caesar always spends the first minute in room 1. 
    Note: The senate is weird, just because room a leads to room b does not mean Caesar can get back into room a from room b.

    输出

    Output a single floating point number denoting the probability that Caesar survives. The output should be accurate and truncate to three decimal places.

    样例输入 Copy

    1
    5 3
    0.50 2 3 4 5
    0.25 1 3 4 5
    0.30 1 2 4 5
    0.80 1 2 3 5
    0.60 1 2 3 4
    

    样例输出 Copy

    0.113


    意思就是说输入n,m,就是说有n个点的房间,要在这个迷宫中存活m分钟的平均概率
    n行每一行一个p[i]为在第i个房间的存活概率,后面4个数为第i号房间能通往的房间
    #include<iostream>
    #include<stdio.h>
    using namespace std;
    #include <set>
    #include<vector>
    #include<algorithm>
    #define ll long long
    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include<queue>
    #include<stack>
    #include<time.h>
    #include<map>
    //#include<bits/stdc++.h>
    //srand((unsigned)time(NULL));
    
    const int maxn =3e5+7;
    const ll mod = 1e9+7;
    const ll INF=1e18+7;
    const int inf=1e9;
    const ll maxx=1e6+7;
    
    double a[1200];
    int n,m;
    vector<int>v[120];
    double ans;
    int cnt=0;
    void dfs(int u,double sum,int len){
        if(len==m){
            ans+=sum;
            cnt++;
            return ;
        }
        for(int i=0;i<4;i++){
            dfs(v[u][i],sum*a[v[u][i]],len+1);
        }
    }
    
    int main(){
        int t;
        cin>>t;
        while(t--){
            cin>>n>>m;
            int x;
            for(int i=1;i<=n;i++){
                scanf("%lf",&a[i]);
                for(int j=1;j<=4;j++){
                    cin>>x;
                    v[i].push_back(x);
                }
            }
            ans=0;cnt=0;
            dfs(1,a[1],1);
            for(int i=0;i<=n;i++) v[i].clear(); 
            printf("%.3lf
    ",ans*1.0/cnt);
        }
    }
     
  • 相关阅读:
    netcore---Program.cs配置相关信息,及读取配置信息
    js
    python3 openssl问题(贼有用)
    Flask+微信公众号开发(接入指南)
    运维角度处理跨域问题
    羞羞的Python模块包
    Nginx之伪404( root与alias )
    【深度学习】:梯度下降,随机梯度下降(SGD),和mini-batch梯度下降
    【深度学习】:小白也能看懂的卷积神经网络
    《剑指offer》5:替换空格
  • 原文地址:https://www.cnblogs.com/lipu123/p/14630167.html
Copyright © 2011-2022 走看看