马的遍历问题
来源:互联网 发布:哈布斯堡家族知乎 编辑:程序博客网 时间:2024/04/29 20:05
最近在复习一些数据结构和经典算法
在写马的遍历问题时遇到了死循环,不知道问题出在哪里,下面是代码,请高手指点
//马的遍历
bool travelCells(int x,int y,vector<vector<int>>& cellsVV,vector<vector<int>>& passVV)
{
static int hasPassed = 0;
cellsVV[x][y] = 1;
hasPassed++;
if(64 == hasPassed)
return true;
int i,j;
i = x - 1;
j = y - 2;
if (i>=0 && j>=0 && cellsVV[i][j] == 0)
passVV[x*8+y].push_back(i*8+j);
i = x - 2;
j = y - 1;
if (i>=0 && j>=0 && cellsVV[i][j] == 0)
passVV[x*8+y].push_back(i*8+j);
i = x - 2;
j = y + 1;
if (i>=0 && j<8 && cellsVV[i][j] == 0)
passVV[x*8+y].push_back(i*8+j);
i = x - 1;
j = y + 2;
if (i>=0 && j<8 && cellsVV[i][j] == 0)
passVV[x*8+y].push_back(i*8+j);
i = x + 1;
j = y + 2;
if (i<8 && j<8 && cellsVV[i][j] == 0)
passVV[x*8+y].push_back(i*8+j);
i = x + 2;
j = y + 1;
if (i<8 && j<8 && cellsVV[i][j] == 0)
passVV[x*8+y].push_back(i*8+j);
i = x + 2;
j = y - 1;
if (i<8 && j>=0 && cellsVV[i][j] == 0)
passVV[x*8+y].push_back(i*8+j);
i = x + 1;
j = y - 2;
if (i<8 && j>=0 && cellsVV[i][j] == 0)
passVV[x*8+y].push_back(i*8+j);
while(!passVV[x*8+y].empty())
{
int newX = passVV[x*8+y][0]/8;
int newY = passVV[x*8+y][0]%8;
bool res = travelCells(newX,newY,cellsVV,passVV);
if (!res)
{
passVV[x*8+y].erase(passVV[x*8+y].begin());
cellsVV[newX][newY] = 0;
hasPassed--;
}
else
return true;
}
return false;
}
//测试函数
void autoTestPass()
{
vector<vector<int>> cellsVV(8,vector<int>(8));
vector<vector<int>> passVV(64);
for (int i=0;i<8;i++)
{
for (int j=0;j<8;j++)
{
cout<<"起始x:"<<i<<"起始y:"<<j<<endl;
bool res = travelCells(i,j,cellsVV,passVV);
if (res)
{
vector<vector<int>>::const_iterator tmpIter = passVV.begin();
while(tmpIter!=passVV.end())
{
if (!tmpIter->empty())
{
int nPass = *(tmpIter->begin());
cout<<"x:"<<nPass/8<<" && "<<"y:"<<nPass%8<<endl;
}
tmpIter++;
}
}
else
{
cout<<"没有找到遍历路径:(/n";
}
}
}
}
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 中国象棋马的遍历问题
- 马的遍历问题-问题分析
- 【数据结构】马踏棋盘的遍历问题
- 【经典算法问题】马的遍历【回溯】
- cursor遍历的问题
- 树的遍历问题
- 【5003】马遍历问题
- 关于hashmap的遍历问题
- 关于hashmap的遍历问题
- jsp jstl 遍历的问题
- ArrayList的遍历删除问题
- struts2 iterator遍历的问题
- 把微软代码搬到BSP下调试的方法(作者:gooogleman@foxmail.com )
- hibernate中update与saveOrUpdate的区别
- 让memcached和mysql更好的工作
- Cookie 文章 (转2) 觉得很好的COOKIE知识,适合像我一样初学者看
- 头脑风暴
- 马的遍历问题
- 手机网站开发必修课[2]:浏览器兼容性测试
- JavaScript经典书籍
- 怎样查看rpm安装包的安装路径 rpm安装包的安装
- 春节来历
- 珠三角技术沙龙第 5 期报名开始!
- strust1.2 开发问题总结
- access 关键字影响 sql语句报错
- MFC中用ado访问SQL Server 2005数据库