我正在尝试通过一个数组并找出哪些数组至少有一个直接邻居..
我得到的答案是
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]。
现在我该如何设置这两个(数组[1] [1]和数组[1] [3])= 1?
答案 0 :(得分:1)
由于您使用嵌套的'显示'多次循环,将其分解为自己的函数是理想的:
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';
}
}
同样,你找到邻居&#39;使用额外的嵌套循环可以大大缩短检查。
这也将使您更轻松地解决问题:
#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;
}
答案 1 :(得分:0)
我真的很困惑,如果你需要的是&#34;至少有一个邻居是1&#34;你为什么要尝试匹配整个社区?你有什么理由可以做到:
// 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 clamp04(int coord){return coord<0?0:(coord>4)?4:coord;}
然后像
一样使用它// 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;
}
}
另外,作为旁注,如果您将这些值设置为1,则您执行的操作是一种称为"dilation"的形态滤镜操作