2014 一网络公司 在线 面试题目 爬山问题:打印出从A点到B点的距离

关于那个爬山距离问题的代码说明。

1)题目要求从A(0.0)到B点。在cpp中设置B点坐标是 B(0,100)。如果是别的数值,请修改它。

  int PlaceBLocation=100;

 

2)一座山的坐标中startpoin和endpoint是否可以跟别的山可以重合成一样?

  这个问题在题目里没有明确说,而且从它给的测试用例看是不重合的,故在该程序中没有处理节点重复的情况。

 

3)测试用例输入格式,我记得原题的输入输出格式如下(我没有记得在哪里给出了B点的坐标)。

 

  //input format

    3             //表示有3座山

 1,3,1   //第一座山的坐标是起点1,终点3, 高度1。格式为

    2,4,2  //第二座山的坐标是起点2,终点4, 高度2。

   7,8,3   //第三座山的坐标是起点7,终点8, 高度3。

//output

     110     //从A点到B点,需要110步

 

我的这个代码测试用例和执行过程为:

./amazon_climb_mountain

3

1,3,1  

2,4,2  

7,8,3  

110 

 

//从A(0.0) 点到B(100.0)点,需要110步。

 


#include < cmath >
#include < cstdio >
#include < vector >
#include < map >
#include < stack >
#include < list >
#include < iostream >
#include < algorithm >
//must include it for strlen in some compilers
#include < cstring >
using namespace std;

bool isSpace(char x){
    return x == ' ' || x == '\r' || x == '\n' || x == '\r' || x == '\b' || x == '\t';
}

char * rightTrim(char *str){
      long len = strlen(str);
    while(--len>=0){
        if(isSpace(str[len])){
            str[len] = '\0';
        }else{
            break;
        }
    }
    return str;
}

char * getInputLine(char *buffer,   long length){
    if(fgets(buffer,length, stdin)==NULL){
        return NULL;
    }
    rightTrim(buffer);
    if(strlen(buffer)<=0){
        return NULL;
    }
    return buffer;
}

  long splitAndConvert(char* strings,   long *array){
    char* tokenPtr = strtok(strings,",");
      long i=0;
    while(tokenPtr != NULL){
        array[i] = atol(tokenPtr);
        i++;
        tokenPtr=strtok(NULL,",");
    }
    return i;
}
  bool GetNodeStatus(int value,vector >&nodevector){
      vector >::iterator it;
      bool rst=false;
      for(it=nodevector.begin();it!=nodevector.end();it++){
            //suppose no overlap instead.
            if(it->first==value) {
               // numbers++;
               rst=it->second;
               break;
            }          
        }
     
    return rst;
  }
 
  int PlaceBLocation=100;
  long process_data( int row,int column, vector &datavector){
    
     int data[row][column];
     vector AllnodesValue;
     
     vector > nodevector; //true means start-point
     vector >::iterator it;
    
     map Point_HeightMap; //point-height map
     map::iterator mapit; 
    
     for(int i=0;i
       for(int j=0;j
         data[i][j]=datavector[i*column+j];
         // cout<<" "<<data[i][j];
        } 
     }
    
     for(int index=0;index
      nodevector.push_back(make_pair(data[index][0],true));
      nodevector.push_back(make_pair(data[index][1],false));
     
      //maybe overlap for start point and end port
      Point_HeightMap[data[index][0]]=data[index][2];
      Point_HeightMap[data[index][1]]=data[index][2];
      //save all point value
      AllnodesValue.push_back(data[index][0]);
      AllnodesValue.push_back(data[index][1]);
     }
    
     //ascending sort all nodes
     sort(AllnodesValue.begin(),AllnodesValue.end());
     long sum=0;
     long currheight=0,lastheight=0;
     bool CurrIsStartPoint;
    
     sum += AllnodesValue[0];
     //process each node one by one
     for(int i=0;i
        CurrIsStartPoint=false;
        currheight=Point_HeightMap[AllnodesValue[i]];       
        CurrIsStartPoint=GetNodeStatus(AllnodesValue[i],nodevector);
      //distance from current point to next point 
        if(CurrIsStartPoint) {
            if(currheight>lastheight){
                 //climb
                sum += (currheight - lastheight);
                lastheight=currheight;              
            }
           //go forward to the next node
            sum += (AllnodesValue[i+1]-AllnodesValue[i]);
          }
        else { //is ending point
            if(currheight==lastheight) {
                if((i+1)
                 //there are remaining nodes
                //go down mountain
                // next point is not equal to this point
              if(AllnodesValue[i+1] != AllnodesValue[i]){
                 bool IsBeginPoint=GetNodeStatus(AllnodesValue[i+1],nodevector);
                 if(IsBeginPoint){
                   sum += currheight;//slow down completely to ground
                   lastheight=0;
                  }
                 else { //slow difference
                     sum += (currheight-Point_HeightMap[AllnodesValue[i+1]]);
                     lastheight=Point_HeightMap[AllnodesValue[i+1]];
                  }
                  //go forward to the next node
                sum += (AllnodesValue[i+1]-AllnodesValue[i]);
               }
             else { //overlap
                  //ignore
                 ;
                 }  
              }
             else {
               //this is last node in list
                sum += currheight;//slow down completely to ground
                lastheight=0;       
              }
            }
          else { if(currheight
                      //go forward to the next node
                     //this mountain is inner mounain
                       sum += (AllnodesValue[i+1]-AllnodesValue[i]);
                     }
            }            
        }      
     }
     //go ahead to place B, its distance is difference between place B and the last point.
     sum += (PlaceBLocation - AllnodesValue.back());    
     return sum; 
   }

int main(int argc, char** argv) {
char *line=new char[1000];
    getInputLine(line,1000);
    long number[1];
    long NumberLength = splitAndConvert(line,number);
 
    if(NumberLength != 1) {
        cout<<"error"<<endl;
        return 0;
    }
   
    vector datavector;
    int index=0;
    while(index
        //get triple for each mountain
         getInputLine(line, 1000);
         long data[3];
         long len=splitAndConvert(line,data);
       //valid check for array
       if(len !=3 ){
         cout<<"error"<<endl;
         return 0;  
        } 
      index++;
      for(int i=0;i
        datavector.push_back(data[i]);
     }
   
    cout<<process_data(number[0],3,datavector)<<endl;
            
    return 0;
}

 

你可能感兴趣的:(2014 一网络公司 在线 面试题目 爬山问题:打印出从A点到B点的距离)