UVa : 10651 (Pebble Solitaire)


Recursive Solution (Exhaustive search)

// http://uva.onlinejudge.org/external/106/10651.html

// Run time : 0.012s

// @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 <bitset>
#include <utility>
#include <set>
#include <math.h>
#define pi acos(-1.0)
#define N 1000000
using namespace std;

int cnt;

void recursive (char a [])
{
    char org [15];
    strcpy (org, a);

    for ( int i = 0; i < 11; i++ ) {
        if ( a [i] == 'o' && a [i + 1] == 'o' ) {
            if ( i + 2 < 12 && a [i + 2] == '-' ) {
                a [i + 2] = 'o';
                a [i] = a [i + 1] = '-';
                recursive (a);
                strcpy (a, org);
            }
            if ( i - 1 >= 0 && a [i - 1] == '-' ) {
                a [i - 1] = 'o';
                a [i] = a [i + 1] = '-';
                recursive (a);
                strcpy (a, org);
            }
        }
    }

    int c = 0;

    for ( int i = 0; i < 12; i++ ) {
        if ( a [i] == 'o' ) c++;
    }

    if ( c < cnt )
        cnt = c;
}

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

    while ( testCase-- ) {
        char input [12 + 3];
        scanf ("%s", input);

        cnt = 100;

        recursive (input);

        printf ("%d\n", cnt);
    }

    return 0;
}

// @END_OF_SOURCE_CODE

Dynamic programming solution


// Run time : 0.008s

// @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 <bitset>
#include <utility>
#include <set>
#include <math.h>
#define pi acos(-1.0)
#define N 1000000
using namespace std;

int cnt;
map <string, int> m;
string s;

int recursive (string s)
{
    if ( m [s] )
        return m [s];
    string t = s;

    for ( int i = 0; i < 11; i++ ) {
        if ( s.at (i) == 'o' && s.at (i + 1) == 'o' ) {
            if ( i + 2 < 12 && s.at (i + 2) == '-' ) {
                s.at (i + 2) = 'o';
                s.at (i) = s.at (i + 1) = '-';
                m [s] = recursive (s);
                s = t;
            }
            if ( i - 1 >= 0 && s.at (i - 1) == '-' ) {
                s.at (i - 1) = 'o';
                s.at (i) = s.at (i + 1) = '-';
                m [s] = recursive (s);
                s = t;
            }
        }
    }

    int c = 0;

    for ( int i = 0; i < 12; i++ ) {
        if ( s.at (i) == 'o' ) c++;
    }

    if ( c < cnt )
        cnt = c;

    return cnt;
}

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

    while ( testCase-- ) {
        string input;
        cin >> input;

        cnt = 100;

        printf ("%d\n", recursive (input));
    }

    return 0;
}

// @END_OF_SOURCE_CODE


Breadth first search solution


// Run time : 0.008s

// @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 <bitset>
#include <utility>
#include <set>
#include <math.h>
#define pi acos(-1.0)
#define N 1000000
using namespace std;

string child;
queue <string> q;
map <string, bool> visited;
int cnt;

bool generateState (string s)
{
    string t = s;
    bool haveChild = false;

    for ( int i = 0; i < 11; i++ ) {
        if ( s.at (i) == 'o' && s.at (i + 1) == 'o' ) {
            if ( i + 2 < 12 && s.at (i + 2) == '-' ) {
                s.at (i + 2) = 'o';
                s.at (i) = s.at (i + 1) = '-';
                if ( !visited [s] ) {
                    visited [s] = true;
                    q.push (s);
                }
                haveChild = true;
                s = t;
            }
            if ( i - 1 >= 0 && s.at (i - 1) == '-' ) {
                s.at (i - 1) = 'o';
                s.at (i) = s.at (i + 1) = '-';
                if ( !visited [s] ) {
                    visited [s] = true;
                    q.push (s);
                }
                haveChild = true;
                s = t;
            }
        }
    }

    return haveChild;

}

int countPebble (string p)
{
    int c = 0;

    for ( size_t i = 0; i < p.size (); i++ ) {
        if ( p.at (i) == 'o' )
            c++;
    }

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

    while ( testCase-- ) {
        string input;
        cin >> input;

        cnt = 100;
        q.push (input);
        visited [input] = true;

        while ( !q.empty () ) {
            string pop = q.front ();
            q.pop ();

            if ( !generateState (pop) ) {
                int t = countPebble (pop);
                if ( t < cnt )
                    cnt = t;
            }
        }

        printf ("%d\n", cnt);

    }

    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