Skip to content

Instantly share code, notes, and snippets.

@quangnle
Last active June 29, 2017 07:35
Show Gist options
  • Save quangnle/13b03d7dbae19ebe0aae5e4dcab6d4f5 to your computer and use it in GitHub Desktop.
Save quangnle/13b03d7dbae19ebe0aae5e4dcab6d4f5 to your computer and use it in GitHub Desktop.
void Solve(int[,] m, int sCol, int eCol, int sRow, int eRow, int br, int bc)
{
// determine the direction of the tile based on the mising block
var direction = GetDirection(sCol, eCol, sRow, eRow, br, bc);
// solve for size = 1
if ((eCol - sCol == 1) && (eRow - sRow == 1))
{
PlaceOne(m, sCol, eCol, sRow, eRow, direction, cIdx);
cIdx += 1;
}
else
{
// get the half board
var mR = sRow + ((eRow - sRow) >> 1);
var mC = sCol + ((eCol - sCol) >> 1);
// place a tile in the middle of the board
PlaceOne(m, mC, mC+1, mR, mR + 1, direction, cIdx);
cIdx++;
// solving for 4 regions recursively
// top left
if (br >= sRow && br <= mR && bc >= sCol && bc <= mC)
{
Solve(m, sCol, mC, sRow, mR, br, bc);
}
else
{
Solve(m, sCol, mC, sRow, mR, mR, mC);
}
//top right
if (br >= sRow && br <= mR && bc >= mC + 1 && bc <= eCol)
{
Solve(m, mC + 1, eCol, sRow, mR, br, bc);
}
else
{
Solve(m, mC + 1, eCol, sRow, mR, mR, mC + 1);
}
// bot left
if (br >= mR+1 && br <= eRow && bc >= sCol && bc <= mC)
{
Solve(m, sCol, mC, mR + 1, eRow, br, bc);
}
else
{
Solve(m, sCol, mC, mR+ 1, eRow, mR+1, mC);
}
// bot right
if (br >= mR + 1 && br <= eRow && bc >= mC + 1 && bc <= eCol)
{
Solve(m, mC + 1, eCol, mR + 1, eRow, br, bc);
}
else
{
Solve(m, mC + 1, eCol, mR + 1, eRow, mR+1, mC+1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment