uva 10651 - Pebble Solitaire

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.

 

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

 



这道题是一道简单状压dp,只要熟练掌握位运算即可,实际上这道题只要判断是否存在‘-oo'和’oo-'的情况,然后对这三位按位取反得到一个新的状态,一直到状态没有上述两种情况存在,就统计二进制1的位数。其余过程就是简单的记忆化搜索。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

char ch;
const int Maxn=1<<12;
int dp[Maxn];
bool check(int x,int i){
    if(x&1<<i-1&&x&1<<i&&!(x&1<<i+1)) return true;
    if(!(x&1<<i-1)&&x&1<<i&&x&1<<i+1) return true;
    return false;
}
int countbit(int x){
    int ans=0;
    while(x){
        ans+=x&1;
        x>>=1;
    }
    return ans;
}
int dfs(int x){
    int &ans=dp[x];
    if(ans!=13) return ans;
    bool flag=true;
    for(int i=1;i<11;i++)
        if(check(x,i)){
            ans=min(ans,dfs(x^1<<i-1^1<<i^1<<i+1));
            flag=false;
        }
    if(flag) return countbit(x);
    return ans;
}
int main()
{
    int n,num;
    scanf("%d%*c",&n);
    while(n--){
        num=0;
        for(int i=0;i<12;i++){
            scanf("%c",&ch);
            if(ch=='o') num|=1<<i;
        }
        scanf("%*c");
        for(int i=0;i<Maxn;i++) dp[i]=13;
        printf("%d\n",dfs(num));
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity Pebble 服务中的广播可以通过以下步骤来实现: 1. 在 Unity 中创建一个新的脚本,例如 BroadcastManager。 2. 在脚本中定义需要广播的消息类型和参数。 3. 使用 Unity 的 SendMessage() 函数来发送消息。 4. 在 Pebble 应用程序中创建一个监听器来接收广播。 5. 在监听器中定义需要执行的操作。 下面是一个示例代码: 在 Unity 中: ``` // BroadcastManager.cs public class BroadcastManager : MonoBehaviour { // Define message type and parameters public enum MessageType { PLAYER_HEALTH_CHANGED, PLAYER_POSITION_CHANGED, PLAYER_SCORE_CHANGED } // Send message using SendMessage() public void SendBroadcastMessage(MessageType messageType, object[] parameters) { SendMessage(messageType.ToString(), parameters); } } ``` 在 Pebble 应用程序中: ``` // AppMessageHandlers.c static void inbox_received_handler(DictionaryIterator *iter, void *context) { Tuple *tuple = dict_read_first(iter); while (tuple) { switch (tuple->key) { case MESSAGE_KEY_BROADCAST_TYPE: if (strcmp(tuple->value->cstring, "PLAYER_HEALTH_CHANGED") == 0) { // Do something when PLAYER_HEALTH_CHANGED message is received } else if (strcmp(tuple->value->cstring, "PLAYER_POSITION_CHANGED") == 0) { // Do something when PLAYER_POSITION_CHANGED message is received } else if (strcmp(tuple->value->cstring, "PLAYER_SCORE_CHANGED") == 0) { // Do something when PLAYER_SCORE_CHANGED message is received } break; } tuple = dict_read_next(iter); } } ``` 在 Pebble 应用程序中注册监听器: ``` // app_init() app_message_register_inbox_received(inbox_received_handler); app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum()); ``` 注意:在发送广播消息时,需要使用字符串表示消息类型,因此需要将 MessageType 转换为字符串。在接收广播消息时,需要比较字符串以确定消息类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值