编程之美4.2 扩展

 


扩展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;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值