UVA_10651_PebbleSolitaire

来源:互联网 发布:微信淘宝链接转换技术 编辑:程序博客网 时间:2024/06/05 04:16

10651 - Pebble Solitaire

Time limit: 3.000 seconds

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 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 nd 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 fteenth 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 nd 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
5
---oo-------
-o--o-oo----
-o----ooo---
oooooooooooo
oooooooooo-o
Sample Output
1
2
3
12
1


这个题目最开始看起来有点棘手

但是发现棋盘大小是固定的

那么最多才只有2^12的状态数量

那么简单的bfs一下或者dfs一下所有的状态

用二进制表示下状态就可以了


另外其实每种状态对应的最终状态的数量是固定的

因此也可以记录下来每个状态对应的最终状态的棋子数目

然后每次输入只需要查询数组就可以了


#include <iostream>#include <stdio.h>#include <queue>#include <string.h>using namespace std;const int M=1<<13;int isu[M];char ti[15];int f;queue<int > re;int cn(int now){    int c=0;    while(now>0)    {        if(now&1)            c++;        now>>=1;    }    return c;}void bfs(int now){    while(!re.empty())        re.pop();    isu[now]=cn(now);    f=min(f,isu[now]);    if(f==1)        return;    re.push(now);    while(!re.empty())    {        int t=re.front();        re.pop();        //cout<<t<<" ";        for(int i=0;i<12;i++)        {            if((1<<i)&t)            {                if(i-2>=0&&((1<<(i-1))&t)&&!((1<<(i-2))&t))                {                    int tmp=t-(1<<(i-1))-(1<<i)+(1<<(i-2));                    if(!isu[tmp])                    {                        isu[tmp]=isu[t]-1;                        re.push(tmp);                        f=min(f,isu[tmp]);                        if(f==1)                            return;                    }                }                if(i+2<12&&((1<<(i+1))&t)&&!((1<<(i+2))&t))                {                    int tmp=t-(1<<(i+1))-(1<<i)+(1<<(i+2));                    if(!isu[tmp])                    {                        isu[tmp]=isu[t]-1;                        re.push(tmp);                        f=min(f,isu[tmp]);                        if(f==1)                            return;                    }                }            }        }    }}int main(){    int t;    int now;    scanf("%d",&t);    while(t--)    {        scanf("%s",ti);        now=0;f=15;        for(int i=0;i<12;i++)        {            if(ti[i]=='o')                now+=(1<<i);        }        bfs(now);        printf("%d\n",f);    }    return 0;}


0 0