阵列中的邻居

时间:2016-03-25 05:49:21

标签: c arrays if-statement nearest-neighbor

我在查看以下代码中列出的数组中如何识别1中的1时遇到了问题,同时考虑了角落和角落。数组的边缘。

#include <stdio.h>
/* define grid size */
#define SIZE 7
int grid[SIZE][SIZE];
/* function to find the number of occupied adjacent cells */
int neighbors (int i, int j);
void main ()
{
  int i, j, n;
/* initialize the entire grid to be zero */
 for (i = 0; i < SIZE; i++)
    for (j = 0; j < SIZE; j++)
        grid[i][j] = 0;
/* introduce a few ones */
    grid[1][2] = 1;
    grid[2][2] = 1;
    grid[1][4] = 1;
    grid[2][4] = 1;
    grid[3][2] = 1;
    grid[3][3] = 1;
    grid[3][4] = 1;
    grid[5][3] = 1;
    grid[6][2] = 1;
for (i = 0; i < SIZE; i++)
   for (j = 0; j < SIZE; j++) {
      n = neighbors(i,j);
      printf ("Number of neighbors to element %d,%d =%d\n",i,j,n);
}
 return;
}
/* function to compute the neighbors */
int neighbors (int i, int j)

我认为如果i = 0或i = 6,以及j = 0或j = 6,我可以使用if语句来改变代码的运行方式,但我不确定如何继续。任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

可以在抽象层中包含对数组的直接访问。该层可以隐藏访问逻辑和其他检查。 类似的东西:

int neighbors (int i, int j)
{
    return get_grid(i-1, j-1) + get_grind(i-1, j) + ...
}

调用函数:

track by $index

答案 1 :(得分:0)

我建议:

public class Demo {
    public static void main ( String [] args ) {
        Point p = new Point2d( 1, 2 );
        Visitor v = new Chebyshev();
        p.accept( v );
        System.out.println( p.getMetric() );
    }
}

interface Visitor {
    public void visit ( Point2d p );
    public void visit ( Point3d p );
}

abstract class Point {
    public abstract void accept ( Visitor v );
    private double metric = -1;
    public double getMetric () {
        return metric;
    }
    public void setMetric ( double metric ) {
        this.metric = metric;
    }
}

class Point2d extends Point {
    public Point2d ( double x, double y ) {
        this.x = x;
        this.y = y;
    }

    public void accept ( Visitor v ) {
        v.visit( this );
    }

    private double x;
    public double getX () { return x; }

    private double y;
    public double getY () { return y; }
}

class Point3d extends Point {
    public Point3d ( double x, double y, double z ) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public void accept ( Visitor v ) {
        v.visit( this );
    }

    private double x;
    public double getX () { return x; }

    private double y;
    public double getY () { return y; }

    private double z;
    public double getZ () { return z; }
}

class Euclid implements Visitor {
    public void visit ( Point2d p ) {
        p.setMetric( Math.sqrt( p.getX()*p.getX() + p.getY()*p.getY() ) );
    }
    public void visit ( Point3d p ) {
        p.setMetric( Math.sqrt( p.getX()*p.getX() + p.getY()*p.getY() + p.getZ()*p.getZ() ) );
    }
}

class Chebyshev implements Visitor {
    public void visit ( Point2d p ) {
        double ax = Math.abs( p.getX() );
        double ay = Math.abs( p.getY() );
        p.setMetric( ax>ay ? ax : ay );
    }
    public void visit ( Point3d p ) {
        double ax = Math.abs( p.getX() );
        double ay = Math.abs( p.getY() );
        double az = Math.abs( p.getZ() );
        double max = ax>ay ? ax : ay;
        if ( max<az ) max = az;
        p.setMetric( max );
    }
}