classSolution{
public:int robotSim(vector<int>& commands, vector<vector<int>>& obstacles){int n = commands.size();int grad =0;//方向向北
int x=0, y=0;//初始化点坐标
int dis_max=0;//最远点距离
for(int i=0; i<n;++i){if(commands[i]==-2)
grad +=3;//向左转90度,即向右转270度,加3。
elseif(commands[i]==-1)
grad +=1;//向右转90度,加1else//否则,就是正常移动
{
grad = grad %4;if(grad==0)//向北移动commands[i]
y += commands[i];elseif(grad==1)//向东移动commands[i]
x += commands[i];elseif(grad==2)//向南移动commands[i]
y -= commands[i];else//向西移动commands[i]
x -= commands[i];int dis =pow(x,2)+pow(y,2);
dis_max =max(dis_max, dis);}}return dis_max;}};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
步骤2:当有障碍物时,模拟机器人行走
C++解法:
classSolution{public:introbotSim(vector<int>& commands, vector<vector<int>>& obstacles){int n = commands.size();int m = obstacles.size();int grad =0;//方向向北int x=0, y=0;//初始化点坐标int dis_max=0;//最远点距离
set<pair<int,int>> obstacleSet;for(int i=0; i<m; i++)
obstacleSet.insert(make_pair(obstacles[i][0], obstacles[i][1]));for(int i=0; i<n;++i){if(commands[i]==-2)
grad +=3;//向左转90度,即向右转270度,加3。elseif(commands[i]==-1)
grad +=1;//向右转90度,加1else//否则,就是正常移动{
grad = grad %4;if(grad==0)//向北移动commands[i]{for(int j=0; j<commands[i];++j){
y +=1;if(obstacleSet.find(make_pair(x, y))!= obstacleSet.end())//(x,y)是障碍点{
y -=1;break;}}}elseif(grad==1)//向东移动commands[i]{for(int j=0; j<commands[i];++j){
x +=1;if(obstacleSet.find(make_pair(x, y))!= obstacleSet.end()){
x -=1;break;}}}elseif(grad==2)//向南移动commands[i]{for(int j=0; j<commands[i];++j){
y -=1;if(obstacleSet.find(make_pair(x, y))!= obstacleSet.end()){
y +=1;break;}}}else//向西移动commands[i] {for(int j=0; j<commands[i];++j){
x -=1;if(obstacleSet.find(make_pair(x, y))!= obstacleSet.end()){
x +=1;break;}}}int dis =pow(x,2)+pow(y,2);
dis_max =max(dis_max, dis);}}return dis_max;}};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
提交结果:
步骤3:进一步优化
C++解法:
classSolution{public:introbotSim(vector<int>& commands, vector<vector<int>>& obstacles){int n = commands.size();int m = obstacles.size();int grad =0;//方向向北int x=0, y=0;//初始化点坐标int dis_max=0;//最远点距离int grad_x[4]={0,1,0,-1};int grad_y[4]={1,0,-1,0};
set<pair<int,int>> obstacleSet;for(int i=0; i<m; i++)
obstacleSet.insert(make_pair(obstacles[i][0], obstacles[i][1]));for(int i=0; i<n;++i){if(commands[i]==-2)
grad +=3;//向左转90度,即向右转270度,加3。elseif(commands[i]==-1)
grad +=1;//向右转90度,加1else//否则,就是正常移动{
grad = grad %4;for(int j=0; j<commands[i];++j){
x += grad_x[grad];
y += grad_y[grad];if(obstacleSet.find(make_pair(x, y))!= obstacleSet.end()){
x -= grad_x[grad];
y -= grad_y[grad];break;}}int dis =pow(x,2)+pow(y,2);
dis_max =max(dis_max, dis);}}return dis_max;}};