0

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;
}
Ali
  • 56,466
  • 29
  • 168
  • 265
Giuseppe
  • 490
  • 1
  • 11
  • 27

1 Answers1

2

It doesn't seem like you ever initialize bitmask. If bitmask happens to be 0 then you will never enter the loop. This seems to be part of the problem.
Also (bitmask & (1 << i) can be simplified to just '0' since bitmask is 0 and you are using bitwise and. Therefore that whole if condition can be eliminated as it will always evaluate to true.
Further it doesn't look like you are using a bitmask in the right way. You want a bitmask to have at least one of its bits equal to one as that way when you and some vector with the bitmask you will only keep the relevant bit. Then if you bitshift over you can tell if the interesting bit in the vector is on or off.

emschorsch
  • 1,619
  • 3
  • 19
  • 33
  • but the funtions are ok rigth? – Giuseppe Oct 23 '12 at 04:25
  • for some reason on this line if (!(bitmask & (1 << vec[i]))) , it keeps going in, and ive turned off the bitmask then i use the recursive function, idk why i cant turn it off. – Giuseppe Oct 23 '12 at 04:40
  • But, bitmask 0 , its the initializer, its supposed to have all the flags in zero value – Giuseppe Oct 23 '12 at 04:46
  • It is here bitmask=0; maximo=suma/4; analiza(0,0); – Giuseppe Oct 23 '12 at 05:02
  • when i call the function it initialize in to 0, the proble is , i flag it to 0 , but it stils goes in. – Giuseppe Oct 23 '12 at 05:03
  • this line its not working bitmask | (1 << i); (this suppose to flag true the bitmask) when it comes again to the pos i, it goes in , when it supposed to skip to the i++ – Giuseppe Oct 23 '12 at 05:04
  • I added some comments to my answer, we should probably delete our comments and start our discussion over. – emschorsch Oct 23 '12 at 05:15