?LeetCode刷題實(shí)戰(zhàn)489:掃地機(jī)器人


示例? ? ? ? ? ? ? ? ? ? ? ? ?
輸入:
room = [
??[1,1,1,1,1,0,1,1],
??[1,1,1,1,1,0,1,1],
??[1,0,1,1,1,1,1,1],
??[0,0,0,1,0,0,0,0],
??[1,1,1,1,1,1,1,1]
],
row = 1,
col = 3
解析:
房間格柵用0或1填充。0表示障礙物,1表示可以通過。
機(jī)器人從row=1,col=3的初始位置出發(fā)。在左上角的一行以下,三列以右。
解題
class?Solution?{
public:
????vector<vector<int>> dirs{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
????void?cleanRoom(Robot& robot)?{
????????unordered_set<string> visited;
????????helper(robot, 0, 0, 0, visited);
????}
????void?helper(Robot& robot, int?x, int?y, int?dir, unordered_set<string>& visited)?{
????????robot.clean();
????????visited.insert(to_string(x) + "-"?+ to_string(y));
????????for?(int?i = 0; i < 4; ++i) {
????????????int?cur = (i + dir) % 4, newX = x + dirs[cur][0], newY = y + dirs[cur][1];
????????????if?(!visited.count(to_string(newX) + "-"?+ to_string(newY)) && robot.move()) {
????????????????helper(robot, newX, newY, cur, visited);
????????????????robot.turnRight();
????????????????robot.turnRight();
????????????????robot.move();
????????????????robot.turnLeft();
????????????????robot.turnLeft();
????????????}
????????????robot.turnRight();
????????}
????}
};
LeetCode1-480題匯總,希望對(duì)你有點(diǎn)幫助!
LeetCode刷題實(shí)戰(zhàn)481:神奇字符串
LeetCode刷題實(shí)戰(zhàn)482:密鑰格式化
LeetCode刷題實(shí)戰(zhàn)483:最小好進(jìn)制
LeetCode刷題實(shí)戰(zhàn)484:尋找排列
LeetCode刷題實(shí)戰(zhàn)485:最大連續(xù) 1 的個(gè)數(shù)
LeetCode刷題實(shí)戰(zhàn)486:預(yù)測(cè)贏家
LeetCode刷題實(shí)戰(zhàn)487:最大連續(xù)1的個(gè)數(shù) II
LeetCode刷題實(shí)戰(zhàn)488:祖瑪游戲
