Description
Input
There will be at most 1000 test cases. Each case contains a command sequence with no more than 1000 characters.
Output
For each test case, print the case number, followed by minimal/maximal possible x (in this order), then the minimal/maximal possible y.
Sample Input
F?F
L??
LFFFRF
Sample Output
Case 1: 1 3 -1 1 Case 2: -1 1 0 2Case 3: 1 1 3 3
解题思路: 这题想到了用动态规划来写就很好写了。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 2222;
const int mod = 4;
char s[N];
int dp[N][5];
int work(int num) //x轴
{
int ans=0;
if(num==0) ans=1;
else if(num==1 || num==3) ans=0;
else ans=-1;
return ans;
}
int work1(int num) //y轴
{
int ans=0;
if(num==0||num==2) ans=0;
else if(num==1) ans=1;
else ans=-1;
return ans;
}
int main()
{
int cc=0,n,ans;
int i,j;
while(scanf("%s",s+1)!=EOF) {
n=strlen(s+1);
/
/
// x轴最小值
for(i=0;i<=n;i++)
for(j=0;j<4;j++)
dp[i][j]=inf;
dp[0][0]=0;
ans=inf;
for(i=1;i<=n;i++) { // 当前的转向
for(j=0;j<4;j++) {
if(s[i]=='F') {
dp[i][j]=min(dp[i][j],dp[i-1][j]+1*work(j));
}
else if(s[i]=='L') {
dp[i][j]=min(dp[i][j],dp[i-1][(j-1+mod)%mod]);
}
else if(s[i]=='R') {
dp[i][j]=min(dp[i][j],dp[i-1][(j+1)%mod]);
}
else {
dp[i][j]=min(dp[i][j],dp[i-1][j]+1*work(j));
dp[i][j]=min(dp[i][j],dp[i-1][(j-1+mod)%mod]);
dp[i][j]=min(dp[i][j],dp[i-1][(j+1+mod)%mod]);
}
if(i==n) ans=min(ans,dp[i][j]);
}
}
printf("Case %d: %d",++cc,ans);
///
//
//x轴的最大值!!!!!!
for(i=0;i<=n;i++)
for(j=0;j<4;j++)
dp[i][j]=-inf;
dp[0][0]=0;
ans=-inf;
for(i=1;i<=n;i++) {
for(j=0;j<4;j++) {
if(s[i]=='F') {
dp[i][j]=max(dp[i][j],dp[i-1][j]+1*work(j));
}
else if(s[i]=='L') {
dp[i][j]=max(dp[i][j],dp[i-1][(j-1+mod)%mod]);
}
else if(s[i]=='R') {
dp[i][j]=max(dp[i][j],dp[i-1][(j+1+mod)%mod]);
}
else {
dp[i][j]=max(dp[i][j],dp[i-1][j]+1*work(j));
dp[i][j]=max(dp[i][j],dp[i-1][(j-1+mod)%mod]);
dp[i][j]=max(dp[i][j],dp[i-1][(j+1+mod)%mod]);
}
if(i==n) ans=max(ans,dp[i][j]);
}
}
printf(" %d",ans);
/
/
y轴最小值
for(i=0;i<=n;i++)
for(j=0;j<4;j++)
dp[i][j]=inf;
dp[0][0]=0;
ans=inf;
for(i=1;i<=n;i++) {
for(j=0;j<4;j++) {
if(s[i]=='F') {
dp[i][j]=min(dp[i][j],dp[i-1][j]+1*work1(j));
}
else if(s[i]=='L') {
dp[i][j]=min(dp[i][j],dp[i-1][(j-1+mod)%mod]);
}
else if(s[i]=='R') {
dp[i][j]=min(dp[i][j],dp[i-1][(j+1+mod)%mod]);
}
else {
dp[i][j]=min(dp[i][j],dp[i-1][j]+1*work1(j));
dp[i][j]=min(dp[i][j],dp[i-1][(j-1+mod)%mod]);
dp[i][j]=min(dp[i][j],dp[i-1][(j+1+mod)%mod]);
}
if(i==n) ans=min(ans,dp[i][j]);
}
}
printf(" %d",ans);
///
//
//y轴最大值
for(i=0;i<=n;i++)
for(j=0;j<4;j++)
dp[i][j]=-inf;
dp[0][0]=0;
ans=-inf;
for(i=1;i<=n;i++) {
for(j=0;j<4;j++) {
if(s[i]=='F') {
dp[i][j]=max(dp[i][j],dp[i-1][j]+1*work1(j));
}
else if(s[i]=='L') {
dp[i][j]=max(dp[i][j],dp[i-1][(j-1+mod)%mod]);
}
else if(s[i]=='R') {
dp[i][j]=max(dp[i][j],dp[i-1][(j+1+mod)%mod]);
}
else {
dp[i][j]=max(dp[i][j],dp[i-1][j]+1*work1(j));
dp[i][j]=max(dp[i][j],dp[i-1][(j-1+mod)%mod]);
dp[i][j]=max(dp[i][j],dp[i-1][(j+1+mod)%mod]);
}
if(i==n) ans=max(ans,dp[i][j]);
}
}
printf(" %d\n",ans);
}
return 0;
}