-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path688. Knight Probability in Chessboard.cpp
45 lines (44 loc) · 1.3 KB
/
688. Knight Probability in Chessboard.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
class Solution {
public:
double knightProbability(int n, int k, int r, int c)
{
vector<pair<int,int>>d={{2,1},{-2,1},{2,-1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
vector<vector<double>>dp1(n,vector<double>(n,0.0));
vector<vector<double>>dp2(n,vector<double>(n,0.0));
dp1[r][c]=1.0;
auto is_safe=[&](int x,int y)
{
return x>=0&&y>=0&&x<n&&y<n;
};
for(int move=1;move<=k;move++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
double ans=(dp1[i][j]/8.0);
for(auto it:d)
{
int nx=i+it.first;
int ny=j+it.second;
if(is_safe(nx,ny))
{
dp2[nx][ny]+=ans;
}
}
}
}
dp1=dp2;
dp2=vector<vector<double>>(n,vector<double>(n,0.0));
}
double ans=0.0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
ans+=dp1[i][j];
}
}
return ans;
}
};