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):64Notes
-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";}}
- TopCoder SRM 446 DIV2 500points
- TopCoder SRM 609 DIV2 500points
- Topcoder srm div2 500
- TopCoder SRM 144 DIV2 550points
- topcoder SRM 500 div2 level3
- Topcoder SRM 582 DIV2 500
- topcoder SRM 548 DIV2 500
- Topcoder SRM 514div2 500point
- TopCoder SRM 633 Div2 Problem 500 - Jumping
- TopCoder SRM 634 Div2 Problem 500 - ShoppingSurvey
- Topcoder SRM 663 Div2 Problem 500
- topcoder SRM 513 DIV2
- TopCoder SRM 543 DIV2
- topcoder-srm-233-div2
- Topcoder SRM Div2 Level2
- TopCoder SRM 144 div2
- TOPCODER SRM 612 DIV2
- TOPCODER SRM 613 DIV2
- 汇编指令对应的十六进制表示
- AjaxControltoolkit学习Calendar之我见
- MFC Ribbon Category实现tab页切换视图功能
- win7下vs2008 安装opencv2.3 ,开发环境搭建
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
- TopCoder SRM 446 DIV2 500points
- jsonp解决跨域问题
- cocos2d-x播放背景音乐和战斗音效
- Date.Now 和 StopWatch
- oracle 时间精确到秒 但我只想查精确到日的
- [转]防止表单重复提交的实现方法!
- JAVA反射机制
- 推荐几个企业云协作平台
- Android多渠道批量打包测试无误版