Programming a robot
题目链接:Gym - 101492H
自己的纯暴力做法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* */ 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <string> 6 # include <cstdlib> 7 # include <cmath> 8 # include <ctime> 9 # include <climits> 10 # include <memory> 11 # include <functional> 12 # include <algorithm> 13 # include <bitset> 14 # include <set> 15 # include <map> 16 # include <stack> 17 # include <vector> 18 # include <deque> 19 # include <queue> 20 # include <iomanip> 21 # include <utility> 22 using namespace std; 23 24 # define lson l,m,rt<<1 25 # define rson r,m+1,rt<<1|1 26 # define lowbit(x) (x&(-x)) 27 # define lcm(a,b) (a*b/__gcd(a,b)) 28 typedef long long ll; 29 const ll mod=1e9+7; 30 const int maxn=15; 31 const double pi=acos(-1.0); 32 const int eps=1e-8; 33 34 int main() 35 { 36 char c; 37 ll x0, y0, xd, yd; 38 int flag1=-1, flag2=-1; 39 scanf("%lld %lld %c", &x0, &y0, &c); 40 scanf("%lld %lld", &xd, &yd); 41 42 ll x=xd-x0; 43 ll y=yd-y0; 44 if( x==0 && y==0 )//在原位 45 { 46 printf("0\n"); 47 } 48 49 else if( x==0 && y>0 )//在上方 50 { 51 if( c=='N' ) 52 { 53 //printf("%lld\n", y); 54 printf("1\n"); 55 printf("A %lld\n", y); 56 } 57 else if( c=='E' ) 58 { 59 printf("%d\n", 3+1); 60 for(int i=0; i<3; i++ ) 61 { 62 printf("D\n"); 63 } 64 printf("A %lld\n", y); 65 } 66 67 else if( c=='S' ) 68 { 69 printf("%d\n", 2+1); 70 for(int i=0; i<2; i++ ) 71 { 72 printf("D\n"); 73 } 74 printf("A %lld\n", y); 75 } 76 77 else if( c=='O') 78 { 79 printf("%d\n", 1+1); 80 printf("D\n"); 81 printf("A %lld\n", y); 82 } 83 } 84 85 else if( x==0 && y<0 )//在下方 86 { 87 y = -y; 88 if( c=='S') 89 { 90 printf("1\n"); 91 printf("A %lld\n", y); 92 } 93 94 else if( c=='E' ) 95 { 96 printf("%d\n", 1+1); 97 printf("D\n"); 98 printf("A %lld\n", y); 99 } 100 101 else if( c=='N' ) 102 { 103 printf("%d\n", 2+1); 104 for(int i=0; i<2; i++ ) 105 { 106 printf("D\n"); 107 } 108 printf("A %lld\n", y); 109 } 110 111 else if( c=='O' ) 112 { 113 printf("%d\n", 3+1); 114 for(int i=0; i<3; i++ ) 115 { 116 printf("D\n"); 117 } 118 printf("A %lld\n", y); 119 } 120 } 121 122 else if( x>0 && y==0 )//在右边 123 { 124 if( c=='E' ) 125 { 126 printf("1\n"); 127 printf("A %lld\n", x); 128 } 129 130 else if( c=='S' ) 131 { 132 printf("%d\n", 3+1); 133 for(int i=0; i<3; i++ ) 134 { 135 printf("D\n"); 136 } 137 printf("A %lld\n", x); 138 } 139 140 else if( c=='O' ) 141 { 142 printf("%d\n", 2+1); 143 for(int i=0; i<2; i++ ) 144 { 145 printf("D\n"); 146 } 147 printf("A %lld\n", x); 148 } 149 150 else if( c=='N' ) 151 { 152 printf("%d\n", 1+1); 153 printf("D\n"); 154 printf("A %lld\n", x); 155 } 156 } 157 158 else if(x<0 && y==0 )//在左边 159 { 160 x=-x; 161 if( c=='O' ) 162 { 163 printf("1\n"); 164 printf("A %lld\n", x); 165 } 166 167 else if( c=='N' ) 168 { 169 printf("%d\n", 3+1); 170 for(int i=0; i<3; i++ ) 171 { 172 printf("D\n"); 173 } 174 printf("A %lld\n", x); 175 } 176 177 else if( c=='E' ) 178 { 179 printf("%d\n", 2+1); 180 for(int i=0; i<2; i++ ) 181 { 182 printf("D\n"); 183 } 184 printf("A %lld\n", x); 185 } 186 187 else if( c=='S' ) 188 { 189 printf("%d\n", 1+1); 190 printf("D\n"); 191 printf("A %lld\n", x); 192 } 193 } 194 195 else if( x>0 && y>0 )//在右上方 196 { 197 if( c=='N' ) 198 { 199 printf("%d\n", 1+1+1); 200 printf("A %lld\n", y); 201 printf("D\n"); 202 printf("A %lld\n", x); 203 } 204 205 else if( c=='E' ) 206 { 207 printf("%d\n", 3+1+1); 208 printf("A %lld\n", x); 209 for(int i=0; i<3; i++ ) 210 { 211 printf("D\n"); 212 } 213 printf("A %lld\n", y); 214 } 215 216 else if( c=='S' ) 217 { 218 printf("%d\n", 2+1+1+1); 219 printf("D\n"); 220 printf("D\n"); 221 printf("A %lld\n", y); 222 printf("D\n"); 223 printf("A %lld\n", x); 224 } 225 226 else if( c=='O') 227 { 228 printf("%d\n", 1+1+1+1); 229 printf("D\n"); 230 printf("A %lld\n", y); 231 printf("D\n"); 232 printf("A %lld\n", x); 233 } 234 } 235 236 else if( x>0 && y<0 )//在右下 237 { 238 y=-y; 239 if( c=='S' ) 240 { 241 printf("%d\n", 1+3+1); 242 printf("A %lld\n", y); 243 for(int i=0; i<3; i++ ) 244 { 245 printf("D\n"); 246 } 247 printf("A %lld\n", x); 248 } 249 250 if( c=='O') 251 { 252 printf("%d\n", 2+1+1+1); 253 printf("D\nD\n"); 254 printf("A %lld\n", x); 255 printf("D\n"); 256 printf("A %lld\n", y); 257 } 258 259 if( c=='N' ) 260 { 261 printf("%d\n", 1+1+1+1); 262 printf("D\n"); 263 printf("A %lld\n", x); 264 printf("D\n"); 265 printf("A %lld\n", y); 266 } 267 268 if( c=='E' ) 269 { 270 printf("%d\n", 1+1+1); 271 printf("A %lld\n", x); 272 printf("D\n"); 273 printf("A %lld\n", y); 274 } 275 } 276 277 else if( x<0 && y<0 ) 278 { 279 x = -x; 280 y = -y; 281 if( c=='S' ) 282 { 283 printf("%d\n", 1+1+1); 284 printf("A %lld\n", y); 285 printf("D\n"); 286 printf("A %lld\n", x); 287 } 288 289 if( c=='O' ) 290 { 291 printf("%d\n", 1+3+1); 292 printf("A %lld\n", x); 293 printf("D\nD\nD\n"); 294 printf("A %lld\n", y); 295 } 296 297 if( c=='N' ) 298 { 299 printf("%d\n", 2+1+1+1); 300 printf("D\nD\n"); 301 printf("A %lld\n", y); 302 printf("D\n"); 303 printf("A %lld\n", x); 304 } 305 306 if( c=='E' ) 307 { 308 printf("%d\n", 1+1+1+1); 309 printf("D\n"); 310 printf("A %lld\n", y); 311 printf("D\n"); 312 printf("A %lld\n", x); 313 } 314 } 315 316 else if( x<0 && y>0 )//在左上 317 { 318 x=-x; 319 if( c=='N' ) 320 { 321 printf("%d\n", 1+3+1); 322 printf("A %lld\n", y); 323 printf("D\nD\nD\n"); 324 printf("A %lld\n", x); 325 } 326 327 if( c=='E' ) 328 { 329 printf("%d\n", 2+1+1+1); 330 printf("D\nD\n"); 331 printf("A %lld\n", x); 332 printf("D\n"); 333 printf("A %lld\n", y); 334 } 335 336 if( c=='S' ) 337 { 338 printf("%d\n", 1+1+1+1); 339 printf("D\n"); 340 printf("A %lld\n", x); 341 printf("D\n"); 342 printf("A %lld\n", y); 343 } 344 345 if( c=='O' ) 346 { 347 printf("%d\n", 1+1+1); 348 printf("A %lld\n", x); 349 printf("D\n"); 350 printf("A %lld\n", y); 351 } 352 } 353 return 0; 354 }
后来看别人的代码才知道“模拟”
1 /* */ 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <string> 6 # include <cstdlib> 7 # include <cmath> 8 # include <ctime> 9 # include <climits> 10 # include <memory> 11 # include <functional> 12 # include <algorithm> 13 # include <bitset> 14 # include <set> 15 # include <map> 16 # include <stack> 17 # include <vector> 18 # include <deque> 19 # include <queue> 20 # include <iomanip> 21 # include <utility> 22 using namespace std; 23 24 # define lson l,m,rt<<1 25 # define rson r,m+1,rt<<1|1 26 # define lowbit(x) (x&(-x)) 27 # define lcm(a,b) (a*b/__gcd(a,b)) 28 typedef long long ll; 29 const ll mod=1e9+7; 30 const int maxn=15; 31 const double pi=acos(-1.0); 32 const int eps=1e-8; 33 34 char s[]={'N', 'E', 'S', 'O', 'N', 'E', 'S'}; 35 36 int _find(char c, int k) 37 { 38 for(int i=0; s[i]!='\0'; i++ ) 39 if( s[i]==c && i>=k ) 40 return i; 41 } 42 43 int main() 44 { 45 int a, b, x, y; 46 char m, n, c; 47 while( cin>>x>>y>>c>>a>>b ) 48 { 49 int k1=_find(c, 0), k2, k3; 50 51 if( a==x ) 52 k2=0; 53 if( b==y ) 54 k3=0; 55 56 if( a!=x && b!=y ) 57 { 58 if( a>x )//右 59 { 60 m='E'; 61 if( b>y )//上 62 n='N'; 63 else if( b<y )//下 64 n='S'; 65 } 66 else if( a<x )//左 67 { 68 m='O'; 69 if( b>y )//上 70 n='N'; 71 else if( b<y )//下 72 n='S'; 73 } 74 75 k2 = _find(n, k1)-k1; 76 k3 = _find(m, k1)-k1; 77 cout<<max(k2, k3)+2<<endl;//max(k2,k3)是求一定要转够的书,2是两次直走步骤 78 int px=fabs(x-a); 79 int py=fabs(y-b); 80 81 if( k2>k3 ) 82 { 83 for(int i=0; i<k3; i++ ) 84 { 85 cout<<"D"<<endl;//转到要转的方向才可以走 86 } 87 cout<<"A "<<px<<endl; 88 for(int i=k3; i<k2; i++ ) 89 cout<<"D"<<endl; 90 cout<<"A "<<py<<endl; 91 } 92 93 else 94 { 95 for(int i=0; i<k2; i++ ) 96 cout<<"D"<<endl; 97 cout<<"A "<<py<<endl; 98 for(int i=k2; i<k3; i++ ) 99 cout<<"D"<<endl; 100 cout<<"A "<<px<<endl; 101 } 102 } 103 104 else 105 { 106 if( x==a&&y==b ) 107 cout<<0<<endl; 108 109 else if( x==a ) 110 { 111 if( b>y )//正上方 112 n='N'; 113 else//正下方 114 n='S'; 115 116 k2=_find(n, k1)-k1; 117 cout<<k2+1<<endl; 118 for(int i=0; i<k2; i++ ) 119 cout<<"D"<<endl; 120 cout<<"A "<<fabs(b-y)<<endl; 121 } 122 else if( y==b ) 123 { 124 if( a>x ) 125 m='E'; 126 else 127 m='O'; 128 k3=_find(m,k1)-k1; 129 cout<<k3+1<<endl; 130 for(int i=0; i<k3; i++ ) 131 cout<<"D"<<endl; 132 cout<<"A "<<fabs(a-x)<<endl; 133 } 134 } 135 } 136 return 0; 137 }