棋盘覆盖问题

/* 标准文档模板 */

#include "Stdio.h"
#include "Conio.h"
#define N 16

void Chess(int tr,int tc,int dr,int dc,int size);
void Show();


int board[N+1][N+1];
int main()
{
 /* 此处添加你自己的代码 */
  board[1][1]=-1;
  Chess(1,1,1,1,N);
  Show();


  getch();
  return 0;
}

void Chess(int tr,int tc,int dr,int dc,int size)
{
  int s=size/2;
  static ti=1;
  int t=ti++;
  if(size==1) return ;

  if(dr    Chess(tr,tc,dr,dc,s);
  else
  {
    board[tr+s-1][tc+s-1]=t;
    Chess(tr,tc,tr+s-1,tc+s-1,s);

  }

  if(dr=tc+s)
    Chess(tr,tc+s,dr,dc,s);
  else
  {
    board[tr+s-1][tc+s]=t;
    Chess(tr,tc+s,tr+s-1,tc+s,s);
  }

  if(dr>=tr+s && dc    Chess(tr+s,tc,dr,dc,s);
  else
  {
    board[tr+s][tc+s-1]=t;
    Chess(tr+s,tc,tr+s,tc+s-1,s);

  }

  if(dr>=tr+s && dc>=tc+s)
    Chess(tr+s,tc+s,dr,dc,s);
  else
  {
    board[tr+s][tc+s]=t;
    Chess(tr+s,tc+s,tr+s,tc+s,s);

  }

}

void Show()
{
  int i,j;
  for(i=1;i<=N;i++)
  {  for(j=1;j<=N;j++)
      printf("%3d ",board[i][j]);

      printf("\n");
} }

Leave a Reply

Your email address will not be published. Required fields are marked *