本段程序的基本思想是利用蚁群算法中的蚁周模型,来对全局的迷宫图进行信息素的跟新
和为每一只蚂蚁选择下一个方格。 一共会进行RcMax = 2000轮模拟(理论上模拟的次数越多结果
会越接近真实值),而在每一轮中会排除 M = 10只蚂蚁进行探路。同时在算法的回溯思想上采用的
是栈的数据结构来实现的。当栈最终为空时则表示无解。但同时这段程序的一缺点就是:由于我没在
算法中对每一轮的每只探路蚂蚁采用多线程的模式,所以整体的运行效率还不是很高。如读者有好的
思想或建议,请留言。
#include<iostream>
#include<stack>
#include<bitset>
using namespace std;
//坐标类
struct Point
{
int x;
int y;
};
//地图类
template<int A,int B>
class Map
{
public:
int (*p)[B];//1表示为障碍方格,0表示该方格可通
bitset<4> (*around)[B];//记录每一个方格四周四个方法的可选标记
int row;//行数
int col;//列数
Map()
{
p =new int[A][B];
around = new bitset<4>[A][B];
}
Map(Map<A,B> & B1)
{
p =new int[A][B];
around = new bitset<4>[A][B];
row = B1.row;
col = B1.col;
for(int i = 0;i<row;i++)
{
for(int j = 0;j<col;j++)
{
p[i][j] = B1.p[i][j];
around[i][j] = B1.around[i][j];
}
}
}
Map<A,B> & operator=(Map<A,B> & B1)
{
row = B1.row;
col = B1.col;
for(int i = 0;i<row;i++)
{
for(int j = 0;j<col;j++)
{
this->p[i][j] = B1.p[i][j];
around[i][j] = B1.around[i][j];
}
}
return *this;
}
};
//start起始点