void Grid::input_ways(){
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
if(grid_nodes[i][j].pass){
for(int l = 0; l < height; ++l){
for(int k = 0; k < width; ++k){
if(grid_nodes[l][k].pass){
if(i == l && j == k){++k;}
fstream way;
way.open(path_to_ways_folder + "/way" + to_string(i * width + j) + "to" + to_string(l * width + k) + ".txt", fstream::in | fstream::out);
cout << path_to_ways_folder + "/way" + to_string(i * width + j) + "to" + to_string(l * width + k) + ".txt" << endl;
if(way.peek() == EOF){
astar(i * width + j, l * width + k);
cout << "Запись пути от " << (i * width + j) << " к "<< (l * width + k) << endl;
for(int p = this-> way.size() - 1; p >= 0; --p){
way << this-> way[p].x;
way << ' ';
way << this-> way[p].y;
}
}
ways.resize(ways.size() + 1);
while(!way.eof()){
way >> ways.back().x;
way >> ways.back().y;
}
way.close();
}
}
}
}
}
}
}
#include <vector>
#include <iostream>
#include <iterator>
#include <fstream>
struct Node
{
int x;
int y;
Node() : x(0), y(0) {}
Node(int x, int y) : x(x), y(y) {}
friend std::ostream& operator<<(std::ostream& os, const Node& node)
{
os << node.x << " " << node.y << " ";
return os;
}
friend std::istream& operator>>(std::istream& is, Node& node)
{
is >> node.x >> node.y;
return is;
}
};
class Way
{
private:
std::vector<Node> path;
public:
Way(){}
Way(const std::vector<Node>& path) : path(path) {}
Way(const Way& otherWay) : path(otherWay.path) {}
// в файл пишем количество элементов в path и сам path
friend std::fstream& operator<<(std::fstream& os, const Way& way)
{
os << way.path.size() << " ";
std::copy(way.path.begin(), way.path.end(), std::ostream_iterator<Node>(os, " "));
os << std::endl;
return os;
}
friend std::fstream& operator>>(std::fstream& is, Way& way)
{
size_t size = 0;
is >> size;
way.path.resize(size);
std::copy_n(std::istream_iterator<Node>(is), size, way.path.begin());
return is;
}
// на экран выводим только содержимое path
friend std::ostream& operator<<(std::ostream& os, const Way& way)
{
std::copy(way.path.begin(), way.path.end(), std::ostream_iterator<Node>(os, " "));
os << std::endl;
return os;
}
};
int main()
{
std::vector<Node> path0{ Node(3,2), Node(2,4), Node(4,5) };
std::vector<Node> path1{ Node(1,4), Node(4,3), Node(3,2) };
Way way0(path0);
Way way1(path1);
std::fstream ways;
ways.open("1.txt", std::ios_base::out | std::ios_base::trunc);
ways << way0 << way1;
ways.close();
Way way0r;
Way way1r;
ways.open("1.txt", std::ios_base::in);
ways >> way0r >> way1r;
ways.close();
std::cout << way0r << way1r;
return 0;
}