TopCoder SRM 446 DIV2 500points

来源:互联网 发布:2017年9月宏观经济数据 编辑:程序博客网 时间:2024/09/21 09:26

Problem Statement

 

Consider the 3x3x3 cube shown above. There are nine squares on each of its six faces, and each face is colored using the following pattern:

  • The four corner squares are red.
  • The center square is green.
  • The remaining four squares are blue.

There is a robot standing in the green square of the top face of the cube, facing one of the blue squares. It receives a sequence of commands. Each command is one of the following:

  • 'L': Stay in the current square and turn left 90 degrees.
  • 'R': Stay in the current square and turn right 90 degrees.
  • 'W': Walk forward in the current direction to the next square.

Note that the robot can cross an edge of the cube into another face. When that happens, the cube will rotate automatically to keep the robot on the top face.

You are given a string movement containing the sequence of commands received by the robot. The robot will execute all of the commands in order. Return the color of the robot's final landing square - "RED", "GREEN" or "BLUE" (all quotes for clarity).

Definition

 Class:CubeWalkingMethod:finalPositionParameters:stringReturns:stringMethod signature:string finalPosition(string movement)(be sure your method is public)

Limits

 Time limit (s):2.000Memory limit (MB):64

Notes

-The answer does not depend on the initial direction of the robot.

Constraints

-movement will contain between 1 and 50 characters, inclusive.-Each character in movement will be 'L', 'R' or 'W'.

Examples

0)  
"LLRR"
Returns: "GREEN"
In this example, the robot only turns left or right without ever moving to a different square.1)  
"WWWWWWWWWWWW"
Returns: "GREEN"
Walking 12 squares forward in the same direction will lead the robot back to its original position.2)  
"WLWRW"
Returns: "RED"
3)  
"WWLLWRWLWLLRWW"
Returns: "BLUE"

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     


初始站在魔方顶面中心绿色处,朝向未知(其实这无所谓),有R(原地右转),L(原地左转),W(前进一格)三种指令,求经过给定指令操作后最后到达位置的颜色


因为各个面都一样,6个面可以简化成1个面,比如站在(1,2)B处,面向右(top面),指令为W,那么走到right面的(1,0)处且面向右,这等价于到top面的(1,0)处且面向右。

四个方向我们用方向向量记录:上(1,0),右(0,1),下(-1,0),左(0,-1),如果我们的指令为R就换右边一个向量,指令为L就换左边一个向量,直接模拟即可


#include<cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cctype>#include<cmath>#include<string>#include<cstring>#include<queue>#include<vector>#define sqr(x) (x)*(x)#define INF 0x1f1f1f1f#define PI 3.1415926535#define mm using namespace std;class CubeWalking{public:string finalPosition(string movement);};string CubeWalking::finalPosition(string movement){int x=1,y=1;int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};int d=0;for (int i=0;i<movement.size();i++){switch (movement[i]){case 'W':x=(x+dir[d][0]+3)%3;y=(y+dir[d][1]+3)%3;break;case 'R':d=(d+1)%4;break;case 'L':d=(d+3)%4;break;}}char map[3][3]={'R','B','R','B','G','B','R','B','R'};//cout<<x<<' '<<y<<endl;switch (map[x][y]){case 'R':return "RED";case 'B':return "BLUE";case 'G':return "GREEN";}}


1 0
原创粉丝点击