直接,我试图将两个点之间的所有可能路径添加到一个二维矢量的棋子中。基本上在一个结构中制作所有路径,以便我可以通过它并确定最短路径。我似乎只是添加了第一个向量,并且正在添加空向量的每个其他迭代迭代。
注意**在我的代码中它显示了“path.erase()”,我用它来清除向量,以便下一次迭代接收一个空向量。我也使用了std :: vector.clear(),但结果相同。
我对2d向量没有太多经验,觉得我正在访问2d向量错误。希望接受任何必要的建议或更正。
#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <algorithm>
#include <iterator>
using namespace std;
class Piece {
private:
char type; //type or rank of piece (can be 'K' for "Knight" or 'O' for every other piece)
int x, y;
bool atFinish;
int movesMade;
int finalCount;
int visited[8][8]; //where '0' represents unvisited square and '1' represents visited
vector<string> path;
vector <vector<string> > possiblePaths;
public:
Piece(int xIn, int yIn, char typeIn) {
x = xIn;
y = yIn;
movesMade = 0;
finalCount = 100;
type = typeIn;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
visited[i][j] = 0;
}
}
}
bool canMove(int y, int x) {
return ((x > 0 && x < 8)
&& (y > 0 && y < 8)
&& (visited[y][x] == 0));
}
void move(int endY, int endX) {
int preX, preY;
vector<int> v;
random_device rd;
//Randomizing the possible moves for every new position the knight comes to
//so as to not continually use the same sequence of moves at every new position
for (int i = 0; i < 8; i++)
v.push_back(i);
mt19937 g(rd());
shuffle(v.begin(), v.end(), g);
/*********************************************/
if (x == endX && y == endY) {
movesMade = 0;
}
else {
for (int i = 0; i < 8; i++) {
preX = x;
preY = y;
movePiece(v[i]);
if (canMove(y, x)) {
movesMade+= 1;
visited[y][x] = 1;
string pos = to_string(y) + to_string(x);
path.push_back(pos);
move(endY, endX);
}
else {
y = preY;
x = preX;
}
x = preX;
y = preY;
}
}
}
void movePiece(int move) {
switch (move) {
case 0: //piece moves 'down' 2 and 'right' 1
x += 1;
y += 2;
break;
case 1: //piece moves 'down' 2 and 'left' 1
x -= 1;
y += 2;
break;
case 2: //piece moves 'up' 2 and 'right' 1
x += 1;
y -= 2;
break;
case 3: // piece moves 'up' 2 and 'left' 1
x -= 1;
y -=2;
break;
case 4: //piece moves 'left' 2 and 'down' 1
x -= 2;
y += 1;
break;
case 5: //piece moves 'left' 2 and 'up' 1
x -= 2;
y -= 1;
break;
case 6: //piece moves 'right' 2 and 'down' 1
x += 2;
y += 1;
break;
case 7: //piece moves 'right' 2 and 'up' 1
x += 2;
y -= 1;
break;
default:
break;
}
}
void solution(int startY, int startX, int endY, int endX) {
for (int i = 0; i < 50; i++) {
y = startY;
x = startX;
move(endY, endX);
possiblePaths.push_back(path);
path.erase(path.begin(), path.end());
}
//Error checking purposes, print size of 2d vector and all paths made
cout << possiblePaths.size() << "\n";
for (int i = 0; i < possiblePaths.size(); i++) {
vector<string> v = possiblePaths[i];
for (vector<string>::const_iterator j = v.begin(); j != v.end(); j++)
cout << *j << " ";
cout << "\n";
}
}
};
int convertLetter(char letter) {
switch(letter) {
case 'A':
return 0;
case 'B':
return 1;
case 'C':
return 2;
case 'D':
return 3;
case 'E':
return 4;
case 'F':
return 5;
case 'G':
return 6;
case 'H':
return 7;
case '1':
return 7;
case '2':
return 6;
case '3':
return 5;
case '4':
return 4;
case '5':
return 3;
case '6':
return 2;
case '7':
return 1;
case '8':
return 0;
default:
return -1;
}
}
void play(string start, string end) {
int startX = convertLetter(start[0]);
int startY = convertLetter(start[1]);
int endY = convertLetter(end[1]);
int endX = convertLetter(end[0]);
Piece p(startY, startX, 'K');
p.solution(startY, startX, endY, endX);
}
int main(int argc, char const *argv[])
{
if ( argc != 3) // argc should be 3 for correct execution
cout << "Invalid entry \n";
else {
string start = argv[1];
string end = argv[2];
play(start, end);
}
}
答案 0 :(得分:0)
我不确定我是否完全理解您的算法,但您是否打算在找到可能的路径后清除visited
?类似的东西:
for (int i = 0; i < 50; i++) {
y = startY;
x = startX;
move(endY, endX);
possiblePaths.push_back(path);
path.erase(path.begin(), path.end());
for(auto& v : visited) std::fill(std::begin(v), std::end(v), 0);
}
否则看起来后续搜索会被阻止,因为大多数位置都会被访问。