标签:
There will be at most 1000 test cases. Each case contains a command sequence with no more than 1000 characters.
For each test case, print the case number, followed by minimal/maximal possible x (in this order), then the minimal/maximal possible y.
F?F
L??
LFFFRF
Case 1: 1 3 -1 1
Case 2: -1 1 0 2
Case 3: 1 1 3 3
这个题嘛……那时候我还不会dp
大概是这样dp[pos][dir]:在pos这个位置,朝向dir时,最多能够给四个极限贡献多少
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; bool vis[int(1e4)+10][4]; struct node { int xmin,xmax,ymin,ymax; }dp[int(1e4)+10][4]; int goal; char s[int(1e4)+10]; node dfs(int pos,int dir) { if(vis[pos][dir]) return dp[pos][dir]; vis[pos][dir]=1; dp[pos][dir].xmin=dp[pos][dir].xmax=dp[pos][dir].ymin=dp[pos][dir].ymax=0; if(pos==goal) { if(s[pos]=='?') { if(dir==0) dp[pos][dir].ymax=1; else if(dir==1) dp[pos][dir].ymin=-1; else if(dir==2) dp[pos][dir].xmin=-1; else dp[pos][dir].xmax=1; } else if(s[pos]=='F') { if(dir==0) dp[pos][dir].ymin=dp[pos][dir].ymax=1; else if(dir==1) dp[pos][dir].ymin=dp[pos][dir].ymax=-1; else if(dir==2) dp[pos][dir].xmin=dp[pos][dir].xmax=-1; else dp[pos][dir].xmin=dp[pos][dir].xmax=1; } } else { if(s[pos]=='L') { if(dir==0) dp[pos][dir]=dfs(pos+1,2); else if(dir==1) dp[pos][dir]=dfs(pos+1,3); else if(dir==2) dp[pos][dir]=dfs(pos+1,1); else dp[pos][dir]=dfs(pos+1,0); } else if(s[pos]=='R') { if(dir==0) dp[pos][dir]=dfs(pos+1,3); else if(dir==1) dp[pos][dir]=dfs(pos+1,2); else if(dir==2) dp[pos][dir]=dfs(pos+1,0); else dp[pos][dir]=dfs(pos+1,1); } else if(s[pos]=='F') { dp[pos][dir]=dfs(pos+1,dir); if(dir==0) { dp[pos][dir].ymin++; dp[pos][dir].ymax++; } else if(dir==1) { dp[pos][dir].ymin--; dp[pos][dir].ymax--; } else if(dir==2) { dp[pos][dir].xmin--; dp[pos][dir].xmax--; } else { dp[pos][dir].xmin++; dp[pos][dir].xmax++; } } else { node one,two,three; three=dfs(pos+1,dir); if(dir==0) { one=dfs(pos+1,2); two=dfs(pos+1,3); three.ymin++; three.ymax++; } else if(dir==1) { one=dfs(pos+1,3); two=dfs(pos+1,2); three.ymin--; three.ymax--; } else if(dir==2) { one=dfs(pos+1,1); two=dfs(pos+1,0); three.xmin--; three.xmax--; } else { one=dfs(pos+1,0); two=dfs(pos+1,1); three.xmin++; three.xmax++; } dp[pos][dir].xmin=min(min(one.xmin,two.xmin),three.xmin); dp[pos][dir].xmax=max(max(one.xmax,two.xmax),three.xmax); dp[pos][dir].ymin=min(min(one.ymin,two.ymin),three.ymin); dp[pos][dir].ymax=max(max(one.ymax,two.ymax),three.ymax); } } return dp[pos][dir]; } int main() { int cs=0; while(scanf("%s",s)!=EOF) { goal=strlen(s)-1; memset(vis,0,sizeof(vis)); node ans=dfs(0,3); printf("Case %d: %d %d %d %d\n",++cs,ans.xmin,ans.xmax,ans.ymin,ans.ymax); } }
标签:
原文地址:http://blog.csdn.net/stl112514/article/details/44106201