这是代码:
bloque.cpp(砖墙代码)
#include <vector>
#include <iostream>
using namespace std;
class bloque{
private:
vector<char> block;
public:
bloque(int n){
block.resize(n+1);
rellenar();
}
~bloque(void) {}
void rellenar(void){
for(int i=0;i<block.size()-1;i++){
block.at(i)='x';
}
block.at(block.size()-1)='|';
}
int dim(void){
return block.size();
}
char val(int pos){
return block.at(pos);
}
};
muro.cpp(墙代码)//创建墙,看看墙是否正确
#include <vector>
#include <iostream>
#include <cmath>
#include "template_matrix_t.hpp"
#include "bloque.hpp"
using namespace std;
class Muro{
private:
int l;
int h;
matrix_t<char> muro;
int nmax;
int nmin;
public:
//Im sure the problem would be in this part of the code
Muro(int l_, int h_, bloque A, bloque B){
l=l_;
h=h_;
nmax=l/(A.dim()-1);
nmin=l/(B.dim()-1);
int diml = l + 1 + l/(A.dim()-1);
muro.resize(h,diml);
}
~Muro(void) {}
void combinacion(bloque A,bloque B){
int ncomb=0;
construir(A,B,ncomb);
cout << "\Existen" << ncomb << "posibles soluciones";
}
private :
//Make a combination
bool relleno(matrix_t<char>& maux,vector<int>& pos,int p){
int stop=0;
if((maux.at(p,pos[p])==' ')&&(pos[p]<maux.get_n()))
maux.at(p,pos[p])=='A';
else{
int v = pos[p];
while ((v>=0)&&(stop==0)){
if (maux.at(p,v)=='A'){
maux.at(p,v)='B';
stop=1;
}
else{
maux.at(p,v)='A';
v--;
}
}
if((v<0)&&(maux.at(p,maux.get_n()-1=='A'))){
maux.at(p,0)='A';
for(int i=1;i<maux.get_n()-1;i++){
maux.at(p,i)=' ';
}
return true;
}
if((v<0)&&(maux.at(p,pos[p])=='A')){
++pos[p];
maux.at(p,pos[p])='A';
}
}
return false;
}
void escribir(void){
muro.write(cout);
cout<<"\n";
}
//Make the wall and try the solution
void construir(bloque& A,bloque& B, int& ncomb){
int ai = 0;
int bi = 0;
matrix_t<char> maux(h,nmax);
int i,j,c,ind;
vector<int> pos(h,0);
int nf=0;
vector<int>nx(h,0);
int val=0;
for(i=0;i<h;i++){
muro.at(i,0)='|';
}
int stop=0;
for(i=0;i<=nmax;i++){ /
val+=pow(2,i);
}
val = pow(val,h);
for(ind=0;ind<val;ind++){
j=h-1;
while((stop==0)&&(j>=0)){
if(relleno(maux,pos,j)==true)
j--;
else
stop=1;
}
int v=0;
bool continuar;
for(i=0;i<h;i++){
j=1;
continuar=true;
while(((j<muro.get_n())&&(v<nmax)&&(continuar==true))){
if(maux.at(i,v)=='A'){
if((j+A.dim()< muro.get_n())){
if((i==0)||((i!=0)&&(muro.at(i-1,j+A.dim())!='|'))){
while(ai<A.dim()){
muro.at(i,j) = A.val(ai);
++ai;
j++;
}
}
ai = 0;
}
if((j+A.dim() == muro.get_n())){
while(ai<A.dim()){
muro.at(i,j) = A.val(ai);
++ai;
j++;
}
ai = 0;
}
}
if(maux.at(i,v)=='B'){
if ((j+B.dim()< muro.get_n())){
if((i==0)||((i!=0)&&(muro.at(i-1,j+B.dim())!='|'))){
while(bi<B.dim()){
muro.at(i,j) = B.val(bi);
++bi;
j++;
}
}
bi=0;
}
if((j+B.dim() == muro.get_n())) {
while(bi<B.dim()){
muro.at(i,j) = B.val(bi);
++bi;
j++;
}
bi=0;
}
}
if((maux.at(i,v)!='A')&&(maux.at(i,v)!='B')){
continuar=false;
}
v++;
}
v=0;
}
nf=0;
for(c=0;c<h;c++){
for(j=0;j<muro.get_n();j++){
if(muro.at(c,j)=='x')
++nx[c];
}
}
for(c=0;c<h-1;c++){
if((nx[c]!=nx[c+1])||(nx[c]!=l))
++nf;
}
if((nf==0)&&(nx[0]==l)){
++ncomb;
escribir();
}
}
}
};
main.cpp
#include <iostream>
#include "muro.hpp"
using namespace std;
int main(void){
int h,l,a,b;
cout<<"\nIntroduzca las dimensiones del muro: ";
cin>>h;
cin>>l;
cout<<"\nIntroduzca el tamaño de los bloques en orden ascendente: ";
cin>>a;
cin>>b;
bloque A(a);
bloque B(b);
Muro m1(l,h,A,B);
m1.combinacion(A,B);
}