uva 10651 Pebble Solitaire (BFS)

uva 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 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 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 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

题目大意:给出一种棋子(其实是鹅卵石,看成是棋子)排列的情况,‘o’代表当前位置有棋子,‘-’代表空。当出现“oo-”或者“-oo”的情况时,棋子可以发生跳转,棋子可以以它相邻的棋子为支撑点跳到支撑点另一边的空位上,作为支撑点的棋子会消失。问。在经过跳转后最少剩下的棋子数。

解题思路:用BFS或者DFS都能做。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
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;
}
时间: 2024-12-25 23:56:49

uva 10651 Pebble Solitaire (BFS)的相关文章

UVa 10651 Pebble Solitaire(DP 记忆化搜索)

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 many pebbles as possible

UVA 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 many pebbles as possible from the board. P

UVa 10651 Pebble Solitaire(状态压缩DP)

题意:转化为01序列,可以做如下转换011–>100, 110–>001 使得序列的1最少 二进制的集合表示: 空集:0 全集:( 1< #include<iostream> #include<algorithm> #include<map> #include<cstdio> #include<cstdlib> #include<vector> #include<cmath> #include<cs

UVA - 439 - Knight Moves (BFS)

UVA - 439 Knight Moves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knigh

UVa 10651 - Pebble Solitaire

题目:有一个类似跳棋的游戏,一共有12个位置'o'代表棋子'-'代表空位, 'oo-'可以转化成'--o','-oo'可以转化成'o--',给你初始状态,问最后,至少剩下几个棋子. 分析:dp,记忆化搜索,位运算.利用搜索在相邻状态间dp即可. 每个状态的最优解为他能转化所有状态中的最优解. 因为,一共有2^12 = 4096个状态,每次找到解即存储,不会重复计算,所以时间方面没问题. 利用一个12位整数表示一个状态,'o'用1表示,'-'用0表示,则'---oo-------'为24(2进制0

UVa 10651 Pebble Solitaire (DP 卵石游戏 记忆化搜索)

 题意  给你一个长度为12的字符串  由字符'-'和字符'o'组成  其中"-oo"和"oo-"分别可以通过一次转换变为"o--"和"--o"  可以发现每次转换o都少了一个  只需求出给你的字符串做多能转换多少次就行了 令d[s]表示字符串s最多可以转换的次数  若s可以通过一次转换变为字符串t  有d[s]=max(d[s],d[t]+1) #include<iostream> #include<s

uva 532 Dungeon Master(BFS)

uva 532 Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. Y

UVA 10047 - The Monocycle(BFS)

题目链接:点击打开链接 题意:从起点到终点,每秒可以选择前进.向左.向右转, 每前进一格轮子转到下一个颜色, 一共5中颜色, 开始的时候绿色接触地面,朝北, 要求最后也绿色接触地面,求能否到达目标点以及最短时间. 思路:和普通BFS相比,多了两个附加条件,所以要将状态表示全面,也要对应加两维. 水题. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream&g

UVA 10651 --Pebble Solitaire +dfs

这道题有两种做法:搜索和状态压缩dp 因为这个题的状态只要2^12,所以可以用dfs或bfs将所有的可达状态走一遍,然后就可以得到答案了. 我是用二进制压缩以后再进行的dfs:其实也可以直接开一个12位长度数组表示状态,然后dfs或bfs,这样 状态判重可以用hash或二进制压缩. 状态压缩dp的话,现在还没看,就放到以后再研究去了. 代码如下: #include<iostream> #include<cstring> #include<cstdio> using na