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.
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.
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\n",ans*1.0/cnt); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 手把手教你更优雅的享受 DeepSeek
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现