0

如何使此路径查找 C++ 代码变为动态,以便代理可以自行搜索路径(从网格的一个角到另一个角),但除非移动到网格,否则代理无法看到。我是 C++ 新手,并尝试制作这个小程序,但它似乎是静态的,我需要帮助,以便我可以将其更改为动态,或者代理可以自己进行路径搜索,而不是通过分配路径,到目前为止我制作了 5 条不同的路径,因为代理可以选择任意多的路径,但路径长度应为 6,并且只能向上或向右移动。任何帮助将不胜感激。提前致谢

 #include <iostream> 
#include <ctime> 
#include <cstdlib>
#include <iomanip>
#include <math.h>
#include <ctime>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

using namespace std;
using namespace System;
using namespace System::IO;

/*int Grid[ 4 ][ 4 ] = 
{
    { 29, 8, 11, 9 },
    { 22, 24, 3, 15 },
    { 24, 26, 5, 6 },
    { 33, 10, 32, 21}
};
int Grid[ 4 ][ 4 ] = 
    {
    { 0, 0, -5, 0.0 },
    { -1, 0, 2, 2 },
    { 2, 0, 0, 0 },
    { 0, 2, 0, -1}
    };
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
    */
int g[16] = {33,10,32,21,24,26,5,6,22,24,3,15,29,8,11,9};
int r[16] = {0,2,0,-1,2,0,0,0,-1,0,2,2,0,0,-5,0};


#define SPACE "[   ]" 

bool Finished( false );
int m[4];
//int E = 6;
int E, E1, E2, E3;


void printmatrix(char arg[], int length)
{

    for (int j = length; j >= 0; j=-4)
    {
    cout <<"\n" << arg[j] << "\n ";

    }   
    //}
}
void FindNextPos()
{       std::cout<<"\n"<<"Search Path 1"<<"\n";
    for (int c= 1; c < 5; c++)
        {   if(r[c] != r[3])
                {   
                    m[c] = 1;
                    E = E + r[c] - 1;


//std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E <<")"<<m[c]<< std::endl;

                    //system("pause");
                }
    else 
                {
                Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E <<")"<<m[c]<< std::endl;
                }
    }

        for (int c= 7; c <= 16; c= c+ 4)
                {   
                    if(r[c] != r[16]){
                    m[c] = 2;
                    E = E + r[c] - 1;
    //std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E <<")"<< std::endl;

                    //system("pause");
                }
    else 
                {
                //m[c] = 2;
                Finished = true;
    std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E <<")"<< std::endl;
                }
}
        }

//int E1 = 6;
void nextpos1(){ 

    std::cout<<"\n"<<"Search Path 2"<<"\n";
for (int c= 0; c < 3; c++)
        {   if(r[c] != r[3])
                {   
                    m[c] = 3;
                    E1 = E1 + r[c] - 1;
    std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
                    //system("pause");
                }
    else 
                {
                Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
                }
    }

        for (int c= 6; c < 15; c= c + 4)
                {   
                    if(r[c] != r[16]){
                    m[c] = 4;
                    E1 = E1 + r[c] - 1;

std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
                    //system("pause");
                }
    else 
                {
                        m[c] = 4;
                Finished = true;
std::cout<<"(ID,C,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 + r[c] - 1 <<")"<< std::endl;
                }
        }
        for (int c= 15; c < 16; ++c)
                {   
                    if(r[c] != 0){
                    m[c] = 5;
                    E1 = E1 + r[c] - 1;

std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
                    //system("pause");
                }
    else 
                {
                        m[c] = 5;
                Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
std::cout << "\n"<<"finish-->" << r[c] << "\t" <<"E1-->"<< E1 <<"\n" ;
                }
        }
}
    //int E2 = 6;
