Ultraviolet A 10651 Pebble Solitaire (BFS)

Source: Internet
Author: User

Ultraviolet A 10651 Pebble Solitaire (BFS)
Ultraviolet A 10651 Pebble Solitaire

Pebble solitaire is an interesting game. this is a game where you are given a board with an arrangement of small cavities, initially all but one occupied by a pebble each. the aim of the game is to remove as your pebbles as possible from the board. pebbles disappear from the board as a result of a move. A move is possible if there is a straight line of three adjacent cavities, let us call them A, B, and C, with B in the middle, where A is vacant, but B and C each contain a pebble. the move constitutes of moving the pebble from C to A, and removing the pebble in B from the board. you may continue to make moves until no more moves are possible.

In this problem, we look at a simple variant of this game, namely a board with twelve cavities located along a line. in the beginning of each game, some of the cavities are occupied by pebbles. your mission is to find a sequence of moves such that as few pebbles as possible are left on the board.

Input

The input begins with a positive integer n on a line of its own. thereafter n different games follow. each game consists of one line of input with exactly twelve characters, describing the twelve cavities of the board in order. each character is either '-' or 'O' (The teenth character of English alphabet in lowercase ). a'-'(minus) character denotes an empty cavity, whereas A 'O' character denotes a cavity with a pebble in it. as you will find in the sample that there may be inputs where no moves is possible.

Output

For each of the n games in the input, output the minimum number of pebbles left on the board possible to obtain as a result of moves, on a row of its own.

Sample Input Output for Sample Input

5

-Oo ---

-O-oo --

-O -- ooo-

Oooooooooooooo

Oooooooooo-o

1

2

3

12

1

The following figure shows the arrangement of pawns (in fact, pebbles, as pawns). 'O' indicates that there are pawns at the current position, and '-' indicates that there is null. When "oo-" or "-oo" occurs, the pawn can jump to the other side of the Support Point, the pawns used as the support point will disappear. Q. The minimum number of pieces left after the jump. Solution: Both BFS and DFS can be used.
#include 
  
   #include 
   
    #include #include 
    
     #include 
     
      using namespace std;typedef long long ll;char s[13];int first, last, Min, vis[10005];struct queue{    char num[13];    int hash() {        int sum = 0;        for (int i = 0; i < 12; i++) {            if (num[i] == 'o') {                sum += 1 << (12 - i);             }        }        return sum;    }};queue q[100005];void BFS() {    char temp[13];    while (first < last) {        int cnt = 0;        strcpy(temp, q[first].num);        for (int i = 0; i < 12; i++) {            if (temp[i] == 'o') {                cnt++;                if (i >= 2) {                    if (temp[i - 1] == 'o' && temp[i - 2] == '-') {                        strcpy(q[last].num, temp);                        q[last].num[i] = q[last].num[i - 1] = '-';                        q[last].num[i - 2] = 'o';                        if (!vis[q[last].hash()]) {                            vis[q[last].hash()] = 1;                            last++;                        }                    }                }                if (i <= 9) {                    if (temp[i + 1] == 'o' && temp[i + 2] == '-') {                        strcpy(q[last].num, temp);                        q[last].num[i] = q[last].num[i + 1] = '-';                        q[last].num[i + 2] = 'o';                        if (!vis[q[last].hash()]) {                            vis[q[last].hash()] = 1;                            last++;                        }                    }                }            }        }        if (cnt < Min) Min = cnt;        first++;    }}int main() {    int T;    scanf("%d", &T);    while (T--) {        memset(vis, 0, sizeof(vis));        scanf("%s", s);        first = last = 1;        strcpy(q[first].num, s);                    vis[q[first].hash()] = 1;        last++;        Min = 13;        BFS();        printf("%d\n", Min);    }    return 0;}
     
    
   
  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.