海贼007

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

【题意】:机器人Herb在起点A,根据指令a[i]在网格中行走,每次走a[i]格后会顺时针转向a[i]*90度。重复执行T次指令到达B,求A、B两地的曼哈顿距离。

【算法】:
1.根据指令走一遍。
2.根据转的度数更新重复次数。
3.根据重复次数求出B点位置,算出曼哈段距离d,return d。
 
【Java代码】来自菜鸟
 1 import java.util.*;
 2 import java.util.regex.*;
 3 import java.text.*;
 4 import java.math.*;
 5 
 6 
 7 public class RobotHerbDiv2
 8 {
 9     int x,y;
10     int direction;
11     public int getdist(int T, int[] a)
12     {
13         x=y=0;
14         direction=0;
15 
16         for(int i=0;i<a.length;i++){
17             move(a[i]);
18         }
19         
20         int dx=x,dy=y;
21         if((direction%360)==180){
22             if(T%2==0){
23                 x=0;
24                 y=0;
25             }
26         }
27         else if((direction%360)==90){
28             if(T%4==2){
29                 x-=dy;
30                 y+=dx;
31             }
32             else if(T%4==0){
33                 x=0;
34                 y=0;
35             }
36         }
37         else if((direction%360)==270){
38             if(T%4==2){
39                 x+=dy;
40                 y-=dx;
41             }        
42             else if(T%4==0){
43                 x=0;
44                 y=0;
45             }
46         }        
47         else{
48             x=x*T;
49             y=y*T;
50         }
51         
52         return Math.abs(x)+Math.abs(y);
53     }
54     
55     public void move(int steps){
56         if(direction==90){
57             y+=steps;    
58         }
59         else if(direction==180){
60             x-=steps;
61         }
62         else if(direction==270){
63             y-=steps;
64         }
65         else{
66             x+=steps;
67         }
68         direction=(direction+90*steps)%360;
69     }
70     
71 
72 }
73 //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
View Code

【代码改进】:直接重复T次,反正T最大就100,a最多也就50个,性能不会损失多少,代码可读性却好多了。

 1 import java.util.*;
 2 import java.util.regex.*;
 3 import java.text.*;
 4 import java.math.*;
 5 
 6 
 7 public class RobotHerbDiv2
 8 {
 9     int x,y;
10     int direction;
11     public int getdist(int T, int[] a)
12     {
13         x=y=0;
14         direction=0;
15         while(T-->0)
16         for(int i=0;i<a.length;i++){
17             move(a[i]);
18         }
19         
20         return Math.abs(x)+Math.abs(y);
21     }
22     
23     public void move(int steps){
24         if(direction==90){
25             y+=steps;    
26         }
27         else if(direction==180){
28             x-=steps;
29         }
30         else if(direction==270){
31             y-=steps;
32         }
33         else{
34             x+=steps;
35         }
36         direction=(direction+90*steps)%360;
37     }
38 }
39 //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
View Code

【Java代码】来自大神

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class RobotHerbDiv2 {
    public int getdist(int T, int[] a) {
        int x = 0;
        int y = 0;
        int dir = 0;
        int[] dx = {0,1,0,-1};
        int[] dy = {1,0,-1,0};
        for(int t=0;t<T;t++)
        for(int i=0;i<a.length;i++) {
            x+=dx[dir]*a[i];
            y+=dy[dir]*a[i];
            dir=(dir+a[i])%4;
        }
        return Math.abs(x)+Math.abs(y);
    }
}
View Code

【C++代码】来自大神

#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <vector>
 
using namespace std;
 
#define REP(i,x,y) for(int i = x; i < int(y); i++)
 
class RobotHerbDiv2 {
  public: int getdist(int T, vector<int> a){
    int dx[]={ 1, 0, -1,  0};
    int dy[]={ 0, 1,  0, -1};
    int x=0,y=0,j=0;
    while(T--){
      REP(i, 0, a.size()){
        x+=dx[j]*a[i];
        y+=dy[j]*a[i];
        j=(j+a[i])%4;
      }
    }
    int ans=abs(x)+abs(y);
    return int(ans);
  }
};
View Code

【总结】:大神的代码出奇的一致,看来这道题是道经典题。

 

posted on 2013-07-29 19:52  wzhscript  阅读(234)  评论(0编辑  收藏  举报