Topic links:
Topic analysis:
Obviously the process can be divided into the following process
- You can go forward to the finish
- Closest to turn back \ (180 ° \) degrees
- You can go back to the finish
- Place finish turn the rest of the rotation operation
last step can not control, because anyway, no displacement
What is the need to operate to turn back the action, similar \ (0/1 \) thinking backpack \ (DP \) it, then what can the law of cosines
Code:
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
return cnt * f;
}
int n, x, f, b, p = 1e3;
const double PI = 3.1415926535;
int trn[55], cnt;
double ans;
bool dp[55][361];
char c[10];
int main() {
n = read();
for (register int i = 1; i <= n; ++i) {
scanf("%s", c + 1), x = read();
if (c[1] == 'f') f += x;
if (c[1] == 'b') b += x;
if (c[1] == 'r') trn[++cnt] = -x;
if (c[1] == 'l') trn[++cnt] = x;
}
ans += f;
dp[0][0] = 1;
for (register int i = 1; i <= cnt; ++i)
for (register int j = 0; j < 360; ++j)
if (dp[i - 1][j] == 1)
dp[i][j] = 1, dp[i][(j + trn[i] + 360 * 10) % 360] = 1;
for (register int i = 0; i < 360; ++i)
if (dp[cnt][i] == 1) p = min(p, abs(i - 180));
ans = sqrt(f * f + b * b + 2 * b * f * cos(p * PI / 180));
printf("%.6lf", ans);
return 0;
}