华为OD机试算法:转骰子
题目描述
骰子是一个立方体,每个面一个数字,初始为左1,右2,前3(观察者方向),后4,上5,下6,用123456表示这个状态,放置在平面上,
可以向左翻转(用L表示向左翻转1次),
可以向右翻转(用R表示向右翻转1次),
可以向前翻转(用F表示向前翻转1次),
可以向后翻转(用B表示向后翻转1次),
可以逆时针旋转(用A表示逆时针旋转90度),
可以顺时针旋转(用C表示顺时针旋转90度),
现从123456这个初始状态开始,根据输入的动作序列,计算得到最终的状态。
输入描述
输入一行,为只包含LRFBAC的字母序列,最大长度为50,字母可重复。
输出描述
输出最终状态
用例
题目解析
创建一个字典,用于存储每个面的数字与对应的操作。例如,左面为1,右面为2,前面为3,后面为4,上面为5,下面为6。
根据输入的操作序列,依次执行相应的翻转或旋转操作,并更新字典中的数字与对应的面。
最后输出最终状态,即字典中各个面对应的数字。
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); rl.on("line", (line) => { const directives = line.split(" "); turnDice(directives); }); function turnDice(directives) { const dice = new Dice(); directives.forEach((directive) => { dice[`turn${directive}`](); }); dice.print(); } class Dice { constructor() { this.faces = [1, 2, 3, 4, 5, 6]; } turnL() { [this.faces[0], this.faces[2], this.faces[5], this.faces[3]] = [this.faces[3], this.faces[0], this.faces[2], this.faces[5]]; } turnR() { [this.faces[0], this.faces[2], this.faces[5], this.faces[3]] = [this.faces[2], this.faces[5], this.faces[3], this.faces[0]]; } turnF() { [this.faces[0], this.faces[1], this.faces[5], this.faces[4]] = [this.faces[1], this.faces[5], this.faces[4], this.faces[0]]; } turnB() { [this.faces[0], this.faces[1], this.faces[5], this.faces[4]] = [this.faces[4], this.faces[0], this.faces[1], this.faces[5]]; } turnA() { [this.faces[0], this.faces[1], this.faces[5], this.faces[4]] = [this.faces[1], this.faces[5], this.faces[4], this.faces[0]]; } turnC() { [this.faces[0], this.faces[2], this.faces[5], this.faces[3]] = [this.faces[3], this.faces[0], this.faces[2], this.faces[5]]; } print() { console.log(this.faces.join('')); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] directives = sc.nextLine().split(" "); turnDice(directives); } public static void turnDice(String[] directives) { Dice dice = new Dice(); for (String directive : directives) { switch (directive) { case "L": dice.turnLeft(); break; case "R": dice.turnRight(); break; case "F": dice.turnFront(); break; case "B": dice.turnBack(); break; case "A": dice.turnAround(); break; case "C": dice.turnCounterclockwise(); break; } } dice.print(); } } class Dice { int left = 1; int right = 2; int front = 3; int back = 4; int top = 5; int bottom = 6; public void turnLeft() { int tmp = this.right; this.right = this.bottom; this.bottom = this.left; this.left = this.top; this.top = tmp; } public void turnRight() { int tmp = this.left; this.left = this.bottom; this.bottom = this.right; this.right = this.top; this.top = tmp; } public void turnFront() { int tmp = this.front; this.front = this.top; this.top = this.back; this.back = this.bottom; this.bottom = tmp; } public void turnBack() { int tmp = this.top; this.top = this.front; this.front = this.bottom; this.bottom = this.back; this.back = tmp; } public void turnAround() { int tmp = this.right; this.right = this.front; this.front = this.left; this.left = this.back; this.back = tmp; } public void turnCounterclockwise() { int tmp = this.front; this.front = this.right; this.right = this.back; this.back = this.left; this.left = tmp; } public void print() { StringBuilder sb = new StringBuilder(); sb.append(this.left).append(this.right).append(this.front).append(this.back).append(this.top).append(this.bottom); System.out.println(sb.toString()); } }
class Dice: def __init__(self): self.faces = [1, 2, 3, 4, 5, 6] def turn(self, direction): if direction == "L": self.faces[0], self.faces[2], self.faces[5], self.faces[3] = self.faces[2], self.faces[5], self.faces[3], self.faces[0] elif direction == "R": self.faces[0], self.faces[3], self.faces[5], self.faces[2] = self.faces[3], self.faces[5], self.faces[2], self.faces[0] elif direction == "F": self.faces[0], self.faces[1], self.faces[5], self.faces[4] = self.faces[1], self.faces[5], self.faces[4], self.faces[0] elif direction == "B": self.faces[0], self.faces[4], self.faces[5], self.faces[1] = self.faces[4], self.faces[5], self.faces[1], self.faces[0] elif direction == "A": self.faces[0], self.faces[1], self.faces[5], self.faces[4] = self.faces[1], self.faces[5], self.faces[4], self.faces[0] elif direction == "C": self.faces[0], self.faces[4], self.faces[5], self.faces[1] = self.faces[4], self.faces[5], self.faces[1], self.faces[0] def __str__(self): return ''.join(map(str, self.faces)) def turnDice(directives): dice = Dice() for directive in directives: dice.turn(directive) return str(dice) directives = input().split() print(turnDice(directives))