//搜索有效,但效率不高。从文本文件中搜索节点。每次循环while循环时,起始值都会添加到openlist中。这意味着搜索时间比预期的要长得多。我正在使用指针将内存分配给开放列表,该开放列表存储找到的节点,然后将最好的节点推送到存储最佳寻路路径的闭合列表。我正在使用迭代器来遍历每个列表。任何人都可以帮我提高算法的效率吗?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <conio.h>
#include <deque>
using namespace std;
struct coords
{
int x;
int y;
char ch;
int score;
int cost;
coords* parent;
};
bool search ( deque <coords*> &templist, int x, int y)
{
deque <coords*>::iterator m;
m = templist.begin();
while( m != templist.end())
{
if ((*m)->x == x && (*m)->y == y)
{
return true;
}
m++;
}
return false;
}
int main()
{
deque <coords*> openlist;
deque <coords*> closedlist;
deque <coords*>::iterator p;
deque <coords*>::iterator k;
coords* newstate = new(coords);
coords* start = new(coords);
coords* current = new(coords);
coords* goal = new(coords);
//=========== Reading grid from text file =========== //
vector<string> grid; // creating 10x10 grid.
int row = 10;
int col = 10;
ifstream input( "dMap.txt" ); // The input file
string line; // stores each line.
while(getline(input, line))
{
grid.push_back(line); // get new line.
}
while( input && row <= 100 ) // checking each row and col for values from .txt
{
input >> grid[ row ][ col ];
if( input )
{
if( ++col == 10 )
{
++row;
col = 0;
}
}
}
for(size_t i=0 ; i < grid.size(); ++i) // finding the size of each line
{
cout <<grid[i] << '\n';
}
input.close(); // Grid reading from .txt file closed.
/// Breadth-First Search
goal-> x = 3;
goal-> y = 8;
start->x = 2;
start->y = 2;
openlist.push_back(start);
// Loop until we reach the goal.
while (complete != true)
{
current = openlist.front();
openlist.pop_front();
if (current-> x == goal->x && current->y == goal->y)
{
complete = true;
}
closedlist.push_back(current);
// Generate each possible directio
int count =5;
for( int n = 1; n <count; n++)
{
// North
if( n == 1)
{
newstate = new(coords);
newstate ->x = current-> x;
newstate ->y = current-> y += 1;
if(search(openlist, newstate->x, newstate->y) == false && search(closedlist, newstate->x, newstate->y))
{
openlist.push_back(newstate);
}
}
// East
if( n == 2)
{
newstate = new(coords);
newstate ->x = current->x += 1;
newstate ->y = current->y;
if(search(openlist, newstate->x, newstate->y) == false && search(closedlist, newstate->x, newstate->y))
{
openlist.push_back(newstate);
}
}
// South
if( n == 3)
{
newstate = new(coords);
newstate ->x = current->x;
newstate ->y = current->y -= 1;
if(search(openlist, newstate->x, newstate->y) == false && search(closedlist, newstate->x, newstate->y))
{
openlist.push_back(newstate);
}
}
// west
if( n == 4)
{
newstate = new(coords);
newstate ->x = current->x -= 1;
newstate ->y = current-> y;
if(search(openlist, newstate->x, newstate->y) == false && search(closedlist, newstate->x, newstate->y))
{
openlist.push_back(newstate);
}
cout << "Current: " << current->x << ", " << current->y << endl;
p = openlist.begin();
cout<<"openlist:"<< endl;
while( p != openlist.end() )
{
cout << (*p)->x << " , "<< (*p)->y << endl;
p++;
}
k = closedlist.begin();
cout<<"closedlist:"<< endl;
while( k != closedlist.end() )
{
cout << (*k)->x << " , "<< (*k)->y<< endl;
k++;
}
}
}
cin.get();
}
cout << " GOAL FOUND ";
system("pause");
}