-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoupServings
More file actions
99 lines (86 loc) · 1.66 KB
/
SoupServings
File metadata and controls
99 lines (86 loc) · 1.66 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//https://leetcode.com/contest/weekly-contest-78/problems/soup-servings/
//
// Created by Avinash Kumar on 3/31/18.
//
//Solution valid for small N
#include<iostream>
#include<string>
#include<vector>
#include "math.h"
using namespace std;
void ChangeVal(int i,int j, vector<vector<double>> &prob)
{
int x = (i-4>=0)?i-4:0;
int y = j;
if(prob[x][y] > 0)
{
prob[x][y] += prob[i][j]*0.25;
}
else
{
prob[x][y] = prob[i][j]*0.25;
}
x = (i-3>=0)?i-3:0;
y = (j-1>=0)?j-1:0;
if(prob[x][y] > 0)
{
prob[x][y] += prob[i][j]*0.25;
}
else
{
prob[x][y] = prob[i][j]*0.25;
}
x = (i-2>=0)?i-2:0;
y = (j-2>=0)?j-2:0;
if(prob[x][y] > 0)
{
prob[x][y] += prob[i][j]*0.25;
}
else
{
prob[x][y] = prob[i][j]*0.25;
}
x = (i-1>=0)?i-1:0;
y = (j-3>=0)?j-3:0;
if(prob[x][y] > 0)
{
prob[x][y] += prob[i][j]*0.25;
}
else
{
prob[x][y] = prob[i][j]*0.25;
}
}
double FindSol(int N)
{
int highLim = ceil(N*1.0/25.0);
vector<vector<double>> prob;
prob.resize(highLim+1);
for(int i=0; i<highLim+1;i++)
{
prob[i].resize(highLim+1,-1.0);
}
prob[highLim][highLim] = 1.0;
for(int i=highLim; i>=1; i--)
{
for(int j=highLim; j>=1; j--)
{
if((i==highLim && j==highLim) || prob[i][j] != -1)
{
ChangeVal(i,j, prob);
}
}
}
double ret = 0.0;
for(int i=1;i<=highLim; i++)
{
ret += prob[0][i];
}
ret += prob[0][0]/2;
cout<< ret;
}
int main()
{
int N=100;
cout << FindSol(N);
}