如何在二维数组中找到索引为 0 或 1 的二维形状的周长和面积?
How to find circumference and area of a 2D shape whose indices are given as 0 or 1 in a 2D array?
给定一个大小为 20x20 的二维数组,其值类似于二维形状,例如正方形或矩形:
public static int[][] rectangle= {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
我想在执行以下操作的算法中找到它的周长和面积:
从中心点 (point[10][10]) 开始寻找最近的 (1) 点。
以最近的(1)点为起点,遍历所有剩余的1来统计
周长.
计算圆周围的零点个数,计算面积
下面是我现在所在的位置; “猜测”方法计算最近的 (1) 点并执行“计数”方法,然后计算周长。
public static void guess() {
boolean found = false;
if(!found) {
int y = 10;
for(int x = 10; x <= 20; x++) {
if(rectangle[x][y]==1) {
rectangle[x][y] = 2;
found = true;
break;
}else if(rectangle[x][y++]==1) {
rectangle[x][y] = 2;
found = true;
break;
}
}
}
if(!found) {
int y = 10;
for(int x = 10; x >= 0; x--) {
if(rectangle[x][y]==1) {
rectangle[x][y] = 2;
found = true;
break;
}else if(rectangle[x][y--]==1) {
rectangle[x][y] = 2;
found = true;
break;
}
}
}
if(!found) {
int x = 10;
for(int y = 10; y <= 20; y++) {
if(rectangle[x][y]==1) {
rectangle[x][y] = 2;
found = true;
break;
}else if(rectangle[x][y++]==1) {
rectangle[x][y] = 2;
found = true;
break;
}
}
}
if(!found) {
int x = 10;
for(int y = 10; y >= 0; y--) {
if(rectangle[x][y]==1) {
rectangle[x][y] = 2;
found = true;
break;
}else if(rectangle[x][y--]==1) {
rectangle[x][y] = 2;
found = true;
break;
}
}
}
for(int i = 0; i < 20; i++) {
for(int j = 0; j < 20; j++) {
if(rectangle[i][j] == 2) {
Count(i, j);
break;
}
}
}
}
public static void Count(int x, int y) {
public int circumf;
int tx = x;
int ty = y;
for(int c = 40; c >=0; c--) {
if((c/2)-1<0 || x>=20 || x<0 || y>=20 || y<0) break;
if(rectangle[x][(int) (c/2)-1]==1 || rectangle[(int) (c/2)-1][y]==1
|| rectangle[x++][y]==1 || rectangle[x][y++]==1
|| rectangle[x--][(int) (c/2)-1]==1 || rectangle[(int) (c/2)-1][y--]==1 || rectangle[x--][y]==1 || rectangle[x][y--]==1) {
circumf++;
}
}
x = tx;
y = ty;
for(int c = 0; c <=40; c++) {
if((c/2)>=20 || x>=20 || x<0 || y>=20 || y<0) break;
if(rectangle[x][(int) (c/2)]==1 || rectangle[(int) (c/2)][y]==1
|| rectangle[x++][y]==1 || rectangle[x][y++]==1
|| rectangle[x--][(int) (c/2)]==1 || rectangle[(int) (c/2)]
[y--]==1 || rectangle[x--][y]==1 || rectangle[x][y--]==1) {
circumf++;
}
}
System.out.print(circumf);
现在,guess 方法可以正确计算最近的点,但是 count 方法不能正确计算上例中接近 70 的周长。
关于面积计算的算法,我还没搞清楚
上面的代码并不是我所知道的最出色或最有条理的代码,但我们将不胜感激任何帮助!
假设您在索引 [i][j]
找到最近的一个。由于您的对象是矩形,因此您需要检查交叉图案中最接近的值。
例如你在矩形左边缘的中间一个:
0 1 0
0 1 0
0 1 0
你检查 1 的“继续”找到边缘方向。有两种可能的结果
[x-1][y]=[x][y]=[x+1][y] or [x][y-1]=[x][y]=[x][y+1]==1
所以,你发现边缘是垂直的。继续迭代这条垂直线,直到条件 [x][y-1]==1
为 false
。然后对水平线做同样的事情。
现在对于 0 的计数,您可以在执行上述操作时存储所有角的索引 i、j check.If您知道它们的位置并且数组的大小是固定的您可以计算 0喜欢:
0's area = [(DL_corner_index - 1) - (UL_corner_index + 1)] x [(UR_corner_index - 1) - (UL_corner_index + 1)]
int rectangle[10][10] ={
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0}
};
//calculating area of zeros with known corner coordinates
//you have stored the coordinates in variables like this
int UL_X=1,UR_X=8;
int UL_Y=1,DL_Y=8;
int area = ((UR_X-1) - UL_X)*((DL_Y-1)-UL_Y);
cout<<area; //36
}
给定一个大小为 20x20 的二维数组,其值类似于二维形状,例如正方形或矩形:
public static int[][] rectangle= {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
我想在执行以下操作的算法中找到它的周长和面积:
从中心点 (point[10][10]) 开始寻找最近的 (1) 点。
以最近的(1)点为起点,遍历所有剩余的1来统计 周长.
计算圆周围的零点个数,计算面积
下面是我现在所在的位置; “猜测”方法计算最近的 (1) 点并执行“计数”方法,然后计算周长。
public static void guess() {
boolean found = false;
if(!found) {
int y = 10;
for(int x = 10; x <= 20; x++) {
if(rectangle[x][y]==1) {
rectangle[x][y] = 2;
found = true;
break;
}else if(rectangle[x][y++]==1) {
rectangle[x][y] = 2;
found = true;
break;
}
}
}
if(!found) {
int y = 10;
for(int x = 10; x >= 0; x--) {
if(rectangle[x][y]==1) {
rectangle[x][y] = 2;
found = true;
break;
}else if(rectangle[x][y--]==1) {
rectangle[x][y] = 2;
found = true;
break;
}
}
}
if(!found) {
int x = 10;
for(int y = 10; y <= 20; y++) {
if(rectangle[x][y]==1) {
rectangle[x][y] = 2;
found = true;
break;
}else if(rectangle[x][y++]==1) {
rectangle[x][y] = 2;
found = true;
break;
}
}
}
if(!found) {
int x = 10;
for(int y = 10; y >= 0; y--) {
if(rectangle[x][y]==1) {
rectangle[x][y] = 2;
found = true;
break;
}else if(rectangle[x][y--]==1) {
rectangle[x][y] = 2;
found = true;
break;
}
}
}
for(int i = 0; i < 20; i++) {
for(int j = 0; j < 20; j++) {
if(rectangle[i][j] == 2) {
Count(i, j);
break;
}
}
}
}
public static void Count(int x, int y) {
public int circumf;
int tx = x;
int ty = y;
for(int c = 40; c >=0; c--) {
if((c/2)-1<0 || x>=20 || x<0 || y>=20 || y<0) break;
if(rectangle[x][(int) (c/2)-1]==1 || rectangle[(int) (c/2)-1][y]==1
|| rectangle[x++][y]==1 || rectangle[x][y++]==1
|| rectangle[x--][(int) (c/2)-1]==1 || rectangle[(int) (c/2)-1][y--]==1 || rectangle[x--][y]==1 || rectangle[x][y--]==1) {
circumf++;
}
}
x = tx;
y = ty;
for(int c = 0; c <=40; c++) {
if((c/2)>=20 || x>=20 || x<0 || y>=20 || y<0) break;
if(rectangle[x][(int) (c/2)]==1 || rectangle[(int) (c/2)][y]==1
|| rectangle[x++][y]==1 || rectangle[x][y++]==1
|| rectangle[x--][(int) (c/2)]==1 || rectangle[(int) (c/2)]
[y--]==1 || rectangle[x--][y]==1 || rectangle[x][y--]==1) {
circumf++;
}
}
System.out.print(circumf);
现在,guess 方法可以正确计算最近的点,但是 count 方法不能正确计算上例中接近 70 的周长。
关于面积计算的算法,我还没搞清楚
上面的代码并不是我所知道的最出色或最有条理的代码,但我们将不胜感激任何帮助!
假设您在索引 [i][j]
找到最近的一个。由于您的对象是矩形,因此您需要检查交叉图案中最接近的值。
例如你在矩形左边缘的中间一个:
0 1 0
0 1 0
0 1 0
你检查 1 的“继续”找到边缘方向。有两种可能的结果
[x-1][y]=[x][y]=[x+1][y] or [x][y-1]=[x][y]=[x][y+1]==1
所以,你发现边缘是垂直的。继续迭代这条垂直线,直到条件 [x][y-1]==1
为 false
。然后对水平线做同样的事情。
现在对于 0 的计数,您可以在执行上述操作时存储所有角的索引 i、j check.If您知道它们的位置并且数组的大小是固定的您可以计算 0喜欢:
0's area = [(DL_corner_index - 1) - (UL_corner_index + 1)] x [(UR_corner_index - 1) - (UL_corner_index + 1)]
int rectangle[10][10] ={
{0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0}
};
//calculating area of zeros with known corner coordinates
//you have stored the coordinates in variables like this
int UL_X=1,UR_X=8;
int UL_Y=1,DL_Y=8;
int area = ((UR_X-1) - UL_X)*((DL_Y-1)-UL_Y);
cout<<area; //36
}