I have to fill in a 2^n by 2^n board that has a hole in it with tiles that look like the example below. The tile we have to use is the combination of the 1's in the image below and the 0 is the hole (cause I really don't know how to explain it).
2x2 board
I've got the whole code done, but it only works for boards of 2x2, 4x4, 8x8. Anything bigger and it doesn't fill it in completely. I've tried a bunch of things and can't fix it.
Anyone see where I'm messing something up or any suggestions on how I should change my program so it's cleaner.
Below are the printout for the 4x4 and 8x8 so you can see how it's suppose to look like.
4x4 board, 8x8 board
And here's the code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct hole
{
int row;
int col;
};
void initialize (int, int**, hole);
void decor(int);
void print(int, int**);
int fill (int, int**, hole, int, int, int, int, int, int, int&);
void check (int**, int, int, int);
void norm (int&, int&, int&, int&);
void state (int&, int, int, int);
main ()
{ int m = 3, row=1, col=1, start = 1;
hole space;
int n = (1 << m);
int** array = new int*[n];
for (int i = 0; i < n; i++)
{ array[i] = new int[n]; }
space.row = row-1;
space.col = col-1;
initialize (n, array, space);
fill (n, array, space, 0, 0, 0, 0, 0, 0, start, start);
print (n, array);
//free the array space
for(int i = 0; i < n; i++)
{ delete array[i]; }
delete array;
system ("pause");
return 0;
}
void initialize (int n, int** array, hole a)
{ for (int x = 0; x < n; ++x)
{ for (int y = 0; y < n; ++y)
{ array[x][y] = 99; }
}
array[a.row][a.col] = 0;
}
void norm (int& row, int& col, int& r, int& c)
{ r = row % 2;
c = col % 2;
}
void state (int& h, int c, int p, int b)
{ if (h+c <= b)
{ h += c + p; }
}
int fill (int n, int **a, hole now, int xh, int yh, int xc, int yc, int pxc, int pyc, int& num)
{ static int o = n/2;
if (n==2)
{ int row, col;
state (xh, xc, 0, o);
state (yh, yc, 0, o);
norm (now.row, now.col, row, col);
if (row == 0)
{ if (col == 0) { check (a, xh, yh+1, num); }
if (col == 1) { check (a, xh, yh, num); }
check (a, xh+1, yh, num);
check (a, xh+1, yh+1, num);
}
if (row == 1)
{ if (col == 0) { check (a, xh+1, yh+1, num); }
if (col == 1) { check (a, xh+1, yh, num); }
check (a, xh, yh, num);
check (a, xh, yh+1, num);
}
}
else
{ int half = (n/2);
int x, y;
pxc += xc;
pyc += yc;
xc = xh;
yc = yh;
xh += half;
yh += half;
if (now.row < xh) x = 1;
else x = 2;
if (now.col < yh) y = 1;
else y = 2;
hole q1, q2, q3, q4;
// Space for switch
switch (x+y)
{ case 2:
q1.row = now.row;
q1.col = now.col;
q2.row = (xh-1);
q3.col = (yh-1);
q2.col = q4.col = yh;
q3.row = q4.row = xh;
a[xh-1][yh] = a[xh][yh-1] = a[xh][yh] = num;
break;
case 3:
if (x == 1) // if row 1 col 2
{ q3.col = (yh-1);
q3.row = xh;
q2.row = now.row;
q2.col = now.col;
a[xh][yh-1] = num;
}
else // row 2 col 1
{ q2.row = (xh-1);
q2.col = yh;
q3.row = now.row;
q3.col = now.col;
a[xh-1][yh] = num;
}
q1.row = (xh-1);
q1.col = (yh-1);
q4.row = xh;
q4.col = yh;
a[xh][yh] = a[xh-1][yh-1] = num;
break;
case 4:
q4.row = now.row;
q4.col = now.col;
q1.row = q2.row = (xh-1);
q3.row = xh;
q1.col = q3.col = (yh-1);
q2.col = yh;
a[xh-1][yh-1] = a[xh-1][yh] = a[xh][yh-1] = num;
break;
} // End Switch
fill(half, a, q1, 0, 0, xc, yc, pxc, pyc, ++num);
fill(half, a, q2, 0, yh, xc, yc, pxc, pyc, ++num);
fill(half, a, q3, xh, 0, xc, yc, pxc, pyc, ++num);
fill(half, a, q4, xh, yh, xc, yc, pxc, pyc, ++num);
}
}
void decor(int n)
{ cout << "\n-";
for (int i = 0; i < n; i++)
{ cout << "-----"; }
cout << endl;
}
void print(int n , int **ar)
{ decor(n);
for (int x = 0; x < n; x++)
{ for (int y = 0; y < n; y++)
{ cout << "|" << setw(3) << ar[x][y] << " "; }
cout << "|";
decor(n);
}
}
void check (int **ar, int x, int y, int num)
{ if (ar[x][y] == 99)
{ ar[x][y] = num; }
}