UVa : 11535 (Set of Marbles)


One of the problems from my first contest ULAB NCPC 2008. This is the third most solvable problem.

1. Just check all possible combination of 2 ^ n. here value of n is pretty small.
2. I have used bit masking. For example, 5 (decimal) = 0000000101 (binary)
it means, ball numbered 1 and 3 are in Box1. Because 1st and 3rd bit is one
you can easily keep track of visited states using decimal numbers
maximum number of states = 2 ^ 10 = 1024
3. It’s a special judge problem, so it has different accepted solution.

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#define INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL unsigned long long
using namespace std;

int main ()
{
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        int n; scanf ("%d", &n);
        int b; scanf ("%d", &b);
        int start = 0;

        for ( int i = 0; i < b; i++ ) {
            int p; scanf ("%d", &p);
            start |= (1 << (p - 1));
        }

        bool vis [1024 + 5];
        memset (vis, false, sizeof (vis));

        vis [start] = true;
        int cnt = 1;

        while ( cnt != (1 << n)) {
            for ( int i = 0; i < n; i++ ) {
                int bit = start & (1 << i);
                int newNum = start;

                if ( bit == 0 ) newNum |= (1 << i);
                else newNum ^= (1 << i);

                if ( !vis [newNum] ) {
                    vis [newNum] = true;
                    start = newNum;
                    printf ("Move %d from B%d to B%d\n", i + 1, bit ? 1 : 2, bit ? 2 : 1);
                    cnt++;
                    break;
                }
            }
        }

        printf ("\n");

    }

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s