c++ 滑雪問題(vector窮舉)
阿新 • • 發佈:2018-11-10
#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
#define L 5
using namespace std;
int map[L][L];//set map with global variable so it will easy to visit
class point
{
public:
int x;
int y;
point(int X, int Y) :x(X), y(Y){};
};
void printRoads(vector<vector<point>>&ways)
{
for (int i = 0; i < ways.size(); i++)
{
cout << "Road " << i << " ;";;
for (int k = 0; k < ways[i].size()-1; k++)
{
cout << map[ways[i][k].x][ways[i][k].y]<<"->";
}
cout << "end";
cout << endl;
}
}
void printArr(int p[][L])
{
for (int i = 0; i < L; i++)
{
for (int k = 0; k < L; k++)
{
cout << p[i][k] << " ";
}
cout << endl;
}
}
bool nextPoint(vector<point>way,vector<point>&p)//if we get next point we add them to p and return 1 but if not return 0,ps: the way must have one start firstly
{
point up(way.back().x - 1, way.back().y);
point right(way.back().x, way.back().y-1);
point down(way.back().x + 1, way.back().y);
point left(way.back().x, way.back().y+1);
p.push_back(up);
p.push_back(down);
p.push_back(right);
p.push_back(left);
//exclude the one included in the way or beyond the map
vector<point>::iterator it = p.begin();
while (it != p.end())
{
bool key = 0;
for (int i = 0; i < way.size(); i++)
{
if ((*it).x == way[i].x && (*it).y == way[i].y)
key = 1;
}
if ((*it).x < 0 || (*it).x >= L || (*it).y < 0 || (*it).y >= L ||map[(*it).x][(*it).y]<map[way.back().x][way.back().y]||key)
{
it = p.erase(it);
}
else
{
it++;
}
}
if (p.size() == 0)
return 0;
else
return 1;
}
int counts(vector<vector<point>>&ways)
{
int count = 0;
for (int i = 0; i < ways.size(); i++)
{
for (int k = 0; k < ways[i].size(); k++)
{
count++;
}
}
return count;
}
void findWaysInOnePoint(point start,vector<vector<point>>&ways)
{
vector<point>way;//after using it ,clear it
way.push_back(start);
ways.push_back(way);
vector<point>possP;
while (1)
{
int count = counts(ways);
int bug = ways.size();
for (int i = 0; i < bug; i++)//update every way once
{
if (nextPoint(ways[i], possP))//if the count is 1 -> keep size of ways if possP is 2||3 -> ways add to 2||3-1
{
if (possP.size() == 1)
{
point temp = possP[0];
way = ways[i];
way.push_back(temp);
ways[i] = way;
way.clear();
}
if (possP.size() == 2)
{
point temp = possP[0];
point temp1 = possP[1];
way = ways[i];
way.push_back(temp);
ways.push_back(way);
way.clear();
way = ways[i];
way.push_back(temp1);
ways[i] = way;
way.clear();
}
if (possP.size() == 3)
{
point temp = possP[0];
point temp1 = possP[1];
point temp2 = possP[2];
way = ways[i];
way.push_back(temp);
ways.push_back(way);
way.clear();
way = ways[i];
way.push_back(temp1);
ways.push_back(way);
way.clear();
way = ways[i];
way.push_back(temp2);
ways[i] = way;
way.clear();
}
}
possP.clear();
}
int fuck = counts(ways);
possP.clear();
if (count == fuck)
{
break;
}
}
}
void main()
{
// this data is used to exam the special situation , the longest is 11 in the array of 4*4
//map[0][0] = 3; map[0][1] = 9; map[0][2] = 8; map[0][3] = 7; // 3 9 8 7
//map[1][0] = 4; map[1][1] = 5; map[1][2] = 6; map[1][3] = 7; // 4 5 6 7
//map[2][0] = 4; map[2][1] = 1; map[2][2] = 0; map[2][3] = 0; // 4 1 0 0
//map[3][0] = 5; map[3][1] = 2; map[3][2] = 1; map[3][3] = 1; // 5 2 1 1
srand(time(NULL));
for (int i = 0; i < L; i++)
{
for (int k = 0; k < L; k++)
{
map[i][k] = rand() % 10;
}
}
printArr(map);
cout << endl;
vector<vector<point>>ways;
vector<vector<vector<point>>>allTheWays;
for (int i = 0; i < L; i++)
{
for (int k = 0; k < L; k++)
{
cout << "(" << i << "," << k << ") --------" << endl;
findWaysInOnePoint(point(i,k), ways);
printRoads(ways);
allTheWays.push_back(ways);
ways.clear();
cout << endl;
}
}
vector<int>length;
for (int i = 0; i < allTheWays.size(); i++)
{
for (int k = 0; k < allTheWays[i].size(); k++)
{
length.push_back(allTheWays[i][k].size());
}
}
sort(length.begin(),length.end());
cout << endl;
cout << "The longest way is " << length.back() << endl;
}
#include<vector>
#include<algorithm>
#include<ctime>
#define L 5
using namespace std;
int map[L][L];//set map with global variable so it will easy to visit
class point
{
public:
int x;
int y;
point(int X, int Y) :x(X), y(Y){};
};
void printRoads(vector<vector<point>>&ways)
{
{
cout << "Road " << i << " ;";;
for (int k = 0; k < ways[i].size()-1; k++)
{
cout << map[ways[i][k].x][ways[i][k].y]<<"->";
}
cout << "end";
cout << endl;
}
}
void printArr(int p[][L])
{
{
for (int k = 0; k < L; k++)
{
cout << p[i][k] << " ";
}
cout << endl;
}
}
bool nextPoint(vector<point>way,vector<point>&p)//if we get next point we add them to p and return 1 but if not return 0,ps: the way must have one start firstly
{
point right(way.back().x, way.back().y-1);
point down(way.back().x + 1, way.back().y);
point left(way.back().x, way.back().y+1);
p.push_back(up);
p.push_back(down);
p.push_back(right);
p.push_back(left);
//exclude the one included in the way or beyond the map
vector<point>::iterator it = p.begin();
while (it != p.end())
{
bool key = 0;
for (int i = 0; i < way.size(); i++)
{
if ((*it).x == way[i].x && (*it).y == way[i].y)
key = 1;
}
if ((*it).x < 0 || (*it).x >= L || (*it).y < 0 || (*it).y >= L ||map[(*it).x][(*it).y]<map[way.back().x][way.back().y]||key)
{
it = p.erase(it);
}
else
{
it++;
}
}
if (p.size() == 0)
return 0;
else
return 1;
}
int counts(vector<vector<point>>&ways)
{
int count = 0;
for (int i = 0; i < ways.size(); i++)
{
for (int k = 0; k < ways[i].size(); k++)
{
count++;
}
}
return count;
}
void findWaysInOnePoint(point start,vector<vector<point>>&ways)
{
vector<point>way;//after using it ,clear it
way.push_back(start);
ways.push_back(way);
vector<point>possP;
while (1)
{
int count = counts(ways);
int bug = ways.size();
for (int i = 0; i < bug; i++)//update every way once
{
if (nextPoint(ways[i], possP))//if the count is 1 -> keep size of ways if possP is 2||3 -> ways add to 2||3-1
{
if (possP.size() == 1)
{
point temp = possP[0];
way = ways[i];
way.push_back(temp);
ways[i] = way;
way.clear();
}
if (possP.size() == 2)
{
point temp = possP[0];
point temp1 = possP[1];
way = ways[i];
way.push_back(temp);
ways.push_back(way);
way.clear();
way = ways[i];
way.push_back(temp1);
ways[i] = way;
way.clear();
}
if (possP.size() == 3)
{
point temp = possP[0];
point temp1 = possP[1];
point temp2 = possP[2];
way = ways[i];
way.push_back(temp);
ways.push_back(way);
way.clear();
way = ways[i];
way.push_back(temp1);
ways.push_back(way);
way.clear();
way = ways[i];
way.push_back(temp2);
ways[i] = way;
way.clear();
}
}
possP.clear();
}
int fuck = counts(ways);
possP.clear();
if (count == fuck)
{
break;
}
}
}
void main()
{
// this data is used to exam the special situation , the longest is 11 in the array of 4*4
//map[0][0] = 3; map[0][1] = 9; map[0][2] = 8; map[0][3] = 7; // 3 9 8 7
//map[1][0] = 4; map[1][1] = 5; map[1][2] = 6; map[1][3] = 7; // 4 5 6 7
//map[2][0] = 4; map[2][1] = 1; map[2][2] = 0; map[2][3] = 0; // 4 1 0 0
//map[3][0] = 5; map[3][1] = 2; map[3][2] = 1; map[3][3] = 1; // 5 2 1 1
srand(time(NULL));
for (int i = 0; i < L; i++)
{
for (int k = 0; k < L; k++)
{
map[i][k] = rand() % 10;
}
}
printArr(map);
cout << endl;
vector<vector<point>>ways;
vector<vector<vector<point>>>allTheWays;
for (int i = 0; i < L; i++)
{
for (int k = 0; k < L; k++)
{
cout << "(" << i << "," << k << ") --------" << endl;
findWaysInOnePoint(point(i,k), ways);
printRoads(ways);
allTheWays.push_back(ways);
ways.clear();
cout << endl;
}
}
vector<int>length;
for (int i = 0; i < allTheWays.size(); i++)
{
for (int k = 0; k < allTheWays[i].size(); k++)
{
length.push_back(allTheWays[i][k].size());
}
}
sort(length.begin(),length.end());
cout << endl;
cout << "The longest way is " << length.back() << endl;
}