# LeetCode - 2069. Walking Robot Simulation II ### 題目網址:https://leetcode.com/problems/walking-robot-simulation-ii/ ###### tags: `LeetCode` `Medium` `模擬` ```cpp= /* -LeetCode format- Problem: 2069. Walking Robot Simulation II Difficulty: Medium by Inversionpeter */ string direction[4] = { "East", "North", "West", "South" }; int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 }, xs[400], ys[400], directions[400] = { 3 }; class Robot { public: int nowX, nowY, newX, newY, nowDirection, nowMove, fullRound; Robot(int width, int height) { nowMove = nowX = nowY = nowDirection = 0; fullRound = (width << 1) + (height << 1) - 4; for (int i = 1; i < fullRound; ++i) { newX = nowX + dx[nowDirection]; newY = nowY + dy[nowDirection]; if (newX < 0 || newY < 0 || newX >= width || newY >= height) { nowDirection = (nowDirection + 1) & 3; newX = nowX + dx[nowDirection]; newY = nowY + dy[nowDirection]; } nowX = xs[i] = newX; nowY = ys[i] = newY; directions[i] = nowDirection; } nowX = nowY = nowDirection = 0; } void move(int num) { nowMove = (nowMove + num) % fullRound; nowDirection = directions[nowMove]; nowX = xs[nowMove]; nowY = ys[nowMove]; } vector<int> getPos() { return { nowX, nowY }; } string getDir() { return direction[nowDirection]; } }; /** * Your Robot object will be instantiated and called as such: * Robot* obj = new Robot(width, height); * obj->move(num); * vector<int> param_2 = obj->getPos(); * string param_3 = obj->getDir(); */ ```