您好,我正在尝试编写一个“迷宫中的机器人”。用c ++编写的程序。 [英] Hello, I am trying to write a "robot in the labyrinth" program in c++.

查看:57
本文介绍了您好,我正在尝试编写一个“迷宫中的机器人”。用c ++编写的程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个包含20行和列的二维数组,我已经完成了,第二件事是,我需要在数组的左上角放置一个元素(机器人)(第一行和第一列)。我无法提出任何解决方案,任何帮助将不胜感激。

以下程序的详细说明:

I need to create a 2d array with 20 rows and columns, which i have done, 2nd thing is, I need to put an element(robot) at the top left corner of the array(first row and first column). I can't come up with any solution for that, Any help would be appreciated.
Detailed explanation of the program below:

Write a program "Robot in the labyrinth" that creates a two dimensional array - a labyrinth with 20 rows and 20 columns and fills it with random numbers in the range from 1 to 9. At the beginning the robot is standing in the top-left corner of the labyrinth (element in the first row and the first column in an array). 
|	9	7|
| 3	4	5|
| 5	2	2|
After that the robot moves so that it will always go to the next element whose value is the largest, but the previous element is replaced by zero. 
4	3	9		4	3	9
6		7		6	0	
8	4	2		8	4	2
The program stops work, when the all elements values around the robot are zero.
2	0	6
0		0
7	0	5





我尝试了什么:





What I have tried:

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main(){
	srand(time(0));
            	
           int a[20][20],i,j;
           
           for(i=0; i<=19; i++){
           	for(j=0; j<=19; j++){
           		a[i][j] = rand() % 9+ 1; 
			   }
		   }
		   for(i=0; i<=19; i++){
           	for(j=0; j<=19; j++){
           		 cout<<a[i][j]<<" "; 
			   }
			   cout<<endl;
		   }
            
           		 
           
           return 0;
}

推荐答案

在我看来,你需要存储机器人的当前位置。那可能是结构或数组。我假设你将使用一个数组,第一个位置是x坐标,第二个位置是y坐标。要将机器人的位置设置为左上角,请将两个坐标设置为零:
It seems to me that you need to store the current position of the robot. That could be in a structure or an array. I'll assume you will use an array with the first position the x coordinate and the second position the y coordinate. To set the robot's position to the upper left corner you set both coordinates to zero :
int position[2] = { 0, 0 };

回想起来,我想我会去对于结构方法。这是一种可能性:

In retrospect, I think I would go for the structure approach. Here's one possibility :

typedef struct
{
   int x;
   int y;
} Point2D;

Point2D robotPos = { 0, 0 };   // initialized to origin

robotPos.x = 1;
robotPos.y = 1;             // set to 1,1


创建X和Y变量 - 两个整数 - 以显示机器人当前位置:

Create X and Y variables - both integers - to show the robots current position:
int robotX, robotY;

并将它们都设置为零(因为C ++数组使用零基索引)。当机器人向左移动时,从robotX中减去一个,并在向右移动时添加一个。使用robotY向上和向下执行相同操作。

And set them both to zero (as C++ arrays use zero based indexes). When the robot moves left, subtract one from robotX, and add one when it moves right. Do the same for up and down with robotY.


引用:

我需要放置一个元素(机器人)在数组的左上角(第一行和第一列)。我无法想出任何解决方案,任何帮助都会受到赞赏。

I need to put an element(robot) at the top left corner of the array(first row and first column). I can't come up with any solution for that, Any help would be appreciated.



你没有'放置元素',你只需设置2个变量来记住机器人的位置。


You don't 'put an element', you simply set 2 variables to remember the robot position.

引用:

之后机器人移动,以便它总是转到下一个元素,其值为最大,但前一个元素被零替换。

After that the robot moves so that it will always go to the next element whose value is the largest, but the previous element is replaced by zero.



您有移动机器人的条件。获取示例Excel工作表,填充带有值的小方块并播放机器人,记下用于移动机器人的操作,这是移动机器人的算法。



由于处理电路板的限制会使代码复杂化,你可以用22 * 22数组替换20 * 20数组,并将边框上的元素设置为0.通过这样做,移动代码永远不会超出游戏范围甚至没有检查边界的字段。


You have conditions for moving the robot. Get a sample Excel sheet, fill a small square with values and play robot, write down the proceed you use to move the robot, this is the algorithm to move the robot.

Since handling the limits of the board will complicate your code, you can replace the 20*20 array by a 22*22 array and set the elements on border to 0. By doing so, the moving code will never get outside of play field even without checking borders.


这篇关于您好,我正在尝试编写一个“迷宫中的机器人”。用c ++编写的程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