Pebble Solitaire - UVa 10651 状压dp+记忆化搜素
来源:互联网 发布:北京正大网络科计有17 编辑:程序博客网 时间:2024/05/16 15:34
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 A, B, and C, withB 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
题意:相邻的两块石头,可以让一块跳过另一块,然后把被跳过的拿走,问最后最少剩多少石头。
思路:01的二进制表示状况,然后枚举所有可能的情况往下走,然后记录已经访问过的情况,减少复杂度。
AC代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int num[5010],vis[5010];void dfs(int S){ if(vis[S]==1) return; vis[S]=1; int k=0,p,i; p=S; while(p) { if(p&1) k++; p/=2; } for(i=0;i<=9;i++) if( ((3<<i)&S)==(3<<i) && ( (1<<(i+2))&S)==0) { p=S-(3<<i)+ (1<<(i+2)); dfs(p); k=min(k,num[p]); } for(i=1;i<=10;i++) if( ((3<<i)&S)==(3<<i) && ( (1<<(i-1))&S)==0) { p=S-(3<<i)+ (1<<(i-1)); dfs(p); k=min(k,num[p]); } num[S]=k;}char s[20];int main(){ int t,i,j,k,S; scanf("%d",&t); while(t--) { scanf("%s",s); S=0; for(i=0;i<=11;i++) { S*=2; if(s[i]=='o') S+=1; } dfs(S); printf("%d\n",num[S]); }}
- Pebble Solitaire - UVa 10651 状压dp+记忆化搜素
- UVA 10651 Pebble Solitaire 状压Dp
- UVa 10651 Pebble Solitaire(状压DP)
- uva 10651 Pebble Solitaire(dp,状态压缩,记忆化搜索)
- UVa 10651 Pebble Solitaire(DP 记忆化搜索)
- UVa 10651 Pebble Solitaire (DP 卵石游戏 记忆化搜索)
- 【DP】UVA 10651 Pebble Solitaire 记忆化搜索
- 记忆化搜索 uva-10651-Pebble Solitaire
- UVA 10651 Pebble Solitaire(记忆化)
- UVa 10651 - Pebble Solitaire 状态压缩 dp
- UVa 10651 Pebble Solitaire (DP&bitset)
- UVA 10651 Pebble Solitaire 状态压缩dp
- uva 10651 Pebble Solitaire
- UVA 10651 Pebble Solitaire
- Uva - 10651 - Pebble Solitaire
- uva 10651 pebble Solitaire
- UVa 10651 - Pebble Solitaire
- UVA - 10651 Pebble Solitaire
- 如何取出文本中的单词(实例)
- 80_启动一个应用程序
- 如何在一台pc搭建自己的sftp服务器?
- C语言-----动态内存经典题
- 有一段文本,将文本中的所有单词,存放到一个字符指针数组中(要求每个单词内存恰好)
- Pebble Solitaire - UVa 10651 状压dp+记忆化搜素
- 确定比赛名次
- Intent之FLAG_ACTIVITY_CLEAR_TOP
- 查找最长单词
- 有一段文本,将文本中的所有单词,存放到一个字符指针数组中(要求每个单词内存恰好)
- 【Java编程基础】Chapter 2:标识符、常量和运算符
- 如何检测当前系统的 shell 版本
- 虚析构函数问题:为什么要将基类的的析构函数设成虚的?
- 检查Oracle 中死事务的语句