扩展1:
http://blog.csdn.net/houhouzhe/article/details/6566587
// Test.cpp : Defines the entry point for the console application.
// 一下是1X2 瓷砖覆盖 8X8的穷举算法 相比于以上地址 中穷举算法 时间久一些,主要是里面的for循环 造成,但思路是一样的
#include “stdafx.h”
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<assert.h>
#include<vector>
#include<fstream>
#include<queue>
using namespace std;
time_t t1,t2;
void Tic()
{
t1=time(NULL);
// for(int i=0;i<INT_MAX;++i);
}
void Toc()
{
t2=time(NULL);
std::cout<<“time:”<<(t2-t1)/3600<<“时:”<<(t2-t1)%3600/60<<“分:”<<(t2-t1)%60<<“秒”<<std::endl;
}
int RandInt(int low,int high)
{
float temp=rand()/(static_cast<float>(RAND_MAX));
int k=low+static_cast<int>((high-low)*temp);
return k;
}
template<class T>
void CreateTwoNdims(T**&P,int row,int col)
{
P=new T*[row];
for(int i=0;i<row;++i)
P[i]=new T[col];
}
template<class T>
void Init(T initial,int row,int col,T**&P)
{
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
P[i][j]=initial;
}
template<class T>
void DestroyTwoNdims(T**&P,int row,int col)
{
for(int i=0;i<row;++i)
delete[] P[i];
delete[]P;
P=NULL;
}
#define MAX_ROW 8
#define MAX_COL 8
unsigned int countNum;
ofstream out;
void CoverPlan(int **&land)
{
/*穷举法计算瓷砖覆盖次数*/
/* 1 竖放 0 横放*/
int count=0;
for(int i=0;i<MAX_ROW;++i)
{
for(int j=0;j<MAX_COL;++j)
{
if(land[i][j]<0)
{
#pragma region
if(j==MAX_COL-1)//最后一列
{
if(i<MAX_ROW-1&&land[i+1][j]<0)
{
land[i][j]=1;
land[i+1][j]=1;
CoverPlan(land);
land[i][j]=-1;
land[i+1][j]=-1;
}
else
return;//错误
}
#pragma endregion
#pragma region
else if(i==MAX_ROW-1)//最后一行
{
if(j<MAX_COL-1&&land[i][j+1]<0)
{
land[i][j]=0;
land[i][j+1]=0;
CoverPlan(land);
land[i][j]=-1;
land[i][j+1]=-1;
}
else
return;//错误
}
#pragma endregion
#pragma region
else
{
if(land[i][j+1]<0)
{
land[i][j+1]=0;
land[i][j]=0;
CoverPlan(land);
land[i][j]=-1;
land[i][j+1]=-1;
}
if(land[i+1][j]<0)
{
land[i][j]=1;
land[i+1][j]=1;
CoverPlan(land);
land[i][j]=-1;
land[i+1][j]=-1;
}
}
#pragma endregion
return;//正确
}
else
{
count++;
}
}
}
if(count==MAX_ROW*MAX_COL)
{
countNum++;
out<<countNum<<std::endl;
}
}
void RunAlgorithm()
{
int **land;
int row,col;
row=col=8;
CreateTwoNdims<int>(land,row,col);
Init<int>(-1,row,col,land);
Tic();
out.open(“out.txt”,std::ios::out);
CoverPlan(land);
out.close();
Toc();
std::cout<<countNum<<std::endl;
DestroyTwoNdims<int>(land,row,col);
}
int _tmain(int argc, _TCHAR* argv[])
{
RunAlgorithm();
system(“pause”);
return 0;
}