Topic Links:
Cid=2095&pid=7 ">csu1510
Problem Solving Ideas:
To solve four values X_min,x_max,y_min,y_max
First consider how x_min get: Since the robot finally has the ability to face four directions (e,w,n,s), we can use an array a[4] to save the robot in 4 directions when the value of X
A[0]: x+,a[1]:x-. a[2]:y+,a[3],y-;
Only ask for the a[4] at the end of the minimum value, that is, x_min
The key to seeking a[4] is how to get the optimal solution when running the command:
A[x]=min (turn left, turn right, advance);
namely A[0]=min (a[2],a[3],a[0]+1);
Similarly get a[1],a[2],a[3]
Code:
#include <iostream> #include <cstring> #include <cstdio> #define INF 9999using namespace Std;char str[ 1050];int Min (int a,int b,int c) {if (a>b) a=b; if (a>c) a=c; return A;} int Max (int a,int b,int c) {if (a<b) a=b; if (a<c) a=c; return A;} void Change (int* t,int dir) {int tt=t[0]; if (dir) {t[0]=t[3]; T[3]=T[1]; T[1]=T[2]; T[2]=tt; } else {t[0]=t[2]; T[2]=T[1]; T[1]=T[3]; T[3]=tt; }}int Main () {int len; int t[4]; int case=1; while (~SCANF ("%s", str)) {int a[4]= {0,-inf,-inf,-inf}; int b[4]= {0,inf,inf,inf}; int c[4]= {0,-inf,-inf,-inf}; int d[4]= {0,inf,inf,inf}; Len=strlen (str); for (int i=0; i<len; i++) {if (str[i]== ' F ') {a[0]++,a[1]--; b[0]++,b[1]--; c[2]++,c[3]--; d[2]++,d[3]--; } else if (str[i]== ' L ') {change (a,1); Change (b,1); Change (c,1); Change (d,1); } else if (str[i]== ' R ') {change (a,0); Change (b,0); Change (c,0); Change (d,0); } else {T[0]=max (a[0]+1,a[2],a[3]); T[1]=max (A[1]-1,a[2],a[3]); T[2]=max (a[0],a[1],a[2]); T[3]=max (A[0],a[1],a[3]); memcpy (a,t,16); T[0]=min (B[0]+1,b[2],b[3]); T[1]=min (B[1]-1,b[2],b[3]); T[2]=min (b[0],b[1],b[2]); T[3]=min (B[0],b[1],b[3]); memcpy (b,t,16); T[0]=max (C[0],c[2],c[3]); T[1]=max (C[1],c[2],c[3]); T[2]=max (c[0],c[1],c[2]+1); T[3]=max (c[0],c[1],c[3]-1); memcpy (c,t,16); T[0]=min (d[0],d[2],d[3]); T[1]=min (D[1],d[2],d[3]); T[2]=min (d[0],d[1],d[2]+1); T[3]=min (d[0],d[1],d[3]-1); memcpy (d,t,16); }} T[0]=max (Max (a[0],a[1],a[2]), a[3]); T[1]=min (min (b[0],b[1],b[2]), b[3]); T[2]=max (Max (c[0],c[1],c[2]), c[3]); T[3]=min (min (d[0],d[1],d[2]), d[3]); printf ("Case%d:%d%d%d%d\n", case++,t[1],t[0],t[3],t[2]); } return 0;}
csu1510 Happy Robot Recursion