如何在网格周围创建边界
how to create a boundry around a grid
程序应该输出一个12x24的网格,所有外部行输出0,内部行输出1
这是我为了让第一列和第一行输出0所做的尝试:
#include <iostream>
using namespace std;
#define N 24
// print:
//
// Prints the simulation matrix M as spaces, *'s, and T's.
//
void print(int M[][N], int ROWS, int COLS)
{
// YOU MUST IMPLEMENT THIS:
}
//
// fill:
//
// Fills the simulation matrix such that the boundary rows
// and columns are empty, the internal area is all trees,
// and one tree is burning at index position (row, col).
//
void fill(int M[][N], int ROWS, int COLS, int row, int col)
{
// YOU MUST IMPLEMENT THIS:
//
// main:
}//
int main()
{
int M[N/2][N];
int ROWS, COLS;
int r, c;
ROWS = sizeof(M) / sizeof(M[0]);
COLS = sizeof(M[0]) / sizeof(M[0][0]);
fill(M, ROWS, COLS, 1, 1);
for(r=0; r< ROWS; r++)
{
for(c=0; c< COLS; c++)
{
if(ROWS>1)
{
M[ROWS][COLS]=1;
cout<< M[ROWS][COLS];
}
else
{
M[ROWS][COLS]=0;
cout<< M[ROWS][COLS];
}
}
cout<< endl;
}
print(M, ROWS, COLS);
return 0;
}
如何做到这一点?
首先,问你自己:"我将如何创建一个盒子?"创建一个盒子,需要4个边。然而, 一个人能做的最简单的盒子包含两行,因为我们可以取盒子的高度为零-或无穷小。在代码中,你至少需要2行来创建一个框。
一样:
000000000000000
000000000000000
然而,它没有的高度。换句话说,这是一个零高度的边框。因此创建一个像这样的框:
000000000000000
011111111111110
000000000000000
你注意到了什么?第一行和最后一行都是0。中间行的第一个和最后一个元素都是0,该行的其他元素都是1
进一步扩展:
000000000000000
011111111111110
011111111111110
000000000000000
我们看到了相同的模式-因此可以将其扩展到第n行情况。因此,算法为:
- 第一行和最后一行均为零。
- 对于所有其他行,这些行的第一列和最后一列都为0。
- 其他设置为1
因此,在你的例子中:
for(r=0; r< ROWS; r++)
{
for(c=0; c < COLS; c++)
{
if (r == 0 || r == ROWS - 1) {
M[r][c]=0;
}
else if(c == 0 || c == COLS -1) {
M[r][c]=0;
}
else {
M[r][c]=1;
}
cout<< M[r][c];
}
cout << endl;
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- CGAL:如何创建填充边界曲线的曲面网格?
- 在 c++ 中是否允许创建具有运行时边界的数组?
- 在光流路的场周围创建边界框
- 在 C/C++ 中创建对象时编译器优化的边界是什么
- 如何创建精灵无法通过的边界框
- 创建无边界的DirectShow窗口
- 如何在网格周围创建边界
- Linux护士覆盖由box()创建的边界
- 如何创建 3D 边界框?(直接 3d 11,HLSL)
- 如何在Maya中创建带有边界框的MPxTransform节点