棋盘算法 分而治之
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ChessBoard
{
class Program
{
public static int[,] Board = new int[8, 8];
public static int tail = 0;
static void Main(string[] args)
{
chessBoard(0, 0, 4, 7, 8);
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Console.Write("{0,5}",Board[i,j]);
}
Console.Write("\n");
}
Console.Read();
}
public static void chessBoard(int tr,int tc,int dr,int dc,int size)
{
if (size == 1) return;
int s = size / 2;
int t = tail++;
//特殊方块在左上角
if (dr < tr + s && dc < tc + s)
{
chessBoard(tr, tc, dr, dc, s);
}
else
{
//特殊方块不在左上角
Board[tr + s - 1,tc + s - 1] = t;
chessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
}
//特殊方块在右上角
if (dr < tr + s && dc >= tc + s)
{
chessBoard(tr, tc + s, dr, dc, s);
}
else
{
//特殊方块不在右上角
Board[tr+s-1,tc+s] = t;
chessBoard(tr, tc + s, tr + s - 1, tc + s, s);
}
//特殊方块在左下角
if (dr >= tr + s && dc < tc + s)
{
chessBoard(tr + s, tc, dr, dc, s);
}
else
{
//特殊方块不在左下角
Board[tr + s,tc + s - 1] = t;
chessBoard(tr + s, tc, tr + s, tc + s - 1, s);
}
//特殊方块在右下角
if (dr >= tr + s && dc >= tc + s)
{
chessBoard(tr + s, tc + s, dr, dc, s);
}
else
{
//特殊方块不在右下角
Board[tr + s,tc + s] = t;
chessBoard(tr + s, tc + s, tr + s, tc + s, s);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ChessBoard
{
class Program
{
public static int[,] Board = new int[8, 8];
public static int tail = 0;
static void Main(string[] args)
{
chessBoard(0, 0, 4, 7, 8);
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Console.Write("{0,5}",Board[i,j]);
}
Console.Write("\n");
}
Console.Read();
}
public static void chessBoard(int tr,int tc,int dr,int dc,int size)
{
if (size == 1) return;
int s = size / 2;
int t = tail++;
//特殊方块在左上角
if (dr < tr + s && dc < tc + s)
{
chessBoard(tr, tc, dr, dc, s);
}
else
{
//特殊方块不在左上角
Board[tr + s - 1,tc + s - 1] = t;
chessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
}
//特殊方块在右上角
if (dr < tr + s && dc >= tc + s)
{
chessBoard(tr, tc + s, dr, dc, s);
}
else
{
//特殊方块不在右上角
Board[tr+s-1,tc+s] = t;
chessBoard(tr, tc + s, tr + s - 1, tc + s, s);
}
//特殊方块在左下角
if (dr >= tr + s && dc < tc + s)
{
chessBoard(tr + s, tc, dr, dc, s);
}
else
{
//特殊方块不在左下角
Board[tr + s,tc + s - 1] = t;
chessBoard(tr + s, tc, tr + s, tc + s - 1, s);
}
//特殊方块在右下角
if (dr >= tr + s && dc >= tc + s)
{
chessBoard(tr + s, tc + s, dr, dc, s);
}
else
{
//特殊方块不在右下角
Board[tr + s,tc + s] = t;
chessBoard(tr + s, tc + s, tr + s, tc + s, s);
}
}
}
}