编程之美4.2 扩展
来源:互联网 发布:电脑必备软件 知乎 编辑:程序博客网 时间:2024/05/23 01:34
扩展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;
}
- 编程之美4.2 扩展
- 编程之美4.2扩展问题
- 编程之美3.6 扩展
- 编程之美4.6扩展
- 编程之美3.1 扩展
- 编程之美----扩展问题
- 编程之美 4.2 瓷砖覆盖地板 扩展问题
- 编程之美之扩展问题
- 编程之美:水王扩展题
- 编程之美4.10扩展题
- 编程之美3.11扩展问题
- 编程之美3.10扩展问题
- 编程之美3.8扩展问题
- 编程之美4.1扩展问题
- 编程之美第1题-扩展
- 编程之美3.4扩展问题
- 编程之美3.10扩展问题
- 《编程之美》3.4扩展问题实现
- OpenProcess,TermintaeProcess,ExitProcess
- 在Java代码中使用ZipFile解压zip文件
- RCP开发发布产品图片不显示,解决方法
- 函数指针和指针函数
- Effective C++之实现
- 编程之美4.2 扩展
- 增强现实现在越来越贴近现实
- sql2005的存储过程
- QNX驱动开发——Resource manger framework
- 不同服务器数据库之间的数据操作
- POJ 1426 Find The Multiple bfs
- 谈谈mobile web以及浏览器的单/多进程
- Delphi GUI程序中使用控制台
- HDU-4107-Gangster