如何将一个2d矢量字符串与另一个2d矢量字符串进行比较以获得最佳匹配?

时间:2017-05-06 02:54:46

标签: c++

我的游戏运行状态很少(50步),看起来像是吼叫:

初始状态:State: [(true (cell 1 1 8)), (true (cell 1 2 7)), (true (cell 1 3 6)), (true (cell 2 1 5)), (true (cell 2 2 4)), (true (cell 2 3 3)), (true (cell 3 1 2)), (true (cell 3 2 1)), (true (cell 3 3 b)), (true (step 0))] Hash=1241d9

(move 3 2) State: [(true (cell 1 1 8)), (true (cell 1 2 7)), (true (cell 1 3 6)), (true (cell 2 1 5)), (true (cell 2 2 4)), (true (cell 2 3 3)), (true (cell 3 1 2)), (true (cell 3 2 b)), (true (cell 3 3 1)), (true (step 1))] Hash=123eed

我想假设前两个步骤一起作为参考,并且游戏的其他状态需要与此参考状态进行比较。首先它应该找出最佳匹配(在2个参考状态中),这取决于与最佳匹配的参考状态相同的最高单元数。然后在比较期间,需要将所有不同状态的第一个单元与参考状态的第一个单元进行比较,依此类推。找到最佳匹配后,如果一个状态的单元格与参考状态完全相同,那么它应该打印","另一方面,如果细胞不同,它应该打印出整个细胞而不做任何改变。

我已经编写了一个只有一步作为参考的代码,并将其他状态的每个单元格与参考状态的单元格进行比较。我的代码如下。现在我坚持以多个状态作为参考,找到最佳匹配(与参考状态单元相同的最大单元数),然后与游戏的其他状态进行比较。 谢谢。  `

int main()
{

    int x=0; //reference node number
    ifstream inputFile("8puzzle_1strun.txt");   // attach input stream to file
    ofstream outputFile("output_8puzzle_1strun.txt");
    vector<string> textLines;   // vector that will store all the file lines
    string line;
    if(!inputFile)
    {
        std::cerr << "Can't open input file!\n";
    }

    int number_of_lines = 0;
    while(getline(inputFile, line))      // read the text line by line
    {
         ++number_of_lines;
        textLines.push_back(line);    // store each line as vector element
    }

    outputFile<<"Reference Node is: \n " << textLines.at(x) << "\n" <<endl;

    cout << "textlines size:"<< textLines.size()<< endl;

    vector < vector<string> > input_array;

    for (int i=0;i<textLines.size();i++){
        vector <string> input_cell;
        string part=textLines.at(i);
        string token;
        istringstream is(part);

        while (getline(is,token,'[')){
            while (getline (is, token ,',')){
                //cout <<token<<endl;
                input_cell.push_back(token);
            }
        }

        input_array.push_back(input_cell);
    }

    int size_of_array = input_array[0].size();
    cout<<"array size:"<<size_of_array<< endl;

    vector <string> reference_state_cell;
    string token1;
    for (int j=0;j<65;j++){
    for (int k=0;k<5;k++){
    string part1=textLines.at(k);
    istringstream ls(part1);

    while (getline(ls,token1,'[')){
      while (getline (ls,token1,',')){
    reference_state_cell.push_back(token1);

            }
        }
      reference_state_cell.push_back(token1);
     }
     cout << reference_state_cell[j] <<endl;
    }
    // comparing all the states with the reference state//

    for(int f=0; f<textLines.size(); f++){
        for(int j=0; j<65; j++){
            string str1 = reference_state_cell[j];
            string str2 = input_array[f][j];

            if (str1.compare(str2) == 0)    // when two strings are equal
            {
            outputFile << "," ;
            }
            else{                                    //when two strings are not equal
              outputFile<< input_array[f][j];
            }

        }
           outputFile << endl;
    }


}

`

步骤应该是:

1)首先从参考状态找出最佳匹配(假设一个状态的8个单元与参考状态8个单元相同,那么我们将该状态从两个参考状态中取出并使该单个状态作为我们的参考状态)

  1. 在仅获取一个状态(从2个参考状态)后,我们需要将该状态的每个单元格与其他状态进行比较。

  2. 在比较期间,我们刚刚打印的细胞与参考状态细胞相同&#34;,&#34;其他单元格需要打印出来,不做任何改动。

  3. 4.对于游戏的所有五十个状态,同样的过程继续进行。

0 个答案:

没有答案