递归与分治之 棋盘覆盖

/*
  时间:2011-10-9
  作者:xiaosi
  题目:棋盘覆盖
*/
#include"stdio.h"
#include<iostream>
using namespace std;

int tile=1;//表示L型骨牌的编号
int Board[4][4]={0};//定义棋盘并初始化棋盘
void ChessBoard(int tr,int tc,int dr,int dc,int size);

int main()
{
	cout<<"请输入特殊盘的下标号:"<<endl;
	int x,y;
	cin>>x>>y;
	ChessBoard(0,0,x,y,4);
	cout<<endl<<endl;
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			cout<<Board[i][j]<<" ";
		}
		cout<<endl;
	}
	return 0;
}

void ChessBoard(int tr,int tc,int dr,int dc,int size)
{//采用分治策略的棋盘算法
	if(size==1)
		return;
	int t=tile++,//L型骨牌号
		s=size/2;//分割棋盘

    //覆盖左上角子棋盘
	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);
	}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    原文作者:递归与分治算法
    原文地址: https://blog.csdn.net/SJF0115/article/details/7008181
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