C++ 如何在至少有1个邻居等于1的数组中设置长方体
我试图遍历一个数组,找出哪些数组至少有一个直接邻居 我得到的答案是C++ 如何在至少有1个邻居等于1的数组中设置长方体,c++,C++,我试图遍历一个数组,找出哪些数组至少有一个直接邻居 我得到的答案是 display 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 display 11111 10001 11111 11111 11111 box :1 :1 box :1 :3 这是完美的。我可以从输出中看到哪些阵列正好有1个相邻阵列[1][1]和阵列[1][3] 现在,我该如何将这2个元素(数组[1][1]和数组[1][3])=1?如果您只需要“至少有一
display
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
display
11111
10001
11111
11111
11111
box :1 :1
box :1 :3
这是完美的。我可以从输出中看到哪些阵列正好有1个相邻阵列[1][1]和阵列[1][3]
现在,我该如何将这2个元素(数组[1][1]和数组[1][3])=1?如果您只需要“至少有一个邻居是1”,我真的很困惑为什么要尝试匹配整个邻居。你有没有理由不能这么做:
// going through the loop to figure out which ones have at least 1 neighbor
for (int x = 0; x <5 ; x++)
{
for (int y = 0; y <5 ; y++)
{
if ( (array[x][y] == 0 )
&&
(array [x-1][y] == 1 || array [x+1][y] == 1 ||
array [x][y-1] == 1 || array [x][y+1] ==1 ||
array [x+1][y+1]==1 || array [x-1][y-1]==1 ||
array [x-1][y+1]==1 || array [x+1][y-1] == 1)
)
{
cout << endl;
cout << "box :" << x << " :" << y << endl;
}
}
//遍历循环以确定哪些循环至少有一个邻居
对于(int x=0;x由于多次使用嵌套的“display”循环,因此最好将其分解为自己的函数:
void Draw(int array[5][5]) {
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 5; ++y)
std::cout << array[x][y] << ' ';
std::cout << '\n';
}
}
void Draw(int数组[5][5]){
对于(int x=0;x<5;++x){
对于(int y=0;y<5;++y)
std::cout你说的“仅设置那些…=0”是什么意思?它们已经是0了。我的错误。我的意思是1。现在在主函数堆栈的级别上获得另一个包含两个索引的数组,将上面的点存储在其中,然后读取数组中的点,最后将它们设为1。@myohmy,那么你说的是形态学上的“扩展”带3x3内核的过滤器?我对编程不太熟悉。不确定“带3x3内核的形态学“扩展”过滤器”是什么意思Hanks peter。我主要关心的是将那些带2个邻居的过滤器设置为1;我不确定什么是扩展谢谢..我从你的代码中得到了一些类似的东西,但有些东西让我有点不舒服..如果你设置..if(count==2)array[x][y]=1;对于if(count==1)array[x][y]=1;,它会将只有on邻居的单元格更改为1,您将看到3个零中的最后一个仍然是=0。但实际上,中间的零应该保持为0,其余的为1。现在应该可以正常工作。请确保保持if(count==2)dummyarray[x][y]=0;
未更改。谢谢。当我们想知道他们是否有两个邻居时,计数==2是正确的。如果我们想知道他们是否只有一个邻居呢?这样如果他们只有一个邻居,他们就会变成1;好的。谢谢!我想这也行。我正在考虑用另一种方式构建它,但我会接受它。谢谢!事实上我想我是仍然困惑。我真的不理解你做的“缩短”。你能做同样的事情,但用我编码的方式。我只是想了解发生了什么。
// going through the loop to figure out which ones have at least 1 neighbor
for (int x = 0; x <5 ; x++)
{
for (int y = 0; y <5 ; y++)
{
if ( (array[x][y] == 0 )
&&
(array [clamp04(x-1)][y] == 1 || array [clamp04(x+1)][y] == 1 ||
array [x][clamp04(y-1)] == 1 || array [x][clamp04(y+1)] ==1 ||
array [clamp04(x+1)][clamp04(y+1)]==1 || array [clamp04(x-1)][clamp04(y-1)]==1 ||
array [clamp04(x-1)][clamp04(y+1)]==1 || array [clamp04(x+1)][clamp04(y-1)] == 1)
)
{
cout << endl;
cout << "box :" << x << " :" << y << endl;
}
}
void Draw(int array[5][5]) {
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 5; ++y)
std::cout << array[x][y] << ' ';
std::cout << '\n';
}
}
#include <iostream>
using namespace std;
void Draw(int array[5][5]) {
cout << endl;
for (unsigned x = 0; x < 5; ++x) {
for (unsigned y = 0; y < 5; ++y)
cout << array[x][y] << ' ';
cout << '\n';
}
cout << endl;
}
int main() {
int num = 0;
cout << "How many neighbours?: "; //Asks user for the number of neighbours they'd like to check for.
cin >> num; //Saves the number of neighbours to look for in 'num'
int array[5][5];
int dummyarray[5][5]; //A spare array we'll use to keep track of which boxes have the specified number of neighbours.
for (unsigned x = 0; x < 5; ++x) {
for (unsigned y = 0; y < 5; ++y)
array[x][y] = 1; //Here we fill our original array with 1's
}
for (unsigned x = 0; x < 5; ++x) {
for (unsigned y = 0; y < 5; ++y)
dummyarray[x][y] = 1; //Now we fill our dummy array with 1's so it's the same as our original array
}
Draw(array); //Here we call the Draw function, which prints our array for us.
array[1][1] = 0;
array[1][2] = 0; //Set some values of our original array to 0
array[1][3] = 0;
for (unsigned x = 0; x < 5; ++x) { //Here's where the checking starts
for (unsigned y = 0; y < 5; ++y) {
if (array[x][y] == 0) { //If we find a 0, start checking for neighbours of that 0 that are also 0's
int count = -1; //Because this method counts the original box (array[x][y]) when searching for 0's, we anticipate this by removing one from the count, so that we get the correct number of neighbours
for (int vertical = -1; vertical < 2; ++vertical) { //This loop checks boxes above and below our original box
for (int horizontal = -1; horizontal < 2; ++horizontal) { //This loop checks boxes side to side of our original box
if (vertical + y >= 0 && vertical + y < 5 && horizontal + x >= 0 && horizontal + x < 5) { //If we're inside the array bounds (ie, if we're not trying to access array[-1][-2] or something similar which would undefined behaviour), continue
if (array[x + horizontal][y + vertical] == 0) //If the box we're checking is a 0, add 1 to the number of neighbours our original box has
++count;
}
}
}
cout << "(" << x << ", " << y << "): " << count << endl;
if (count == num) dummyarray[x][y] = 0; //If the box we just checked has the number of neighbours we're looking for, mark it in the dummy array.
}
}
}
for (unsigned x = 0; x < 5; ++x) {
for (unsigned y = 0; y < 5; ++y)
array[x][y] = dummyarray[x][y]; //Here we copy all of dummyarray's data to our original array.
}
Draw(array); //Finally, we draw our array again with Draw()
return 0;
}