遍历网格


<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
    评论
### 回答1: 答:我可以使用Python编写代码来绘制一段动画来描述该过程,首先定义三个变量x,y,z分别表示三个轴的范围,然后使用网格函数生成网格坐标,从随机一个点出发,依次遍历网格上的每个点,在每一次遍历单元格之后,使用matplotlib库绘制出当前遍历过程,最后传入真实的数据生成动画,以描述这一过程。 ### 回答2: 可以使用Python中的matplotlib和numpy库来编写代码绘制该过程的动画。首先,我们需要导入相应的库: ```python import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation ``` 然后,定义x_range、y_range和z_range: ```python x_range = np.arange(0, 11, 1) y_range = np.arange(0, 11, 1) z_range = np.arange(1.5, 1.6, 0.1) ``` 接下来,我们需要定义一个函数来生成所有相邻的点。当网格一个点坐标为(x, y, z)时,相邻点的坐标为(x-1, y, z),(x+1, y, z),(x, y-1, z),(x, y+1, z),(x, y, z-0.1)和(x, y, z+0.1)。定义如下: ```python def generate_adjacent_points(point): adjacent_points = [] for i in [-1, 1]: adjacent_points.append((point[0]+i, point[1], point[2])) adjacent_points.append((point[0], point[1]+i, point[2])) adjacent_points.append((point[0], point[1], point[2]-0.1)) adjacent_points.append((point[0], point[1], point[2]+0.1)) return adjacent_points ``` 然后,定义一个函数用于绘制动画: ```python def animate(i): point = np.random.choice(x_range), np.random.choice(y_range), np.random.choice(z_range) adjacent_points = generate_adjacent_points(point) x = [point[0] for point in adjacent_points] y = [point[1] for point in adjacent_points] z = [point[2] for point in adjacent_points] ax.clear() ax.scatter(x, y, z, c='b') ax.set_xlim(0, 11) ax.set_ylim(0, 11) ax.set_zlim(1.4, 1.7) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ani = animation.FuncAnimation(fig, animate, interval=1000, frames=10) plt.show() ``` 最后,调用`animation.FuncAnimation`函数来生成动画,`interval`参数用于设置每一帧之间的间隔时间,`frames`参数用于设置动画的总帧数。 运行代码,即可看到一个描述从网格上随机一个点出发依次遍历相邻点的动画。 ### 回答3: 首先,我们可以使用numpy中的arange函数生成x_range和y_range范围内的点的坐标: ```python import numpy as np x_range = np.arange(0, 11, 1) y_range = np.arange(0, 11, 1) z_range = np.arange(1.5, 1.6, 0.01) ``` 然后,我们可以使用matplotlib库的animation模块绘制动画。具体步骤如下: 1. 首先,导入需要的库: ```python import matplotlib.pyplot as plt import matplotlib.animation as animation ``` 2. 创建画布和子图: ```python fig = plt.figure() ax = plt.axes(projection='3d') ``` 3. 定义初始状态和每一帧的更新函数: ```python def init(): return [] def update(frame): ax.clear() ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') ax.scatter(frame[0], frame[1], frame[2], color='red') return ax.scatter(frame[0], frame[1], frame[2], color='red') ``` 4. 遍历网格上的每个点,将每个点作为帧添加到动画中: ```python frames = [] for x in x_range: for y in y_range: for z in z_range: frames.append([x, y, z]) ani = animation.FuncAnimation(fig, update, frames=frames, init_func=init, blit=True) ``` 5. 显示动画: ```python plt.show() ``` 综上所述,通过以上代码,我们可以使用python编写代码绘制一段动画,描述从网格上的随机一个点出发依次遍历网格上相邻的每个点的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值