#include <stdio.h>
void neighbour(int sx, int sy);
void diagnolNeighbour(int sx, int sy);
void allNeighbour(int sx, int sy);
void display();
int array[4][4];
int row = 3;
int col = 3;
int startRow = 0;
int startCol = 0;
int main()
{
int k = 0;
int i = 0;
int j = 0;
for (i = 0; i <= row; i++) {
for (j = 0; j <= col; j++) {
++k;
array[i][j] = k;
if (k < 10) {
} else {
}
}
}
for (i = 0; i <= row; i++) {
for (j = 0; j <= col; j++) {
printf("\nNeighbour for (%d,%d) %d: ",i
,j
, array
[i
][j
]); neighbour(i,j);
}
}
display();
printf("Neighbour with Diagnols\n"); for (i = 0; i <= row; i++) {
for (j = 0; j <= col; j++) {
printf("\nNeighbour for (%d,%d) %d: ",i
,j
, array
[i
][j
]); diagnolNeighbour(i,j);
}
}
display();
printf("Neighbour with all Diagnols\n"); for (i = 0; i <= row; i++) {
for (j = 0; j <= col; j++) {
printf("\nNeighbour for (%d,%d) %d: ",i
,j
, array
[i
][j
]); allNeighbour(i,j);
}
}
return 0;
}
void display()
{
int k = 0;
int i = 0;
int j = 0;
for (i = 0; i <= row; i++) {
for (j = 0; j <= col; j++) {
++k;
if (k < 10) {
} else {
}
}
}
}
/******************************************************
* Function: neighbour(int sx, int sy)
* This function returns the neighbour of sx, sy
* For e.g.
* 1 2 3
* 4 5 6
* 7 8 9
*
* Neighbour for 5 (1,1) = 4, 2, 6, 8
* Neighbour for 1 (0,0) = 4, 2
* ****************************************************/
void neighbour(int sx, int sy)
{
if (sy - 1 >= startCol && sy - 1 <= col) {
printf("\n[%d][%d]: %d ", sx
, sy
- 1, array
[sx
][sy
- 1]); }
if (sx - 1 >= startRow && sx - 1 <= row) {
printf("\n[%d][%d]: %d ", sx
- 1, sy
, array
[sx
- 1][sy
]); }
if (sy + 1 >= startCol && sy + 1 <= col) {
printf("\n[%d][%d]: %d ", sx
, sy
+ 1, array
[sx
][sy
+ 1]); }
if (sx + 1 >= startRow && sx + 1 <= row) {
printf("\n[%d][%d]: %d ", sx
+ 1, sy
,array
[sx
+ 1][sy
]); }
}
/******************************************************
* Function: diagnolNeighbour(int sx, int sy)
* This function returns the neighbour of sx, sy and
* diagonal neighbours too.
* For e.g.
* 1 2 3
* 4 5 6
* 7 8 9
*
* Neighbour for 5 (1,1) = 4, 2, 6, 8, 1, 3, 7, 9
* Neighbour for 1 (0,0) = 4, 2, 5
* ****************************************************/
void diagnolNeighbour(int sx, int sy) {
if (sy - 1 >= startCol && sy - 1 <= col) {
printf("%d ", array
[sx
][sy
- 1]); }
if (sx - 1 >= startRow && sx - 1 <= row) {
printf("%d ", array
[sx
- 1][sy
]);
//Diagnols
if (sy - 1 >= startCol && sy - 1 <= col) {
printf("%d ", array
[sx
- 1][sy
- 1]); }
if (sy + 1 >= startCol && sy + 1 <= col) {
printf("%d ", array
[sx
- 1][sy
+ 1]); }
}
if (sy + 1 >= startCol && sy + 1 <= col) {
printf("%d ", array
[sx
][sy
+ 1]); }
if (sx + 1 >= startRow && sx + 1 <= row) {
printf("%d ", array
[sx
+ 1][sy
]);
//Diagnols
if (sy - 1 >= startCol && sy - 1 <= col) {
printf("%d ", array
[sx
+ 1][sy
- 1]); }
if (sy + 1 >= startCol && sy + 1 <= col) {
printf("%d ", array
[sx
+ 1][sy
+ 1]); }
}
}
/******************************************************
* Function: allNeighbour(int sx, int sy)
* This function returns all the neighbour of sx, sy and
* all diagonal neighbours.
* For e.g.
* 1 2 3 4
* 5 6 7 8
* 9 10 11 12
* 13 14 15 16
*
* Neighbour for 5 (1,0) = 1, 9, 13, 6, 7, 8, 10, 15
* Neighbour for 11 (2,2) = 3, 7, 15, 9, 10, 12, 1, 6, 16
* ****************************************************/
void allNeighbour(int sx, int sy)
{
int done = 0;
int tempX = sx;
int tempY = sy;
int i = 0;
int j = 0;
// Get all the elements in row
for (i = 0; i <= row; i++) {
if (array[i][sy] == array[sx][sy]) {
continue;
}
}
// Get all the elements in col
for (j = 0; j <= col; j++) {
if (array[sx][j] == array[sx][sy]) {
continue;
}
}
// Diagnols
while (1) {
tempX--; tempY--;
if (tempX >= startRow && tempY >= startCol) {
printf("%d ", array
[tempX
][tempY
]); } else {
done = 1;
}
sx++,sy++;
if (sx <= row && sy <= col) {
} else if (done) {
break;
}
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp2b2lkIG5laWdoYm91cihpbnQgc3gsIGludCBzeSk7CnZvaWQgZGlhZ25vbE5laWdoYm91cihpbnQgc3gsIGludCBzeSk7CnZvaWQgYWxsTmVpZ2hib3VyKGludCBzeCwgaW50IHN5KTsKdm9pZCBkaXNwbGF5KCk7CgppbnQgYXJyYXlbNF1bNF07CmludCByb3cgPSAzOwppbnQgY29sID0gMzsKaW50IHN0YXJ0Um93ID0gMDsKaW50IHN0YXJ0Q29sID0gMDsKCmludCBtYWluKCkKewogICAgaW50IGsgPSAwOwogICAgaW50IGkgPSAwOwogICAgaW50IGogPSAwOwogICAgCiAgICBmb3IgKGkgPSAwOyBpIDw9IHJvdzsgaSsrKSB7CgkJZm9yIChqID0gMDsgaiA8PSBjb2w7IGorKykgewoJCSAgICArK2s7CgkJICAgIGFycmF5W2ldW2pdID0gazsKCQkgICAgaWYgKGsgPCAxMCkgewoJCQkJcHJpbnRmKCIlZCAgICIsIGFycmF5W2ldW2pdKTsKCQkgICAgfSBlbHNlIHsKCQkJCXByaW50ZigiJWQgICIsIGFycmF5W2ldW2pdKTsKCQkgICAgfQoJCX0KCQlwcmludGYoIlxuIik7CiAgICB9CiAgICBwcmludGYoIlxuXG4iKTsKCiAgICBmb3IgKGkgPSAwOyBpIDw9IHJvdzsgaSsrKSB7CgkJZm9yIChqID0gMDsgaiA8PSBjb2w7IGorKykgewoJCSAgICBwcmludGYoIlxuTmVpZ2hib3VyIGZvciAoJWQsJWQpICVkOiAiLGksaiwgYXJyYXlbaV1bal0pOwoJCSAgICBuZWlnaGJvdXIoaSxqKTsKCQl9CiAgICB9CgogICAgZGlzcGxheSgpOwogICAgcHJpbnRmKCJOZWlnaGJvdXIgd2l0aCBEaWFnbm9sc1xuIik7CiAgICBmb3IgKGkgPSAwOyBpIDw9IHJvdzsgaSsrKSB7CgkJZm9yIChqID0gMDsgaiA8PSBjb2w7IGorKykgewoJCSAgICBwcmludGYoIlxuTmVpZ2hib3VyIGZvciAoJWQsJWQpICVkOiAiLGksaiwgYXJyYXlbaV1bal0pOwoJCSAgICBkaWFnbm9sTmVpZ2hib3VyKGksaik7CgkJfQogICAgfQoKICAgIGRpc3BsYXkoKTsKICAgIHByaW50ZigiTmVpZ2hib3VyIHdpdGggYWxsIERpYWdub2xzXG4iKTsKICAgIGZvciAoaSA9IDA7IGkgPD0gcm93OyBpKyspIHsKCQlmb3IgKGogPSAwOyBqIDw9IGNvbDsgaisrKSB7CgkJICAgIHByaW50ZigiXG5OZWlnaGJvdXIgZm9yICglZCwlZCkgJWQ6ICIsaSxqLCBhcnJheVtpXVtqXSk7CgkJICAgIGFsbE5laWdoYm91cihpLGopOwoJCX0KICAgIH0KICAgIHByaW50ZigiXG4iKTsKCiAgICByZXR1cm4gMDsKfQoKdm9pZCBkaXNwbGF5KCkKewogICAgaW50IGsgPSAwOwogICAgaW50IGkgPSAwOwogICAgaW50IGogPSAwOwogICAgCiAgICBwcmludGYoIlxuXG4iKTsKICAgIGZvciAoaSA9IDA7IGkgPD0gcm93OyBpKyspIHsKCQlmb3IgKGogPSAwOyBqIDw9IGNvbDsgaisrKSB7CgkJICAgICsrazsKCQkgICAgaWYgKGsgPCAxMCkgewoJCQkJcHJpbnRmKCIlZCAgICIsIGFycmF5W2ldW2pdKTsKCQkgICAgfSBlbHNlIHsKCQkJCXByaW50ZigiJWQgICIsIGFycmF5W2ldW2pdKTsKCQkgICAgfQoJCX0KCXByaW50ZigiXG4iKTsKICAgIH0KfQoKLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgogKiBGdW5jdGlvbjogbmVpZ2hib3VyKGludCBzeCwgaW50IHN5KQogKiBUaGlzIGZ1bmN0aW9uIHJldHVybnMgdGhlIG5laWdoYm91ciBvZiBzeCwgc3kKICogRm9yIGUuZy4KICogMSAyIDMKICogNCA1IDYKICogNyA4IDkKICoKICogTmVpZ2hib3VyIGZvciA1ICgxLDEpID0gNCwgMiwgNiwgOAogKiBOZWlnaGJvdXIgZm9yIDEgKDAsMCkgPSA0LCAyCiAqICoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCnZvaWQgbmVpZ2hib3VyKGludCBzeCwgaW50IHN5KQp7CiAgICBpZiAoc3kgLSAxID49IHN0YXJ0Q29sICYmIHN5IC0gMSA8PSBjb2wpIHsKCQlwcmludGYoIlxuWyVkXVslZF06ICVkICIsIHN4LCBzeSAtIDEsIGFycmF5W3N4XVtzeSAtIDFdKTsKICAgIH0KCiAgICBpZiAoc3ggLSAxID49IHN0YXJ0Um93ICYmIHN4IC0gMSA8PSByb3cpIHsKCQlwcmludGYoIlxuWyVkXVslZF06ICVkICIsIHN4IC0gMSwgc3ksIGFycmF5W3N4IC0gMV1bc3ldKTsKICAgIH0KCiAgICBpZiAoc3kgKyAxID49IHN0YXJ0Q29sICYmIHN5ICsgMSA8PSBjb2wpIHsKCQlwcmludGYoIlxuWyVkXVslZF06ICVkICIsIHN4LCBzeSArIDEsIGFycmF5W3N4XVtzeSArIDFdKTsKICAgIH0KCiAgICBpZiAoc3ggKyAxID49IHN0YXJ0Um93ICYmIHN4ICsgMSA8PSByb3cpIHsKCQlwcmludGYoIlxuWyVkXVslZF06ICVkICIsIHN4ICsgMSwgc3ksYXJyYXlbc3ggKyAxXVtzeV0pOwogICAgfQp9CgovKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqCiAqIEZ1bmN0aW9uOiBkaWFnbm9sTmVpZ2hib3VyKGludCBzeCwgaW50IHN5KQogKiBUaGlzIGZ1bmN0aW9uIHJldHVybnMgdGhlIG5laWdoYm91ciBvZiBzeCwgc3kgYW5kCiAqIGRpYWd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GFydENvbCAmJiBzeSAtIDEgPD0gY29sKSB7CgkJICAgIHByaW50ZigiJWQgIiwgYXJyYXlbc3ggKyAxXVtzeSAtIDFdKTsKCQl9CgkJaWYgKHN5ICsgMSA+PSBzdGFydENvbCAmJiBzeSArIDEgPD0gY29sKSB7CgkJICAgIHByaW50ZigiJWQgIiwgYXJyYXlbc3ggKyAxXVtzeSArIDFdKTsKCQl9CiAgICB9Cn0KCi8qKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogRnVuY3Rpb246IGFsbE5laWdoYm91cihpbnQgc3gsIGludCBzeSkKICogVGhpcyBmdW5jdGlvbiByZXR1cm5zIGFsbCB0aGUgbmVpZ2hib3VyIG9mIHN4LCBzeSBhbmQKICogYWxsIGRpYWdvbmFsIG5laWdoYm91cnMuCiAqIEZvciBlLmcuCiAqIDEgICAyICAgMyAgIDQKICogNSAgIDYgICA3ICAgOAogKiA5ICAgMTAgIDExICAxMgogKiAxMyAgMTQgIDE1ICAxNgogKgogKiBOZWlnaGJvdXIgZm9yIDUgICgxLDApID0gMSwgOSwgMTMsIDYsIDcsIDgsIDEwLCAxNQogKiBOZWlnaGJvdXIgZm9yIDExICgyLDIpID0gMywgNywgMTUsIDksIDEwLCAxMiwgMSwgNiwgMTYKICogKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8Kdm9pZCBhbGxOZWlnaGJvdXIoaW50IHN4LCBpbnQgc3kpCnsKICAgIGludCBkb25lID0gMDsKICAgIGludCB0ZW1wWCA9IHN4OwogICAgaW50IHRlbXBZID0gc3k7CiAgICBpbnQgaSA9IDA7CiAgICBpbnQgaiA9IDA7CgogICAgLy8gR2V0IGFsbCB0aGUgZWxlbWVudHMgaW4gcm93CiAgICBmb3IgKGkgPSAwOyBpIDw9IHJvdzsgaSsrKSB7CgkJaWYgKGFycmF5W2ldW3N5XSA9PSBhcnJheVtzeF1bc3ldKSB7CgkJICAgIGNvbnRpbnVlOwoJCX0KCQlwcmludGYoIiVkICIsIGFycmF5W2ldW3N5XSk7CiAgICB9CgogICAgLy8gR2V0IGFsbCB0aGUgZWxlbWVudHMgaW4gY29sCiAgICBmb3IgKGogPSAwOyBqIDw9IGNvbDsgaisrKSB7CgkJaWYgKGFycmF5W3N4XVtqXSA9PSBhcnJheVtzeF1bc3ldKSB7CgkJICAgIGNvbnRpbnVlOwoJCX0KCQlwcmludGYoIiVkICIsIGFycmF5W3N4XVtqXSk7CiAgICB9CgogICAgLy8gRGlhZ25vbHMKICAgIHdoaWxlICgxKSB7CiAgICAgICAgdGVtcFgtLTsgdGVtcFktLTsKCQlpZiAodGVtcFggPj0gc3RhcnRSb3cgICYmIHRlbXBZID49IHN0YXJ0Q29sKSB7CgkJICAgIHByaW50ZigiJWQgIiwgYXJyYXlbdGVtcFhdW3RlbXBZXSk7CgkJfSBlbHNlIHsKCQkgICAgZG9uZSA9IDE7CgkJfQoKCQlzeCsrLHN5Kys7CgkJaWYgKHN4IDw9IHJvdyAmJiBzeSA8PSBjb2wpIHsKCQkgICAgcHJpbnRmKCIlZCAiLCBhcnJheVtzeF1bc3ldKTsKCQl9IGVsc2UgaWYgKGRvbmUpIHsKCQkgICAgYnJlYWs7CgkJfQogICAgfQp9Cg==
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Neighbour for (0,0) 1:
[0][1]: 2
[1][0]: 5
Neighbour for (0,1) 2:
[0][0]: 1
[0][2]: 3
[1][1]: 6
Neighbour for (0,2) 3:
[0][1]: 2
[0][3]: 4
[1][2]: 7
Neighbour for (0,3) 4:
[0][2]: 3
[1][3]: 8
Neighbour for (1,0) 5:
[0][0]: 1
[1][1]: 6
[2][0]: 9
Neighbour for (1,1) 6:
[1][0]: 5
[0][1]: 2
[1][2]: 7
[2][1]: 10
Neighbour for (1,2) 7:
[1][1]: 6
[0][2]: 3
[1][3]: 8
[2][2]: 11
Neighbour for (1,3) 8:
[1][2]: 7
[0][3]: 4
[2][3]: 12
Neighbour for (2,0) 9:
[1][0]: 5
[2][1]: 10
[3][0]: 13
Neighbour for (2,1) 10:
[2][0]: 9
[1][1]: 6
[2][2]: 11
[3][1]: 14
Neighbour for (2,2) 11:
[2][1]: 10
[1][2]: 7
[2][3]: 12
[3][2]: 15
Neighbour for (2,3) 12:
[2][2]: 11
[1][3]: 8
[3][3]: 16
Neighbour for (3,0) 13:
[2][0]: 9
[3][1]: 14
Neighbour for (3,1) 14:
[3][0]: 13
[2][1]: 10
[3][2]: 15
Neighbour for (3,2) 15:
[3][1]: 14
[2][2]: 11
[3][3]: 16
Neighbour for (3,3) 16:
[3][2]: 15
[2][3]: 12
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Neighbour with Diagnols
Neighbour for (0,0) 1: 2 5 6
Neighbour for (0,1) 2: 1 3 6 5 7
Neighbour for (0,2) 3: 2 4 7 6 8
Neighbour for (0,3) 4: 3 8 7
Neighbour for (1,0) 5: 1 2 6 9 10
Neighbour for (1,1) 6: 5 2 1 3 7 10 9 11
Neighbour for (1,2) 7: 6 3 2 4 8 11 10 12
Neighbour for (1,3) 8: 7 4 3 12 11
Neighbour for (2,0) 9: 5 6 10 13 14
Neighbour for (2,1) 10: 9 6 5 7 11 14 13 15
Neighbour for (2,2) 11: 10 7 6 8 12 15 14 16
Neighbour for (2,3) 12: 11 8 7 16 15
Neighbour for (3,0) 13: 9 10 14
Neighbour for (3,1) 14: 13 10 9 11 15
Neighbour for (3,2) 15: 14 11 10 12 16
Neighbour for (3,3) 16: 15 12 11
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Neighbour with all Diagnols
Neighbour for (0,0) 1: 5 9 13 2 3 4 6 11 16
Neighbour for (0,1) 2: 6 10 14 1 3 4 7 12
Neighbour for (0,2) 3: 7 11 15 1 2 4 8
Neighbour for (0,3) 4: 8 12 16 1 2 3
Neighbour for (1,0) 5: 1 9 13 6 7 8 10 15
Neighbour for (1,1) 6: 2 10 14 5 7 8 1 11 16
Neighbour for (1,2) 7: 3 11 15 5 6 8 2 12
Neighbour for (1,3) 8: 4 12 16 5 6 7 3
Neighbour for (2,0) 9: 1 5 13 10 11 12 14
Neighbour for (2,1) 10: 2 6 14 9 11 12 5 15
Neighbour for (2,2) 11: 3 7 15 9 10 12 6 16 1
Neighbour for (2,3) 12: 4 8 16 9 10 11 7 2
Neighbour for (3,0) 13: 1 5 9 14 15 16
Neighbour for (3,1) 14: 2 6 10 13 15 16 9
Neighbour for (3,2) 15: 3 7 11 13 14 16 10 5
Neighbour for (3,3) 16: 4 8 12 13 14 15 11 6 1