迭代法---棋盘覆盖问题

一个用L形小棋子完成的棋盘覆盖问题。

我看大部分帖子写的都是分治法,那我就写个迭代法解决的吧


#include <iostream>
#include <vector>
#define N 4

using namespace std;

const int siz = 1 << N;
int x_pos = 5;
int y_pos = 5;
int directQueue[N] = { 0 };
int axis_x_right = siz;
int axis_y_down = siz;
int axis_x_left = 0;
int axis_y_up = 0;
int axis_x_centre = 0;
int axis_y_centre = 0;

//0:left&up,1:right&up,2:left&down,3:right&down
//-------------->x+
//|
//|
//|
//v  y+

void getRoadMap(int &i, int &x, int &y)
{
	if (i == 0)
	{
		cout << "Direct(0:左上,1:右上,2:左下,3:右下):";
		for (int i = 0; i < N; i++)
		{
			cout << directQueue[i] << " ";
		}
		cout << endl;
	}
	else
	{
		i--;
		axis_x_centre = (axis_x_right + axis_x_left) / 2;
		axis_y_centre = (axis_y_up + axis_y_down) / 2;
		directQueue[i] = (x < axis_x_centre) + 2 * (y < axis_y_centre);
		if (x < axis_x_centre)
			axis_x_right = axis_x_centre;
		else
			axis_x_left = axis_x_centre;
		if (y < axis_y_centre)
			axis_y_down = axis_y_centre;
		else
			axis_y_up = axis_y_centre;

		getRoadMap(i, x, y);
	}
}

vector<vector<int> > rotate90(vector<vector<int> >mat)
{
	vector<vector<int> > matRotate(mat.size(), vector<int>(mat.size()));

	for (int i = 0; i < mat.size(); i++)
	{
		for (int j = 0; j < mat.size(); j++)
		{
			matRotate[j][mat.size() - 1 - i] = mat[i][j];
		}
	}
	return matRotate;
}


vector<vector<int> > generateComponent(vector<vector<int> >inComponent)
{
	int sideLong = inComponent.size() * 2;
	vector<vector<int> > outComponent(sideLong, vector<int>(sideLong));
	//make model
	for (int i = 0; i < inComponent.size(); i++)
	{
		for (int j = 0; j < inComponent.size(); j++)
		{
			outComponent[j][i] += inComponent[j][i];
		}
	}
	for (int i = 0; i < inComponent.size(); i++)
	{
		for (int j = 0; j < inComponent.size(); j++)
		{
			outComponent[j + inComponent.size() / 2][i + inComponent.size() / 2] += inComponent[j][i] * 2;
		}
	}

	vector<vector<int> > inComponent90 = rotate90(inComponent);
	for (int i = 0; i < inComponent.size(); i++)
	{
		for (int j = 0; j < inComponent.size(); j++)
		{
			outComponent[j][i + inComponent.size()] += inComponent90[j][i] * 3;
		}
	}

	for (int i = 0; i < inComponent.size(); i++)
	{
		for (int j = 0; j < inComponent.size(); j++)
		{
			outComponent[j + inComponent.size()][i] += inComponent90[i][j] * 4;
		}
	}
	return outComponent;

}


vector<vector<int> > placeL(int times, int direct)
{
	vector<vector<int> > component(2, vector<int>(2));
	vector<vector<int> > newComponent[100];
	vector<vector<int> > result;
	component[0][0] = 1;
	component[0][1] = 1;
	component[1][0] = 1;
	component[1][1] = 0;
	newComponent[0] = generateComponent(component);

	if (times == 1)
		result = component;
	else
	{
		for (int i = 0; i < times; i++)
		{
			newComponent[i + 1] = generateComponent(newComponent[i]);
		}
		result = newComponent[times - 2];
	}
	if (direct == 2)
		result = rotate90(result);
	if (direct == 3)
		result = rotate90(rotate90(result));
	if (direct == 1)
		result = rotate90(rotate90(rotate90(result)));
	return result;
}

