C++ 查找向量中中间元素的组合
我有一个向量C++ 查找向量中中间元素的组合,c++,algorithm,vector,traveling-salesman,C++,Algorithm,Vector,Traveling Salesman,我有一个向量vectorlist\u的点包含点A B C D A。我想找到所有可能的向量组合,使外部两个元素保持在同一位置。例如 A B C D A--A B D C A--A C B D A--A C D B A--A D B C A--A D C B A.有什么建议吗 我这样做是因为每个点都有一个x,y坐标。我试图找到最短的距离去所有的元素,并返回到原始元素。我想将最小距离设置为第一个组合A B C D A,然后比较所有其他组合 //pseudocode min distance = dis
vectorlist\u的点代码>包含点A B C D A。我想找到所有可能的向量组合,使外部两个元素保持在同一位置。例如
A B C D A--A B D C A--A C B D A--A C D B A--A D B C A--A D C B A.有什么建议吗
我这样做是因为每个点都有一个x,y坐标。我试图找到最短的距离去所有的元素,并返回到原始元素。我想将最小距离设置为第一个组合A B C D A,然后比较所有其他组合
//pseudocode
min distance = distance of A B C D A
while(there are still combinations){
find another combinations of the vector.
if that distance is smaller than min distance, make it the new min distance
save the path in a vector
}
如果需要,这里是我的实现
#include <cmath>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Point
{
private:
int m_x;
int m_y;
string m_name;
public:
Point(int x, int y, string name)
: m_x(x), m_y(y), m_name(name)
{}
Point(){};
int getX() const {return m_x;}
void setX(int x) {m_x=x;}
int getY() const {return m_y;}
void setY(int y) {m_y=y;}
string getName() const {return m_name;}
float getDistance(Point &other);
string toString() const;
void printPoint() const;
// used for printing Point using << operator. For example:
// Point p(1,2,"A");
// cout << p << endl;
friend ostream& operator<<(ostream &os, const Point &p);
};
class ListOfPoints
{
private:
int elements;
public:
vector<Point>sorted_list;
vector<Point>unsorted_list;
void Unsorted_add(Point &newPt);
void Sorted_add(Point &newPt);
void Create_unsorted();
void Sort_list();
void set_elements(int n);
int get_elements();
ListOfPoints();
// adds a new point to the end of the list
void addPoint(Point &newPt);
// prints the list of points
void printList() const;
// draws the points
void draw() const;
};
string Point::toString() const{
// examples how to create string from small parts
string str(m_name);
str += " = (";
str += std::to_string(m_x);
str.append(",").append(std::to_string(m_y)).append(")");
return str;
}
float Point::getDistance(Point &other){
float x1 = float(this->getX());
cout << "this->x = "<< this->getX() <<endl;
cout << "this->y = "<<this->getY() <<endl;
float y1 = float(this->getY());
float x2 = float(other.getX());
float y2 = float(other.getY());
//cout << "x = " << x2 << endl;
//cout << "y = " << y2 << endl;
float dist = sqrt( pow(x2-x1,2) + pow(y2-y1,2) );
cout << "dist = " << dist << endl;
return dist;
}
void Point::printPoint() const{
cout << toString() << endl;
}
// used for printing Point using << operator.
// For example, the following code will work
// Point origin(0,0,'O');
// cout << origin;
ostream& operator<<(ostream &os, const Point &p) {
return os << p.toString();
}
ListOfPoints::ListOfPoints() {
}
void ListOfPoints::Sorted_add(Point &newPt) {
sorted_list.push_back(newPt);
}
void ListOfPoints::Unsorted_add(Point &newPt) {
unsorted_list.push_back(newPt);
}
void ListOfPoints::set_elements(int n){
elements = n;
}
int ListOfPoints::get_elements(){
return this->elements;
}
void ListOfPoints::Create_unsorted(){
int x;
int y;
string name;
cout << "Enter the Number of elements" << endl;
cin >> elements;
for(int i = 0; i<elements; i++){
cout << "Enter the name of the element: " << endl;
cin >> name;
cout <<"Enter the x value" << endl;
cin >> x;
cout <<"Enter they y vlaue" << endl;
cin >> y;
Point p(x,y,name);
unsorted_list.push_back(p);
}
cout << elements << endl;
return;
}
#包括
#包括
#包括
#包括
使用名称空间std;
类点
{
私人:
int m_x;
国际货币基金组织;
字符串m_name;
公众:
点(整数x,整数y,字符串名称)
:m_x(x),m_y(y),m_name(name)
{}
点(){};
int getX()常量{return m_x;}
void setX(int x){m_x=x;}
int getY()常量{return m_y;}
void setY(int y){m_y=y;}
字符串getName()常量{return m_name;}
浮动距离(点和其他);
字符串toString()常量;
void printPoint()常量;
//用于打印点使用根据您的描述,以下程序演示了您试图使用以下工具实现的目标:
要将其扩展到点
类,您需要以某种方式对中间点进行排序,然后使用std::next\u permutation
应用相同的逻辑
下面是一个小例子:
#include <algorithm>
#include <vector>
#include <iostream>
struct Point
{
int m_x;
int m_y;
int get_x() const { return m_x; }
int get_y() const { return m_y; }
Point(int x = 0, int y = 0) : m_x(x), m_y(y) {}
};
int main()
{
std::vector<Point> test = {{0,0},{1,2},{2,1},{3,6},{2,7},{10,10}};
std::vector<std::vector<Point>> results;
auto sorter = [](const Point& p1, const Point& p2) { return std::tie(p1.m_x, p1.m_y) < std::tie(p2.m_x, p2.m_y); };
// sort the items after the beginning and before the end of the sequence
std::sort(test.begin() + 1, std::prev(test.end()), sorter);
do
{
results.push_back(test);
} while (std::next_permutation(test.begin() + 1, std::prev(test.end()), sorter));
for (auto& r : results)
{
for (auto& p : r)
std::cout << "{" << p.get_x() << "," << p.get_y() << ") ";
std::cout << "\n";
}
}
我选择了基于x和y值的排序标准,首先比较x值将有nPn个结果:其中n是向量-2的大小,nPn表示置换。您可能必须找到一种方法来限制它,否则会有大量结果标准库具有查找置换的功能tations调用。您只需将迭代器指定给第二个和最后一个元素作为要排列的范围,然后手动添加第一个和最后一个值。请仔细查看这一点,以寻找更好的算法,因为暴力强制可能不适用于更长的向量,暴力强制的复杂性为O(n!)我有点搞不懂排序是什么意思。我怎么知道按什么标准排序。std::next_Permuation不需要某种标准吗?我试过了,得到了一长串错误。先按x再按y排序。请参见编辑。
ABCDA
ABDCA
ACBDA
ACDBA
ADBCA
ADCBA
#include <algorithm>
#include <vector>
#include <iostream>
struct Point
{
int m_x;
int m_y;
int get_x() const { return m_x; }
int get_y() const { return m_y; }
Point(int x = 0, int y = 0) : m_x(x), m_y(y) {}
};
int main()
{
std::vector<Point> test = {{0,0},{1,2},{2,1},{3,6},{2,7},{10,10}};
std::vector<std::vector<Point>> results;
auto sorter = [](const Point& p1, const Point& p2) { return std::tie(p1.m_x, p1.m_y) < std::tie(p2.m_x, p2.m_y); };
// sort the items after the beginning and before the end of the sequence
std::sort(test.begin() + 1, std::prev(test.end()), sorter);
do
{
results.push_back(test);
} while (std::next_permutation(test.begin() + 1, std::prev(test.end()), sorter));
for (auto& r : results)
{
for (auto& p : r)
std::cout << "{" << p.get_x() << "," << p.get_y() << ") ";
std::cout << "\n";
}
}
{0,0) {1,2) {2,1) {2,7) {3,6) {10,10)
{0,0) {1,2) {2,1) {3,6) {2,7) {10,10)
{0,0) {1,2) {2,7) {2,1) {3,6) {10,10)
{0,0) {1,2) {2,7) {3,6) {2,1) {10,10)
{0,0) {1,2) {3,6) {2,1) {2,7) {10,10)
{0,0) {1,2) {3,6) {2,7) {2,1) {10,10)
{0,0) {2,1) {1,2) {2,7) {3,6) {10,10)
{0,0) {2,1) {1,2) {3,6) {2,7) {10,10)
{0,0) {2,1) {2,7) {1,2) {3,6) {10,10)
{0,0) {2,1) {2,7) {3,6) {1,2) {10,10)
{0,0) {2,1) {3,6) {1,2) {2,7) {10,10)
{0,0) {2,1) {3,6) {2,7) {1,2) {10,10)
{0,0) {2,7) {1,2) {2,1) {3,6) {10,10)
{0,0) {2,7) {1,2) {3,6) {2,1) {10,10)
{0,0) {2,7) {2,1) {1,2) {3,6) {10,10)
{0,0) {2,7) {2,1) {3,6) {1,2) {10,10)
{0,0) {2,7) {3,6) {1,2) {2,1) {10,10)
{0,0) {2,7) {3,6) {2,1) {1,2) {10,10)
{0,0) {3,6) {1,2) {2,1) {2,7) {10,10)
{0,0) {3,6) {1,2) {2,7) {2,1) {10,10)
{0,0) {3,6) {2,1) {1,2) {2,7) {10,10)
{0,0) {3,6) {2,1) {2,7) {1,2) {10,10)
{0,0) {3,6) {2,7) {1,2) {2,1) {10,10)
{0,0) {3,6) {2,7) {2,1) {1,2) {10,10)