void nextpos2(){ 
        std::cout<<"\n"<<"Search Path 3"<<"\n";
for (int c= 0; c < 2; c++)
        {   if(r[c] != r[3])
                {   
                    m[c] = 6;
                    E2 = E2 + r[c] - 1;

std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
                    //system("pause");
                }
    else 
                {
                Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
                }
    }

        for (int c= 1; c < 12; c= c + 4)
                {   
                    if(r[c] != r[14]){
                    m[c] = 7;
                    E2 = E2 + r[c] - 1;

std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
                    //system("pause");
                }
    else 
                {
                        //m[c] = 7;
                Finished = true;
std::cout<<"(C,E)"<<"-->"<<"("<<r[c]<<","<<E2 + r[c] - 1 <<")"<< std::endl;
                }
        }
        for (int c= 13; c < 16; ++c)
                {   m[c] = 8;
                    if(r[c] != r[16]){

                    E2 = E2 + r[c] - 1;

std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
                    //system("pause");
                }
    else 
                {
                    //  m[c] = 8;
                Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
std::cout << "\n"<<"finish-->" << r[c] << "\t" <<"E-->"<< E1 <<"\n" ;
                }
        }
}   

//int E3 = 6;
void nextpos3(){ 

    std::cout<<"\n"<<"Search Path 4"<<"\n";
for (int c= 0; c <= 12; c= c+ 4)
    //for (int c= 4; c <= 12; c= c+ 4)
        {   if(r[c] != r[12])
                {   
                    m[c] = 11;
                    E3 = E3 + r[c] - 1;
                    std::cout<<"\n";
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 <<")"<< std::endl;
                    //system("pause");
                }
    else 
                {
                m[c] = 11;
                Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 + r[c] - 1 <<")"<< std::endl;
                }
    }
    E3 = E3 -1;
        for (int c= 13; c < 16; c++)
                {   
                    if(r[c] != r[15]){
                    m[c] = 12;
                    E3 = E3 + r[c] - 1;

std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 <<")"<< std::endl;
                    //system("pause");
                }
    else 
                {
                        m[c] = 12;
                        Finished = true;
                        std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 + r[c] - 1 <<")"<< std::endl;
                }
        }

}   

void nextpos4(){ 
    int E4 = 6;
    std::cout<<"\n"<<"Search Path 5"<<"\n";
for (int c= 4; c <= 8; c= c+ 4)
    //for (int c= 4; c <= 12; c= c+ 4)
        {   if(r[c] != r[8])
                {   cout<<E4;
                    m[c] = 13;
                    E4 = E4 + r[c] - 1;
                    std::cout<<"\n";
                    std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 <<")"<< std::endl;
                    //system("pause");
                }
    else 
                {
                m[c] = 13;
                Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 + r[c] - 1 <<")"<< std::endl;
                }
    }
    E4 = E4 -1;
        for (int c= 9; c < 12; c++)
                {   
                    if(r[c] != r[11]){
                    m[c] = 14;
                    E4 = E4 + r[c] - 1;

std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 <<")"<< std::endl;
                    //system("pause");
                }
            else 
                {
                        m[c] = 14;
                        Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 + r[c] - 1 <<")"<< std::endl;
                }
        }
        for (int c= 11; c < 16; c = c + 4)
                {   
                    if(r[c] != r[15]){
                    //m[c] = 15;
                    E4 = E4 + r[c] - 1;

std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 <<")"<< std::endl;
                    //system("pause");
                }
            else 
                {
                        m[c] = 15;
                        Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 + r[c] - 1 <<")"<< std::endl;
                }
        }

}   
void PrintRoute( void )
{//std::cout << "Start Node : " << g[0]<<"\t" <<"Finish Node : " << g[15] <<"\n";

std::cout << "\n"<< "Route 1 : "<<"\n";
  for (int c= 1; c < 14; c++)
  {  
        if( m[c] == 1)
            std::cout<< "[r]-->"<<"\t";
            else if (m[c] == 2)
                 std::cout<< "[u]-->"<<"\t";
                    else 
                     std::cout <<"";
   }
    std::cout << "\n";   
}

void PrintRoute1( void )
{//std::cout << "\n"<< "Start Node : " << g[0]<<"\t" << "Finish Node : " << g[15]<<"\n";
std::cout << "\n"<< "Route 2 : "<<"\n";
  for (int c= 1; c < 16; c++)
  {
        if( m[c] == 3 )
            std::cout<< "[r]-->"<<"\t";
            else if (m[c] == 4 )
                 std::cout<< "[u]-->"<<"\t";
            else if (m[c] == 5)
                std::cout<<"[r]-->"<<"\t";
                else 
                     std::cout <<"";
   }

    std::cout << "\n";

}

