前言

以前一份代码的阅读,2019华为软挑


一、题目是什么?

交通规划,给出路,路口,车辆三个txt文件,要求读取数据,并规划车辆行驶出发时间和具体路径,车辆行驶有具体交通规则要求。使所有车辆最快到达目的地 并 程序最快规划出结果为优。赛期:总共1个半月时间;

二、设计步骤

1.分工

本人队长:
1、时间规划:第一周读需求文档;第2周搭好输入输出;第三周写算法;第四周调试并测试性能(预期是完美的,现实…)
2、任务规划:(code就完事了,太久了 不记得了)

2.读入数据

思路:
1、每个车,每条路,每个路口,都设置成一个对象;
2、为车,路,路口设置一个数据集基类,搭建{ID<—>对象}索引。
3、复赛中,Car中增加了优先车以及预置车,所以car中有一个预设路径。
代码如下(示例):

//车(配置文件数据)
class RawCar
{
public:
  bool operator<(const RawCar& obj) const { return m_id < obj.m_id; }
  void init();
  void printInfo();

public:
  int m_id;
  int m_source_cross_id;
  int m_target_cross_id;
  int m_max_speed;
  int m_plan_time;
  int m_priority;
  int m_preset;

  int         m_preset_start_time;
  vector<int> m_preset_road_id_list;

public:
  RawCross* m_pSourceCross;
  RawCross* m_pTargetCross;

  vector<RawRoad*> m_pPresetRoadList;
};
//所有车(配置文件数据)
class RawCarSet : public BaseSet<RawCar>
{
public:
  void readFile(const string& path, const string& path2);
  void init();
  void printInfo();
};

extern RawCarSet gRawCars;
#endif  //__RAW_CAR_H__
//集合基类
template <class T>
class BaseSet
{
public:
  ~BaseSet() {}

public:
  void resetMapping(size_t max_id)
  {
    mapping_data.resize(max_id + 1);
    for (size_t i = 0; i < mapping_data.size(); i++)
      mapping_data[i] = NULL;
  }

  T* getByID(int id)
  {
    if (id > int(mapping_data.size()) - 1 || id < 0)
      return NULL;
    return mapping_data[id];
  }

  size_t size() { return data.size(); }
  T*     get(size_t i) { return &data[i]; }

public:
  timer      t;
  vector<T>  data;
  vector<T*> mapping_data;
};
//整理用,号分开的数据
std::vector<int> split(const std::string& s, const std::string& delim)
{
  std::vector<int> elems;
  size_t           pos       = 0;
  size_t           len       = s.length();
  size_t           delim_len = delim.length();
  if (delim_len == 0)
    return elems;
  while (pos < len)
  {
    int find_pos = s.find(delim, pos);
    if (find_pos < 0)
    {
      elems.push_back(atoi(s.substr(pos, len - pos).c_str()));
      break;
    }
    elems.push_back(atoi(s.substr(pos, find_pos - pos).c_str()));
    pos = find_pos + delim_len;
  }
  return elems;
}


void RawCarSet::readFile(const string& path, const string& path2)
{
  t.reset();
  int coutt = 0;
  //读取car.txt
  ifstream file(path);
  int      max_id = -INT_MAX;
  string   line_str;
  getline(file, line_str);
  while (getline(file, line_str))
  {
    stringstream ss(line_str);
    char         nul;
    RawCar       car;
    ss >> nul >> car.m_id >> nul >> car.m_source_cross_id >> nul >> car.m_target_cross_id >> nul >> car.m_max_speed >> nul >> car.m_plan_time >> nul >> car.m_priority >> nul >> car.m_preset;
    data.push_back(car);
    if (car.m_id > max_id)
      max_id = car.m_id;
  }
  file.close();

  sort(data.begin(), data.end());

  //构建ID查找映射表
  resetMapping(max_id);
  for (size_t i = 0; i < data.size(); i++)
    mapping_data[data[i].m_id] = &data[i];

  //读取presetAnswer.txt
  int              n_preset = 0;
  std::vector<int> splited_str;
  file.open(path2);
  getline(file, line_str);
  while (getline(file, line_str))
  {
    n_preset++;
    line_str    = line_str.substr(1, line_str.length() - 2);
    splited_str = split(line_str, ",");
    RawCar* car              = getByID(splited_str[0]);
    car->m_preset_start_time = splited_str[1];
    for (size_t i = 2; i < splited_str.size() - 1; i++)
      car->m_preset_road_id_list.push_back(splited_str[i]);
    splited_str.clear();
  }

  cout << "read car.txt&presetAnswer.txt, preset:" << n_preset;
  cout << ", controlable:" << data.size() - n_preset;
  cout << ", total:" << data.size();
  t.show(",");
}

总结

简单看一了一下以前数据读入和保存的方法,这里只记录车的数据吧,其他的都一样。因为追求算法解答结果的速度,所有数据都存在内存。(有要求不让用其他库)

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