/*
* @lc app=leetcode id=1041 lang=cpp
*
* [1041] Robot Bounded In Circle
*/
// @lc code=start
class Solution {
public:
bool isRobotBounded(string str) {
int x = 0, y = 0;
vector<int> dirx = {-1, 0, 1, 0};
vector<int> diry = {0, -1, 0, 1};
int xLoc = 0, yLoc = 0;
int N = str.length();
for(int k=0;k<4;k++){
for(int i=0;i<N;i++){
if(str[i] == 'G'){
x += dirx[xLoc];
y += diry[yLoc];
}else if(str[i] == 'L'){
xLoc = (xLoc + 1)%4;
yLoc = (yLoc + 1)%4;
}else if(str[i] == 'R'){
xLoc = (xLoc + 3)%4;
yLoc = (yLoc + 3)%4;
}
}
}
if(x == 0 && y == 0){
return true;
}
return false;
}
};
// @lc code=end
No.277 - LeetCode[1041] Robot Bounded In Circle - 模拟题
最新推荐文章于 2025-12-03 23:35:37 发布
这篇博客介绍了如何用C++实现一个解决方案来判断机器人在一个有限圆形区域内的移动路径是否会被边界限制。通过分析指令序列,计算机器人位置的变化,最后判断其是否能回到起点。
9354

被折叠的 条评论
为什么被折叠?



