Multidimensional arrays

Multidimensional arrays

Representing a graph using an adjacency matrix

Nodes reachable in 1 hop from the node 0:

#include <iostream>
using namespace std;

const int N = 6;

int main() {
  int adjacent [N][N] = { 
    { 0, 1, 1, 1, 0, 0 },
    { 1, 0, 1, 0, 0, 1 },
    { 1, 1, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1 },
    { 0, 1, 1, 0, 1, 0 },
  };

  cout << "Neighbors of the node 0:" << endl;
  
  for (int i = 0; i < N; i++) {
    if (adjacent[0][i] == 1) {
      cout << i << ' ';
    }
  }

  cout << endl;
}
Neighbors of the node 0:
1 2 3 

Nodes reachable in 2 hops:

#include <iostream>
using namespace std;

const int N = 6;

int main() {
  int adjacent [N][N] = { 
    { 0, 1, 1, 1, 0, 0 },
    { 1, 0, 1, 0, 0, 1 },
    { 1, 1, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1 },
    { 0, 1, 1, 0, 1, 0 },
  };

  cout << "Nodes reachable from the node 0 in at most 2 hops:" << endl;
 
  bool reachable[N] = {false};

  for (int i = 0; i < N; i++) {
    if (adjacent[0][i] == 1) {
      reachable[i] = true;
      for(int j = 0; j < N; j++) {
        if (adjacent[i][j] == 1) {
          reachable[j] = true;
        }
      }
    }
  }

  for(int i = 0; i < N; i++) {
    if (reachable[i]) 
      cout << i << ' ';
  }

  cout << endl;
}
Nodes reachable from the node 0 in at most 2 hops:
0 1 2 3 5 

Packing square boxes

#include <iostream>

using namespace std;

const int ROWS = 12;
const int COLS = 12;

const int BOXES = 4;

// returns true if a box of size size can fit at the position (r,c)
bool fits(char room[ROWS][COLS], int r, int c, int size);

// put the box at the position (r,c) 
// (a square of size size is marked with the label character in the array room)
void put(char room[ROWS][COLS], int r, int c, int size, char label);

int main () {
  int size[BOXES]   = {4, 7, 5, 2};
  char label[BOXES] = {'A', 'B', 'C', 'D'};

  char room[ROWS][COLS];
  // Make the room empty (fill the array with '.' character)
  for(int i = 0; i < ROWS; i++) {
    for(int j = 0; j < COLS; j++) {
      room[i][j] = '.';
    }
  }

  // The main box packing procedure
  int count = 0;
  for(int k = 0; k < BOXES; k++) {
    bool success = false;
    // find where it fits
    for(int i = 0; i < ROWS; i++) {
      for(int j = 0; j < COLS; j++) {
        // check if a box of size size[k] fits at (i,j)
        if (fits(room, i, j, size[k])) {
          // put it there, if yes
          // otherwise keep searching
          put(room, i, j, size[k], label[k]);
          success = true;
          count ++;
        }
        if (success) break;
      }
      if (success) break;
    }
  }

  // Report how many boxes we could pack
  cout << endl << count << " boxes out of " << BOXES << ":" << endl << endl;

  // Print the result
  for(int i = 0; i < ROWS; i++) {
    for(int j = 0; j < COLS; j++) {
      cout << room[i][j];
    }
    cout << endl;
  }

  return 0;
}

bool fits(char room[ROWS][COLS], int r, int c, int size) {
  for(int i = r; i < r + size; i++) {
    for(int j = c; j < c + size; j++) {
      if (i >= ROWS || j >= COLS || room[i][j] != '.')
        return false;
    }
  }
  return true;
}

void put(char room[ROWS][COLS], int r, int c, int size, char label) {
  for(int i = r; i < r + size; i++) {
    for(int j = c; j < c + size; j++) {
      room[i][j] = label; 
    }
  }
}



4 boxes out of 4:

AAAABBBBBBB.
AAAABBBBBBB.
AAAABBBBBBB.
AAAABBBBBBB.
DD..BBBBBBB.
DD..BBBBBBB.
....BBBBBBB.
CCCCC.......
CCCCC.......
CCCCC.......
CCCCC.......
CCCCC.......