简体   繁体   中英

nxn matrix find cells that are adjacent to each other

So there is an nxn matrix and its cells are numbered from 1 to n^2. let's take a 4x4 for convenience.

It's cells will be

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Now i need to find adjacent cells... for 1, it will be 2 and 5 ie horizontally and vertically connected and for 6 it will be 2, 5, 7, 10

So adjacency matrix here will be a 16x16 and the row for 1 will be 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0

I have written the code for it below but I am getting extra 1s in some of the rows...it appears that the if conditions are getting messed up in some places. There are 8 different types because of converting the matrix into a size that is its squared.

#include<stdio.h>
int main()
{
int n;
printf("Enter order of matrix:\n");
scanf("%d", &n);
int arr[n][n];

int i, j;
for (i=0;i<n;i++)
    for (j=0;j<n;j++)
        arr[i][j]=0;    


int size=n*n;

int array[size][size];
int index[n][n];

for (i=0;i<size;i++)
{
    for (j=0;j<size;j++)
    {
        array[i][j]=0;
    }
}

//Getting adjacency details of maze or in this case array. The new array will be n2xn2 in size


int adjIndex;
for (i=0;i<n;i++)
{
    for (j=0;j<n;j++)
    {

        adjIndex=((i+1)*n-(n-(j+1)));
        index[i][j]=adjIndex;
    }
}

for (i=0;i<n;i++)
{
    for (j=0;j<n;j++)
    {

        printf(" %d ", index[i][j]);
    }
    printf("\n");
}




for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{

    if (i==0 && j==0)
    {

        array[index[i][j]-1][index[i][j]]=1;
        array[index[i][j]-1][index[i][j]+n-1]=1;

    }

    if (i==0 && j>0 && j<n-1)
    {           
        array[index[i][j]-1][index[i][j]]=1;
        array[index[i][j]-1][index[i][j]-2]=1;
        array[index[i][j]-1][index[i][j]+n-1]=1;    

    }

    if (i==0 && j==n-1)
    {
        array[index[i][j]-1][index[i][j]-2]=1;
        array[index[i][j]-1][index[i][j]+n-1]=1;


    }

    if (j==0 && i > 0 && i < n-1)
    {   
        array[index[i][j]-1][index[i][j]]=1;
        array[index[i][j]-1][index[i][j]+n-1]=1;
        array[index[i][j]-1][index[i][j]-n-1]=1;

    }


    if (j==0 && i==n-1)
    {
        array[index[i][j]-1][index[i][j]]=1;
        array[index[i][j]-1][index[i][j]-n-1]=1;

    }

    if (i==n-1 && j>0 && j<n-1)
    {
        array[index[i][j]-1][index[i][j]]=1;
        array[index[i][j]-1][index[i][j]-2]=1;
        array[index[i][j]-1][index[i][j]-n-1]=1;


    }

    if (i==n-1 && j==n-1)
    {
        array[index[i][j]-1][index[i][j]-2]=1;
        array[index[i][j]-1][index[i][j]-n-1]=1;


    }

    if (j==n-1 && i>0 && i<n-1)
    {
        array[index[i][j]-1][index[i][j]-2]=1;
        array[index[i][j]-1][index[i][j]+n-1]=1;
        array[index[i][j]-1][index[i][j]-n-1]=1;

    }


    else
    {
        array[index[i][j]-1][index[i][j]-n-1]=1;
        array[index[i][j]-1][index[i][j]-2]=1;
        array[index[i][j]-1][index[i][j]]=1;
        array[index[i][j]-1][index[i][j]+n-1]=1;

    }

}
}

for (i=0;i<size;i++)
{
for (j=0;j<size;j++)
    {
        printf(" %d ", array[i][j]);
    }
printf("\n");
}       




}

I am sure it is soemthing trivial. This is a smaller part of a way to make an nxn array with some cells blocked and finding the path between two cells. I have the code working for that part but this one, the simpler one is proving troublesome :|

Alrite guys. It was something very trivial. In the set of if conditions, it kept entering the else one at the last and therefore extra unnecessary cells were being set to 1. I fixed it by using a variable that is set to 1 after the assignments and if it is not set to 1, then the else is executed. It is fine now. Thanks :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM