首页 > 其他 > 详细

UVa10651 - Pebble Solitaire

时间:2014-03-08 00:25:38      阅读:552      评论:0      收藏:0      [点我收藏+]

Problem A
Pebble Solitaire
Input:
 standard input
Output: standard output
Time Limit: 1 second
 

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 many 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 AB, 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 Bfrom 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.

 

bubuko.com,布布扣

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 fifteenth 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--o-oo----

-o----ooo---

oooooooooooo

oooooooooo-o

1

2

3

12

1

 


Swedish National Contest


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
struct node{
    int a,b,c;
    node(int a,int b,int c):a(a),b(b),c(c){}
};
string st;

int dp[1<<13];
int dfs(int mask){
    if(dp[mask] !=-1) return dp[mask];
    vector<int> v;
    vector<node>pos;
    v.clear();
    pos.clear();
    int tmp = mask;
    for(int i = 0; i < 12; i++){
        v.push_back(tmp%2);
        tmp /= 2;
    }
    int i = 1,ret = v[0];
    bool flag = 0;
    while(i < 12){
        if(v[i]==1){
            ret++;
            if((v[i+1]==1&&v[i-1]==0&&i+1<12)||(v[i+1]==0&&v[i-1]==1&&i+1<12)){
                 flag = 1;
                 pos.push_back(node(i-1,i,i+1));
            }
        }
        i++;
    }
    if(!flag)
        return dp[mask] = ret;
    int ans = 1e8;
    for(int i = 0; i < pos.size(); i++){
        int tma = mask;
        if(v[pos[i].a]==0){
            tma ^=  (1<<pos[i].b);
            tma ^=  (1<<pos[i].c);
            tma  |= (1<<pos[i].a);
        }else{
            tma ^=  (1<<pos[i].b);
            tma |=  (1<<pos[i].c);
            tma ^= (1<<pos[i].a);
        }
        ans = min(ans,dfs(tma));
    }
    return dp[mask] = ans;
}
int main(){
    int ncase;
    cin >> ncase;
    memset(dp,-1,sizeof dp);
    while(ncase--){
            cin >> st;
            int ms = 0;
            for(int i = 0; i < st.size(); i++)
                if(st[i]==‘o‘)  ms |= (1<<i);
            cout<<dfs(ms)<<endl;
    }
    return 0;
}


UVa10651 - Pebble Solitaire,布布扣,bubuko.com

UVa10651 - Pebble Solitaire

原文:http://blog.csdn.net/mowayao/article/details/20724955

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!