Leetcode-5055 Robot Bounded In Circle(困于环中的机器人)

 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2 int dx[] = {-1,0,1,0};
 3 int dy[] = {0,1,0,-1};
 4 class Solution
 5 {
 6     public:
 7         bool isRobotBounded(string instructions)
 8         {
 9             int fang = 0;
10             int x = 0,y = 0;
11             int T = 200;
12             while(T--)
13             {
14             _for(i,0,instructions.size())
15             {
16                 if(instructions[i]=='G')
17                 {
18                     x += dx[fang];
19                     y += dy[fang];
20                 }
21                 else if(instructions[i]=='L')
22                 {
23                     fang --;
24                     if(fang==-1)
25                         fang = 3;
26                 }
27                 else
28                 {
29                     fang ++;
30                     if(fang==4)
31                         fang = 0;
32                 }
33             }
34                 if(x==0 && y==0)
35                     break;
36             }
37             return x==0 && y==0;
38         }
39 };

数据范围小于100,那多循环几次看看是否形成闭合回路就行了

上一篇:Codeforces Round #630 (Div. 2) 补题


下一篇:Windows API GDI(3) InflateRect函数