关于那个爬山距离问题的代码说明。
1)题目要求从A(0.0)到B(0,100)的距离,其中要爬过许多山。
用<起点,终点,高度> 来表示一座山。如果是别的数值,请修改它。
2)一座山的坐标中startpoin和endpoint是否可以跟别的山可以重合成一样?
3)测试用例输入格式,我记得原题的输入输出格式如下(我没有记得在哪里给出了B点的坐标)。
1,3,1
//output
我的这个代码测试用例和执行过程为:
./amazon_climb_mountain
3
1,3,1
2,4,2
7,8,3
110
//从A(0.0) 点到B(100.0)点,需要110步。
/*
somebody go from A (0,0) to B (0,D) via many mountains which use tri-tuple <startx,endx,height>.
* this guy must be walk up when arriving at mountains and climb down when leave this mountain.
* Here 这里山的坐标会交叉或者重叠。
questions: how many steps after he cross these mountains from starting point (0,0).
*
*|
* input:
3 //total mountain's number
mountain1: <startx1,endx1,height1>
mountain2: <startx2,endx2,height2>
mountain3: <startx3,endx3,height3>
* output:
* steps
*
* Solution:
* 1) 垂直方向的steps 就是 把各个合体山(几个起点或终点坐标有交差重叠的山)的最高高度乘以2倍,再把这些数值加起来。
* 如果合体山的起始点与前面的合体山的终点是一样的,则要注意特殊处理
* 2) 水平方向的steps 就是最远点的坐标点
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <map>
#include <stack>
#include <list>
#include <iostream>
#include <algorithm>
#include <cstring>
//must include it for strlen() in some compilers
using namespace std;
class Node {
private:
long PointValue;
long height;
bool Isstartpoint;
public:
Node(long value, long height, bool Isstartpoint){
this->PointValue=value;
this->height=height;
this->Isstartpoint=Isstartpoint;
}
virtual ~Node(){
;
}
long getPointValue() {
return PointValue;
}
long getheight() {
return height;
};
bool IsStartPoint() {
return this->Isstartpoint;
}
bool operator <( const Node& rt) const {
cout<<"call this operator <"<<endl;
return ( PointValue < rt.PointValue);
}
bool operator >( const Node& rt) const{
cout<<"call this operator >"<<endl;
return (PointValue > rt.PointValue);
}
};
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;
}
//array saves data delimited by ',' parsed from one line.
//return the total number of data
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;
}
/*
| ------ |
------------------
| |
-----------------------
*/
long process_data(vector<Node>& datavector){
//sort data point increasingly
sort(datavector.begin(),datavector.end(),less<Node>());
//sort data point decreasingly
//sort(datavector.begin(),datavector.end(),greater<Node>());
long steps=0;
long current_height=0;
long lastMountheight=0;
bool mountainconnected=false;
int accumulate=0; //用于找到合体山,即起始点和终止点个数一样。
// 垂直方向的steps 就是把各个合体山的高度的2倍加起来。
for(int len=0; len <datavector.size();len++ ){
if(datavector[len].IsStartPoint())
accumulate++;
else
accumulate--;
//find the highest height of this complicated mountain
if(datavector[len].getheight() > current_height)
current_height=datavector[len].getheight();
//find the complete complicated mountain
if(accumulate==0) {
//if this mountain is connected to former mountain
if(mountainconnected==true){
if(lastMountheight < current_height) {
steps += 2*(current_height - lastMountheight);
}
//if lastMountheight > current, nothing to do
}
else {
steps += (2*current_height);
}
lastMountheight=current_height;
current_height=0;
//check whether this mountain is connected the next mountain, that is,its endpoint is the same as the start point of next mountain.
if(len <(datavector.size()-1)) {
if(datavector[len].getPointValue()==datavector[len+1].getPointValue()){
mountainconnected=true;
}
else {
mountainconnected=false;
}
}
}
}
//水平方向的steps 就是最远点的坐标点
//datavector.front()to get this first element;
steps += datavector.back().getPointValue(); //get this furthest point value
return steps;
}
int main(int argc, char** argv) {
char *line=new char[1000];
cout<<"input number of mountains:"<<endl;
getInputLine(line,1000);
long number[1];
long NumberLength = splitAndConvert(line,number);
if(NumberLength != 1) {
cout<<"error"<<endl;
return 0;
}
cout<<"input mountains <start,end,height>"<<endl;;
//save all input int data one by one
vector<Node> datavector;
int index=0;
while(index<number[0]){
//get trip tuple<startpoint,endpoint, height> for each mountain data from stdin one line by line
getInputLine(line, 1000);
long data[3];
long len=splitAndConvert(line,data);
//valid check for array
if(len !=3 ){
cout<<" input parameter error, exit "<<endl;
return 0;
}
index++;
Node mnode1(data[0],data[2],true); //start point
datavector.push_back(mnode1);
Node mnode2(data[1],data[2],false); //end point
datavector.push_back(mnode2);
}
cout<<process_data(datavector)<<endl;
cout <<"completes successfully, exit"<<endl;
return 0;
}