遍历网格


<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;
}

 




                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值