-
Notifications
You must be signed in to change notification settings - Fork 0
/
frogposition.cpp
59 lines (58 loc) · 1.03 KB
/
frogposition.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
struct node
{
int val;
int time;
double prob;
};
node* createNode()
{
node* ret=(node*)malloc(sizeof(node));
ret->val=0;
ret->time=0;
ret->prob=0;
return ret;
}
double frogPosition(int n, vector<vector<int>>& edges, int t, int target)
{
vector<vector<int>> adj(n+1);
vector<int> grey(n+1,0);
for(auto v:edges)
{
adj[v[0]].push_back(v[1]);
adj[v[1]].push_back(v[0]);
}
node* u=createNode();
u->val=1;
u->prob=1;
grey[1]=1;
queue<node*> q;
q.push(u);
while(!q.empty())
{
u=q.front();
q.pop();
int flag=0;
if(t==u->time&&u->val==target)
return u->prob;
else
{
for(int v:adj[u->val])
if(!grey[v])
++flag;
if(flag)
for(int v:adj[u->val])
if(!grey[v])
{
grey[v]=1;
node* temp=createNode();
temp->val=v;
temp->time=u->time+1;
temp->prob=u->prob*(1.0/flag);
q.push(temp);
}
if(t>u->time&&u->val==target&&flag==0)
return u->prob;
}
}
return 0;
}