void PrintRoute2( void )
{//std::cout << "\n"<< "Start Node : " << g[0] <<"\t"<< "Finish Node : " << g[15]<<"\n";
std::cout << "\n"<< "Route 3: "<<"\n";
  for (int c= 0; c < 15; c++)
  {
        if( m[c] == 6 || m[c] == 8 )
            std::cout<< "[r]-->"<<"\t";
            else if (m[c] == 7 )
                 std::cout<< "[u]-->"<<"\t";
            //else if (m[c] == 8)
            //  std::cout<<"[r]"<<"\t";
                else 
                     std::cout <<"";
   }

    std::cout << "\n";
    }

void PrintRoute3( void )
{//std::cout << "Start Node : " << g[0] <<"\t"<< "Finish Node : " << g[15]<<"\n";

std::cout << "\n"<< "Route 4 : "<<"\n";
  for (int c = 0; c <= 15; c++)
  {  
        if( m[c] == 11)
            std::cout<< "[u]-->"<<"\t";
            else if (m[c] == 12)
                 std::cout<< "[r]-->"<<"\t";
                    else 
                     std::cout <<"";
   }
    std::cout << "\n";   
}
void PrintRoute4( void )
{//std::cout << "\n"<< "Start Node : " << g[0]<<"\t" << "Finish Node : " << g[15]<<"\n";
std::cout << "\n"<< "Route 2 : "<<"\n";
  for (int c= 1; c < 16; c++)
  {
        if( m[c] == 13 )
            std::cout<< "[u]-->"<<"\t";
            else if (m[c] == 14 )
                 std::cout<< "[r]-->"<<"\t";
            else if (m[c] == 15)
                std::cout<<"[u]-->"<<"\t";
                else 
                     std::cout <<"";
   }
}

int main( )

{   int E,E1,E2,E3;

    int a, b;
int m[4][4];
ifstream in;
char state1[] = {'d','p','S','1','3'};
printmatrix(state1, 4);
cout<<"open test file : press";
 in.open("C:/Users /test.txt");
        if (!in) {
                cout << "Cannot open file.\n";
                    system("pause");
                return 0;
                 }

     for (b = 1; b <= 4; b++) {cout << "\n";
            for (a = 1; a <= 4; a++) {
                  in >> m[a][b]; 

                 cout <<"\t" << "["<<m[a][b] <<"]" ; 
                                  }
                cout << "\n";
                //
             } 
            in.close();
                std::cout << "\n";  
int mt[6][6];
     cout<<"open training file : press";

             ifstream i;

             i.open("C:/Users/training.txt");
                if (!i) {
                    cout << "Cannot open file.\n";
                    return 0;
                 }
                cout << "\n";

                for (b = 1; b <= 6; b++) {cout << "\n";
                    for (a = 1; a <= 6; a++) {
                             i >> mt[a][b]; 
                             cout <<"\t" << "["<<mt[a][b] <<"]" ; 
                                  }
                        cout << "\n";
                         } 
            i.close();
                std::cout << "\n";  


    system("pause");

    int F = E = E1 = E2= E3;
    std::cout<< "Enter Energy E";
    //std::cin>>E >> E1 >> E2 >> E3 >>E4;
    std::cin>>F;
    std::cout << "Start Node : " << g[0] <<"\t"<< "Finish Node : " << g[15]<<"\n";
    while( !Finished )
        FindNextPos();
        PrintRoute( );
        nextpos1();
        PrintRoute1();
        nextpos2();
        PrintRoute2();
        nextpos3();
        PrintRoute3();
        nextpos4();
        PrintRoute4();
        std::cin.get( );
        system("pause");

    return 0;
}
4

1 回答 1

0

您将需要某种寻路算法

A*(“A-Star”)算法可能是一个很好的起点。这个页面很好地解释了它。如果 A* 看起来很复杂,您可能想先尝试 Dijkstra 算法。Dijkstra 的算法与 A* 相同,但没有启发式(估计目标成本)。

于 2012-04-12T03:09:15.093 回答