如何正确实现 2D 矢量C++

How to correctly implement 2d Vectors C++

本文关键字:矢量 C++ 2D 实现 何正确      更新时间:2023-10-16

直接来说,我正在尝试将棋子的两个点之间的所有可能路径添加到 2d 向量中。基本上将所有路径都放在一个结构中,这样我就可以通过它并确定最短的路径。我似乎只添加第一个向量,并且每隔一次迭代都会添加一个空向量。

注意**在我的代码中,它显示了"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);
}
}

我不确定我是否完全理解您的算法,但是您是否打算在找到可能的路径后清除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);
}

否则,看起来后续搜索被阻止,因为大多数职位都被访问了。