数据结构与算法之迷宫路径、图元识别c++描述
#intclude<iostream>
using namespace std;
/*全局变量用于存储弹栈元素值*/
int therow, thecol;
/* 迷宫路径参数结点 */
struct postion
{
int row;
int col;
postion(){}
postion(int theRow, int theCol)
{
row = theRow;
col = theCol;
}
};
/* 数组描述队列 */
template<class T>
class Queue
{
/*queue数组用于实现队列
queue1和queue2用于实现迷宫和图元识别*/
private:
int queueFront;//队列第一个元素
int queueBack;//队列最后一个元素
int capacity;//数组容量
int arraysize;
T* queue;
T* queue1;//存储迷宫和图元的行元素数组
int queuesize1;
int queueFront1;//行元素数组第一个元素,用于后面快速访问队列第一个元素
int queueBack1;//行元素数组最后一个元素
T* queue2;
int queuesize2;
int queueFront2;//列元素数组第一个元素,用于后面快速访问队列第一个元素
int queueBack2;//列元素数组最后一个元素
postion pos;//调用队列结点用于后面迷宫存储当前位置和下一个位置
public:
/*构造函数*/
Queue(int theCapacity)
{
capacity = theCapacity;
queue = new T[capacity];
queue1 = new T[capacity];
queue2 = new T[capacity];
queueFront = 0;
queueBack = capacity - 1;
queuesize1 = 0;
queueFront1 = 0;
queueBack1 = capacity - 1;
queuesize2 = 0;
queueFront2 = 0;
queueBack2 = capacity - 1;
arraysize = 0;
}
/*Queue(int theRow, int theCol)
{
pos->row = theRow;
pos->col = theCol;
}*/
~Queue()
{
delete[] queue;
delete[] queue1;
delete[] queue2;
}
/*判断队列是否为空*/
bool isEmpty() const
{
return arraysize == 0;
}
/*判断队列是否已满*/
bool isFull()
{
return arraysize == capacity;
}
/*重新分配数组空间*/
void reSize()
{
capacity *= 2;
}
/*返回队列首元素*/
int Front()
{
return queue[queueFront];
}
/*返回队列末尾元素*/
int Back()
{
return queue[queueBack];
}
/*向队列中插入迷宫和图元识别数组的行列元素,行元素存储在queue1数组,列元素存储在queue2数组,每次插入采用尾插法*/
void push(int theRow,int theCol)
{
queueBack1 = (queueBack1 + 1) % capacity;
queue1[queueBack1] = theRow;
queueBack2 = (queueBack2 + 1) % capacity;
queue2[queueBack2] = theCol;
queuesize1++;
queuesize2++;
}
/*弹出行*/
int popRow()
{
therow = queue1[queueFront1];
queueFront1 = (queueFront1 + 1) % capacity;
queuesize1--;
return therow;
}
/*弹出列*/
int popCol()
{
thecol = queue2[queueFront2];
queueFront2 = (queueFront2 + 1) % capacity;
queuesize2--;
return thecol;
}
/*返回行数组首元素*/
int FrontRow()
{
return queue1[queueFront1];
}
/*返回列数组首元素*/
int FrontCol()
{
return queue2[queueFront2];
}
/*向普通队列插入元素*/
void push(const T& theElement)
{
if ((arraysize + 1) == capacity)
reSize();
queueBack = (queueBack + 1) % capacity;
queue[queueBack] = theElement;
arraysize++;
}
/*普通队列弹出元素*/
int pop(int popElement)
{
if (queueFront == queueBack)
{
return -1;
}
popElement = queue[queueFront];
queueFront = (queueFront + 1) % capacity;
//queue[queueFront].~T;
return popElement;
}
/* 数组描述队列实现迷宫路径 */
bool findPath(int grid[][9],int size)
{
cout << "输入迷宫为:" << endl;
for (int i = 1; i < size - 1; i++)
{
for (int j = 1; j < size - 1; j++)
cout << grid[i][j] << ",";
cout << endl;
}
int pathLength = 0;
postion start = { 3, 2 };
postion finish = { 4, 6 };
cout << start.row << "," << start.col << endl;
cout << finish.row << "," << finish.col << endl;
postion offest[4];
offest[0].row = 0; offest[0].col = 1;
offest[1].row = 1; offest[1].col = 0;
offest[2].row = 0; offest[2].col = -1;
offest[3].row = -1; offest[3].col = 0;
for (int i = 0; i < 4; i++)
{
cout << offest[i].row << "," << offest[i].col << endl;
}
for (int i = 0; i < size; i++)
{
grid[0][i] = grid[size - 1][i] = 1;
grid[i][0] = grid[i][size - 1] = 1;
}
cout << "设置障碍物后迷宫为:" << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
cout << grid[i][j] << ",";
cout << endl;
}
postion here = { start.row,start.col };
cout << start.row << "," << start.col << endl;
cout << finish.row << "," << finish.col << endl;
grid[start.row][start.col] = 2;
int numOfbrs = 4;
Queue<T>q(10);
postion nbr;
cout << here.row << "," << here.col << endl;
while(1)
{
for (int i = 0; i < numOfbrs; i++)
{
nbr.row = here.row + offest[i].row;
nbr.col = here.col + offest[i].col;
cout << "下一个位置是:" << nbr.row << "," << nbr.col << endl;
if (grid[nbr.row][nbr.col] == 0)
{
grid[nbr.row][nbr.col] = grid[here.row][here.col] + 1;
break;
}
if (grid[nbr.row][nbr.col] == grid[here.row][here.col] - 1 &&
grid[here.row+1][here.col] != 0 && grid[here.row][here.col+1] != 0 && grid[here.row][here.col-1] != 0 && grid[here.row-1][here.col] != 0)
{
grid[here.row][here.col] = grid[nbr.row][nbr.col] + 1;
break;
}
}
q.push(nbr.row, nbr.col);
if (nbr.row == finish.row && nbr.col == finish.col)
break;
if (q.queuesize1 == 0)
return false;
here.row = q.FrontRow();
here.col = q.FrontCol();
q.popRow();
q.popCol();
cout << "当前位置是:" << here.row << "," << here.col << endl;
}
pathLength = grid[finish.row][finish.col] - 2;
postion path[15];
here.row = finish.row;
here.col = finish.col;
for (int j = pathLength - 1; j >= 0; j--)
{
path[j].row = here.row;
path[j].col = here.col;
for (int i = 0; i < numOfbrs; i++)
{
nbr.row = here.row + offest[i].row;
nbr.col = here.col + offest[i].col;
if (grid[nbr.row][nbr.col] == j + 2)
break;
}
here.row = nbr.row;
here.col = nbr.col;
}
cout << "迷宫路线距离为:" << pathLength << endl;
cout << "迷宫路线坐标是:";
for (int i = 0; i < pathLength; i++)
{
cout << "(" << path[i].row << "," << path[i].col << ")" << ",";
}
cout << endl;
return true;
}
/* 图元识别 */
void labelComponets(int pixel[][9], int size)
{
postion offest[4];
offest[0].row = 0; offest[0].col = 1;
offest[1].row = 1; offest[1].col = 0;
offest[2].row = 0; offest[2].col = -1;
offest[3].row = -1; offest[3].col = 0;
for (int i = 0; i < size; i++)
{
pixel[0][i] = pixel[size - 1][i] = -1;
pixel[i][0] = pixel[i][size - 1] = -1;
}
cout << "设置障碍物后图元矩阵为:" << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
cout << setw(5) << pixel[i][j] << ",";
cout << endl;
}
int numOfNbrs = 4;
Queue<int> q(20);
postion here, nbr;
int id = 1;
for (int r = 1; r < size - 1; r++)
{
for (int c = 1; c < size - 1; c++)
{
if (pixel[r][c] == 1)
{
pixel[r][c] == ++id;
here = { r,c };
while (1)
{
for (int i = 0; i < numOfNbrs; i++)
{
nbr.row = here.row + offest[i].row;
nbr.col = here.col + offest[i].col;
if (pixel[nbr.row][nbr.col] == 1)
{
pixel[nbr.row][nbr.col] = id;
q.push(nbr.row, nbr.col);
}
}
if (q.queuesize1 == 0)
break;
here.row = q.FrontRow();
here.col = q.FrontCol();
q.popRow();
q.popCol();
}
}
}
}
cout << "****************" << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout << setw(5) << pixel[i][j] << ",";
}
cout << endl;
}
}
/*输出队列queue1和queue2的元素,即输出迷宫和图元识别的数组元素*/
void output(ostream& out)
{
for (int i = 0; i < queuesize1;i++) {
out << queue1[i] << "," << queue2[i] << endl;
}
//out << "队列容量:" << capacity << endl;
}
/*重载<<运算符*/
friend ostream& operator<<(ostream& out, Queue<T>& x)
{
x.output(out);
return out;
}
};
/* 测试图元识别和迷宫路径*/
int main()
{
Queue<int>a(10);
int num[][9] = { {0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0},
{0,0,0,1,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0},
{0,0,0,0,1,1,0,0,0},
{0,1,0,0,0,1,0,0,0},
{0,1,1,1,0,0,0,0,0},
{0,1,1,1,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0}, };
a.labelComponets(num, 9);
a.findPath(num,9);
return 0;
}