vector<vector<int> > getAngularPoint(vector<vector<int> > base)
{//0:LU,1:RU,2:LD,3:RD

	vector<vector<int> > point(4, vector<int>(2));
	for (int i = 0; i < base.size(); i++)
	{
		for (int j = 0; j < base.size(); j++)
		{
			if (base[i][j] != 0)
			{
				point[3][0] = i;
				point[3][1] = j;
			}
		}
	}
	for (int i = base.size() - 1; i >= 0; i--)
	{
		for (int j = 0; j < base.size(); j++)
		{
			if (base[i][j] != 0)
			{
				point[2][0] = i;
				point[2][1] = j;
			}
		}
	}
	for (int i = 0; i < base.size(); i++)
	{
		for (int j = base.size() - 1; j >= 0; j--)
		{
			if (base[i][j] != 0)
			{
				point[1][0] = i;
				point[1][1] = j;
			}
		}
	}
	for (int i = base.size() - 1; i >= 0; i--)
	{
		for (int j = base.size() - 1; j >= 0; j--)
		{
			if (base[i][j] != 0)
			{
				point[0][0] = i;
				point[0][1] = j;
			}
		}
	}
	return point;
}

vector<vector<int> > add(int Direct, vector<vector<int> > base, vector<vector<int> > component)
{
	vector<vector<int> > angularPoint = getAngularPoint(base);
	//cout<<angularPoint[0][0]<<","<<angularPoint[0][1]<<endl<<angularPoint[1][0]<<","<<angularPoint[1][1]<<endl<<angularPoint[2][0]<<","<<angularPoint[2][1]<<endl<<angularPoint[3][0]<<","<<angularPoint[3][1]<<endl;
	if (Direct == 0)
	{
		for (int i = 0; i < component.size(); i++)
		{
			for (int j = 0; j < component.size(); j++)
			{
				base[1 + angularPoint[3][0] - component.size() + i][1 + angularPoint[3][0] - component.size() + j] += component[i][j];
			}
		}

	}
	if (Direct == 1)
	{
		for (int i = 0; i < component.size(); i++)
		{
			for (int j = 0; j < component.size(); j++)
			{
				base[angularPoint[2][0] + i][angularPoint[2][1] + 1 - component.size() + j] += component[i][j];
			}
		}

	}
	if (Direct == 2)
	{
		for (int i = 0; i < component.size(); i++)
		{
			for (int j = 0; j < component.size(); j++)
			{
				base[angularPoint[1][0] + 1 - component.size() + i][angularPoint[1][1] + j] += component[i][j];
			}
		}

	}
	if (Direct == 3)
	{
		for (int i = 0; i < component.size(); i++)
		{
			for (int j = 0; j < component.size(); j++)
			{
				base[angularPoint[0][0] + i][angularPoint[0][0] + j] += component[i][j];
			}
		}

	}
	return base;
}


void drawChessBoard(int x, int y, int Siz)
{
	vector<vector<int> > chessBoard(Siz, vector<int>(Siz));
	vector<vector<int> > temp;
	for (int i = 0; i < Siz; i++)
	for (int j = 0; j < Siz; j++)
		chessBoard[j][i] = 0;
	chessBoard[x][y] = 5555;


	//fill board
	for (int i = 1; i < N + 1; i++)
	{
		int direct = directQueue[i - 1];
		temp = placeL(i, direct);
		for (int i = 0; i < temp.size(); i++)
		{
			/*for(int j=0;j<temp.size();j++)
			{
			cout<<temp[j][i]<<"\t";
			}
			cout<<endl;*/
		}
		chessBoard = add(direct, chessBoard, temp);
	}
	//print
	for (int i = 0; i < Siz; i++)
	{
		for (int j = 0; j < Siz; j++)
		{
			cout << chessBoard[j][i] << "\t";
		}
		cout << endl;
	}
}

int main()
{
	int i = N;
	getRoadMap(i, x_pos, y_pos);
	drawChessBoard(x_pos, y_pos, siz);
	getchar();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值