<table border="1" width="200" cellspacing="1" cellpadding="1"><tbody><tr><td>1,1</td><td>1,2</td></tr><tr><td>2,1<span style="white-space:pre"> </span></td><td>2,2</td></tr><tr><td>3,1</td><td>3,3</td></tr></tbody></table>
从(1,1)到达(3,3),下面为解决方法,使用了递归的方法,也可以使用栈的方法。类似,只不过需要掌握好进出栈就可以了。
<pre name="code" class="cpp">
#include <stdio.h>
#include <iostream>
using namespace std;
const int rowCount=3;//行数和列数
const int colomCount=1;
int stepCount=-1;//路径数目
int allPathCount=0;//统计所有的通过路径数目
int directArray[rowCount+colomCount-1]={0};
void goTheMesh(int begineRow,int beginColom,int goDirect ){
if (begineRow==rowCount && beginColom==colomCount)//满足条件 输出路径
{
std::cout<<"Find a way ,it is the "<<++allPathCount<<" way, need " <<stepCount+1 <<" steps\n";
int curRow=1,curClom=1;
cout<<"start from [1,1]-->";
for (int i = 0; i <=stepCount; ++i)
{
if(directArray[i]==1){
// cout<<"-->right ";
++curClom;
}else{
++curRow;
// cout<<"-->up ";
}
if (curRow!=rowCount || curClom!=colomCount)
{
/* code */
cout<<"["<<curRow<<","<<curClom<<"]-->";
}
else{
cout<<"["<<curRow<<","<<curClom<<"] end!!";
}
/* code */
}
return;
/* code */
}
if (begineRow>rowCount && goDirect!=1)//不能通过
{
// --row;
return;
/* code */
}
if (beginColom>colomCount && goDirect==1)//不能通过
{
// --colom;
return;
/* code */
}
// ++stepCount;
directArray[++stepCount]=1;//记录下次方向
goTheMesh(begineRow,beginColom+1,1);//向右边前进
directArray[stepCount]=0;//记录下次方向
goTheMesh(begineRow+1,beginColom,0);//向下边前进
--stepCount;//退回上一步
}
int main()
{
cout<<"遍历网格,中间无阻挡。\n";
goTheMesh(1,1,1);
return 0;
}
如果有阻挡,只需要记录阻挡的坐标,当达到阻挡位置时直接返回即可。<pre name="code" class="cpp">#include <stdio.h>
#include <iostream>
using namespace std;
const int rowCount=4;//行数和列数
const int colomCount=4;
int stepCount=-1;//路径数目
int allPathCount=0;//统计所有的通过路径数目
int directArray[rowCount+colomCount-1]={0};
int stopBarArray[2][2]={{2,2},{1,4}};
bool stopBarTest(int row ,int colom){
for (int i = 0; i < 2; ++i)
{
if (stopBarArray[i][0]==row && stopBarArray[i][1]==colom)
{
cout<<row<<","<<colom<<"test";
return true;/* code */
}
/* code */
}
return false;
}
void goTheMesh(int begineRow,int beginColom,int goDirect ){
if (stopBarTest(begineRow,beginColom))
{
return;
/* code */
}
if (begineRow==rowCount && beginColom==colomCount)//满足条件 输出路径
{
std::cout<<"Find a way ,it is the "<<++allPathCount<<" way, need " <<stepCount+1 <<" steps\n";
int curRow=1,curClom=1;
cout<<"start from [1,1]-->";
for (int i = 0; i <=stepCount; ++i)
{
if(directArray[i]==1){
// cout<<"-->right ";
++curClom;
}else{
++curRow;
// cout<<"-->up ";
}
if (curRow!=rowCount || curClom!=colomCount)
{
/* code */
cout<<"["<<curRow<<","<<curClom<<"]-->";
}
else{
cout<<"["<<curRow<<","<<curClom<<"] end!!\n";
}
/* code */
}
return;
/* code */
}
if (begineRow>rowCount && goDirect!=1)//不能通过
{
// --row;
return;
/* code */
}
if (beginColom>colomCount && goDirect==1)//不能通过
{
// --colom;
return;
/* code */
}
// ++stepCount;
directArray[++stepCount]=1;//记录下次方向
goTheMesh(begineRow,beginColom+1,1);//向右边前进
directArray[stepCount]=0;//记录下次方向
goTheMesh(begineRow+1,beginColom,0);//向下边前进
--stepCount;//退回上一步
}
int main()
{
cout<<"遍历网格,中间无阻挡。\n";
goTheMesh(1,1,1);
return 0;
}