void ChessBoard(int tr, int tc, int dr, int dc, int size)
{
if (size <= 1)
{
return;
}
int s = size / 2;
if (dr < tr + s && dc < tc + s)
{
//在左上角
board[tr + s - 1][tc + s] = 4;
board[tr + s][tc + s - 1] = 4;
board[tr + s][tc + s] = 4;
ChessBoard(tr, tc, dr, dc, s);
}
else
{
ChessBoard(tr, tc, tr + s - 1, tc + s -1, s);
}
if (dr < tr + s && dc >= tc + s)
{
//在右上角
board[tr + s - 1][tc + s -1] = 3;
board[tr + s][tc + s - 1] = 3;
board[tr + s][tc + s] = 3;
ChessBoard(tr, tc+s, dr, dc, s);
}
else
{
ChessBoard(tr, tc + s, tr + s - 1, tc + s, s);
}
if (dr >= tr + s && dc < tc + s)
{
//在左下角
board[tr + s - 1][tc + s - 1] = 2;
board[tr + s - 1][tc + s] = 2;
board[tr + s][tc + s] = 2;
ChessBoard(tr+s, tc, dr, dc, s);
}
else
{
ChessBoard(tr+s, tc, tr + s, tc + s - 1, s);
}
if (dr >= tr + s && dc >= tc + s)
{
//在右下角
board[tr + s - 1][tc + s - 1] = 1;
board[tr + s - 1][tc + s] = 1;
board[tr + s][tc + s - 1] = 1;
ChessBoard(tr+s, tc+s, dr, dc, s);
}
else
{
ChessBoard(tr+s, tc+s, tr + s , tc + s, s);
}
}
代码原创,图片转载