UVA 10651 Pebble Solitaire(记忆化)
2014-11-22 23:31
337 查看
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 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.
![](http://uva.onlinejudge.org/external/106/p10651.jpg)
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.
Swedish National Contest
问最后最少留下的数:oo- -->--o,-oo -->o--
d[s]代表s串最多可去掉的数;每步可减去一个.
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 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.
![](http://uva.onlinejudge.org/external/106/p10651.jpg)
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 |
问最后最少留下的数:oo- -->--o,-oo -->o--
d[s]代表s串最多可去掉的数;每步可减去一个.
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) map<string,int>d; string str,t; int n,ans; int dp(string s) { if(d[s]>0) return d[s]; d[s]=1; for(int i=0;i<10;i++) { if(s[i]=='o'&&s[i+1]=='o'&&s[i+2]=='-') { t=s; t[i]=t[i+1]='-'; t[i+2]='o'; d[s]=max(d[s],dp(t)+1); } if(s[i]=='-'&&s[i+1]=='o'&&s[i+2]=='o') { t=s; t[i]='o'; t[i+1]=t[i+2]='-'; d[s]=max(d[s],dp(t)+1); } } return d[s]; } int main() { std::ios::sync_with_stdio(false); cin>>n; while(n--) { cin>>str; ans=1; REP(i,12) if(str[i]=='o') ans++; ans-=dp(str); cout<<ans<<endl; } return 0; }
相关文章推荐
- 【DP】UVA 10651 Pebble Solitaire 记忆化搜索
- 【UVA】10651-Pebble Solitaire(直接递归或者记忆化)
- 【DP】UVA 10651 Pebble Solitaire 记忆化搜索
- UVa 10651 Pebble Solitaire (DP&bitset)
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
- UVA 10651 Pebble Solitaire 状态压缩dp
- UVa 10651 Pebble Solitaire(状压DP)
- uva 10651 - Pebble Solitaire(记忆化搜索)
- uva 10651 Pebble Solitaire(动态规划:记忆化搜索)
- UVa 10651 Pebble Solitaire(状态压缩DP)
- UVA 10651 - Pebble Solitaire
- Pebble Solitaire - UVa 10651 状压dp+记忆化搜素
- UVa 10651 Pebble Solitaire (DP 卵石游戏 记忆化搜索)
- UVA 10651 Pebble Solitaire
- uva 10651 - Pebble Solitaire
- uva_10651_Pebble Solitaire(状态压缩DP)
- UVa 10651 Pebble Solitaire(状态压缩DP)
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
- uva 10651 Pebble Solitaire(dp,状态压缩,记忆化搜索)
- UVa 10651 - Pebble Solitaire