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 your 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 themA,B, AndC,BIn the middle, whereAIs vacant,BAndCEach contain a pebble. The move constitutes of moving the pebble fromCToA, And removing the pebble inBFrom 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 integerNOn a line of its own. ThereafterNDifferent 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 specified teenth character of English alphabet in lowercase).'-'(Minus) character denotes an empty cavity, whereas'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 --- Oooooooooooooo Oooooooooo-o |
1 2 3 12 1 |
A 12-character string consists of '-' and 'O'. "-oo" and "Oo-" can be converted to "O --" by one conversion --" and "-- O", you can find that each conversion o is missing one. You only need to find the number of times that your string can be converted;
D [s] indicates the maximum number of times string s can be converted. If s can be converted to string t by one conversion, d [s] = max (d [s], d [T] + 1 );
#include<iostream>#include<string>#include<map>using namespace std;map<string, int> d;int n, ans;string t, S;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(){ cin >> n; while (n--) { ans = 1; cin >> S; for (int i = 0; i < 12; ++i) if (S[i] == 'o') ans++; ans -= dp (S); cout << ans << endl; } return 0;}
Zookeeper
Ultraviolet A 10651 pebble solitaire (DP memory-based search)