I'm writing a program to find if I can make a square with N sticks of N length. Ive used Bitmask cause its easier for the big input I'm gonna put in there. For some reason, my backtracking code its not returning anything, in this line "bitmask & (1 << vec[i])" it never goes in. Can someone help me if im using the wrong functions? I've recently learned this, but I cant find info.
PD:im not an english speaker, sorry for my grammar and syntax problems.
Thanks
// 10364 - Square.cpp: archivo de proyecto principal.
#include "stdafx.h"
#include <stdio.h>
#include <vector>
using namespace std;
vector<int> vec;
int bitmask,casos,palitos,suma,maximo;
bool analiza(int analizado,int lados)// lados =cantidad lados analizados, contador
{
if (analizado==maximo)
{
analizado=0;
lados++;
}
if(lados==4)
{
printf("yes\n");
return true;
}
for (int i=0;i<palitos;i++)
{
if (!(bitmask & (1 << i)))
{
if (analizado+vec[i]<=maximo)
{
bitmask | (1 << i);
if(analiza(analizado+1,lados))
return true;
bitmask & ~(1 << i);
}
}
}
return false;
//prender: bitmask | (1 << indice)
//apagar: bitmask & ~(1 << indice)
/*comparar: bitmask & (1 << indice)*/
}
int main()
{
freopen("in.txt","rt",stdin);
freopen("out.txt","wt",stdout);
scanf("%d\n",&casos);
for(int i=0;i<casos;i++)
{
scanf("%d",&palitos);
vec.clear();
vec.resize(palitos);
suma=0;
for(int j=0;j<palitos;j++)
{
scanf("%d",&vec[j]);
suma+=vec[j];
}
if(suma%4!=0)
printf("no\n");
else{
bitmask=0;
maximo=suma/4;
analiza(0,0);
}
}
return 0;
}