TopCoder SRM 446 DIV2 500points

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:CubeWalking
Method:finalPosition
Parameters:string
Returns:string
Method signature:string finalPosition(string movement)
(be sure your method is public)

Limits

 
Time limit (s):2.000
Memory 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值