康威的生活游戏,数邻居 [英] Conway's Game of Life, counting neighbors
本文介绍了康威的生活游戏,数邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码中某处出错,我想我正在进入一个无限循环. 基本上,我得到一个矩阵和两个索引i,j,并且我需要计算[i] [j]周围有多少个邻居的值为1或2.
I have an error somewhere in my code, I think I am entering an infinite loop. Basically I get a matrix and two indexes, i,j, and i need to count how many neighbors around [i][j] have a value of 1 or 2.
这是我的代码:
int number_of_living_neighbors(matrix mat,int i, int j, int n,int m)
{
int counter=0,row_index=0,column_index=0;
for(row_index=i-1;row_index<=i+1;row_index++)
{
for(column_index=j-1;column_index=j+1;column_index++)
{
if((row_index>=0)&&(row_index<n)&&(column_index>=0)&&(column_index<m))
{
if((mat[row_index][column_index]==1)||(mat[row_index][column_index]==2))
counter++;
if((row_index==i)&&(column_index==j)&&(mat[i][j]==1))
counter--;
}
}
}
printf("The number of living neighbors is %d", counter);
return counter;
}
它不打印任何内容. mat是我得到的矩阵,i,j是指针,n是行数,m是列数.
it doesnt print anything. mat is the matrix i get, i,j are the pointers, n is number of rows, m is number of columns.
推荐答案
for(column_index = j-1 ; column_index = j+1; column_index++)
您的条件包含一个分配,而不是一个比较.您可能要使用<=
而不是=
:
Your condition contains an assignment, not a comparison. You probably want to use <=
instead of =
:
for(column_index = j-1 ; column_index <= j+1; column_index++)
这篇关于康威的生活游戏,数邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文