迷宫的实现

#pragma once


struct Pos
{
size_t _row;
size_t _col;
};


template<size_t M, size_t N>
class Maze
{
public:
//Maze(int maze[][N])
Maze(int* maze)
{
for (size_t i = 0; i < M; ++i)
{
for (size_t j = 0; j < N; ++j)
{
//_maze[i][j] = maze[i][j];
_maze[i][j] = maze[i*N+j];
}
}
}


void Print()
{
for (size_t i = 0; i < M; ++i)
{
for (size_t j = 0; j < N; ++j)
{
cout<<_maze[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;


if (!shortPath.empty())
{
cout<<"最短路径长度:"<<shortPath.size()<<" 出口";
stack<Pos> tmp = shortPath;
while (!tmp.empty())
{
Pos& top = tmp.top();
printf("[%d,%d]<-", top._row, top._col);
tmp.pop();
}


cout<<"入口"<<endl;
}
}


bool CheckAccess(Pos next)
{
if (next._row < M && next._col < N
&& _maze[next._row][next._col] == 0)
{
return true;
}


return false;
}


bool GetPath(Pos entry)
{
Pos cur, next;
cur = entry;
stack<Pos> path;
path.push(entry);


while (!path.empty())
{
cur = path.top(); // ?
_maze[cur._row][cur._col] = 2;


if (cur._row == M-1)
{
return true;
}


// 上
next = cur;
next._row -= 1;
if (CheckAccess(next))
{
path.push(next); // ?
continue;
}


// 下
next = cur;
next._row += 1;
if (CheckAccess(next))
{
path.push(next);
continue;
}


// 右
next = cur;
next._col += 1;
if (CheckAccess(next))
{
path.push(next);
continue;
}


// 左
next = cur;
next._col -= 1;
if (CheckAccess(next))
{
path.push(next);
continue;
}

// 回溯
path.pop();
}


return false;
}


bool CheckAccess(Pos cur, Pos next)
{
if ((next._row < M && next._col < N)
&& (_maze[next._row][next._col] == 0
|| _maze[next._row][next._col] > _maze[cur._row][cur._col]))
{
return true;
}


return false;
}


void GetShortPath(Pos entry, stack<Pos>& path)
{
path.push(entry);


if (entry._row == M-1)
{
if (shortPath.empty() || path.size() < shortPath.size())
{
shortPath = path;
}


cout<<"找到一个出口"<<"["<<entry._row<<","<<entry._col<<"]"<<endl;
path.pop();
return;
}


// 上
Pos next;
next = entry;
next._row -= 1;
if (CheckAccess(entry, next))
{
_maze[next._row][next._col] = _maze[entry._row][entry._col]+1;
GetShortPath(next, path);
}


// 右
next = entry;
next._col += 1;
if (CheckAccess(entry, next))
{
_maze[next._row][next._col] = _maze[entry._row][entry._col]+1;
GetShortPath(next, path);
}


// 下
next = entry;
next._row += 1;
if (CheckAccess(entry, next))
{
_maze[next._row][next._col] = _maze[entry._row][entry._col]+1;
GetShortPath(next, path);
}




next = entry;
next._col -= 1;
if (CheckAccess(entry, next))
{
_maze[next._row][next._col] = _maze[entry._row][entry._col]+1;
GetShortPath(next, path);
}


path.pop();
}


void GetPathR(Pos entry)
{
_maze[entry._row][entry._col] = 2;
if (entry._row == M-1)
{
cout<<"找到一个出口"<<"["<<entry._row<<","<<entry._col<<"]"<<endl;
return;
}


Pos next;
next = entry;
next._row -= 1;
if (CheckAccess(next))
{
GetPathR(next);
}


next = entry;
next._row += 1;
if (CheckAccess(next))
{
GetPathR(next);
}


next = entry;
next._col += 1;
if (CheckAccess(next))
{
GetPathR(next);
}


next = entry;
next._col -= 1;
if (CheckAccess(next))
{
GetPathR(next);
}
}


protected:
int _maze[M][N];
stack<Pos> shortPath;
};


void TestMaze()
{
int mazeArray[10][10] = 
{
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{2,0,0,0,0,0,0,1,1,1},
{1,1,0,1,1,1,0,1,1,1},
{1,1,0,0,0,0,0,1,1,1},
{1,1,1,1,1,0,1,1,1,1},
{1,1,1,1,1,0,1,1,1,1},
{1,1,1,1,1,0,1,1,1,1},
{1,1,1,1,1,0,1,1,1,1},
{1,1,1,1,1,0,1,1,1,1}
};


/*int mazeArray[10][10] = 
{
{1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1},
{0,0,0,1,1,1,1,1,1,1},
{1,1,0,1,1,1,1,1,1,1},
{1,1,0,0,0,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1},
{1,1,0,1,1,0,1,1,1,1}
};*/


Maze<10, 10> maze((int*)mazeArray);
maze.Print();
Pos entry = {2, 0};


stack<Pos> path;
maze.GetShortPath(entry, path);

maze.Print();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值