Uva--10651(动规,记忆化搜索)
2014-08-02 19:42:38
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, with B in the middle, whereA 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
|
思路:也是一种记忆化搜索,对于每层递归,扫整个字符串,看看哪里可以进行变幻,能变幻就继续递归进去。
1 /************************************************************************* 2 > File Name: l.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Thu 31 Jul 2014 05:30:20 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 int n; 17 char s[15]; 18 19 int Count(int p){ 20 int cnt = 0; 21 while(p){ 22 cnt += p & 1; 23 p >>= 1; 24 } 25 return cnt; 26 } 27 28 int Judge(int p,int i){ 29 if(!(p & (1 << i)) && (p & (1 << (i + 1))) && (p & (1 << (i + 2)))) 30 return 1; 31 else if((p & (1 << i)) && (p & (1 << (i + 1))) && !(p & (1 << (i + 2)))) 32 return 2; 33 return 0; 34 } 35 36 int Solve(int p){ 37 int cnt = Count(p); 38 int tmin = 1e9; 39 int flag; 40 int t; 41 for(int i = 0; i < 10; ++i){ 42 flag = Judge(p,i); 43 if(flag == 1){ 44 t = p; 45 t |= (1 << i); 46 t ^= (1 << (i + 1)); 47 t ^= (1 << (i + 2)); 48 //printf("flag 1 , t : %d\n",t); 49 tmin = min(tmin,Solve(t)); 50 } 51 else if(flag == 2){ 52 t = p; 53 t ^= (1 << i); 54 t ^= (1 << (i + 1)); 55 t |= (1 << (i + 2)); 56 //printf("flag 2 , t : %d\n",t); 57 tmin = min(tmin,Solve(t)); 58 } 59 } 60 return min(tmin,cnt); 61 } 62 63 int main(){ 64 int num; 65 scanf("%d",&n); 66 while(n--){ 67 scanf("%s",s); 68 num = 0; 69 for(int i = 0; i < 12; ++i){ 70 if(s[i] == 'o') 71 num |= (1 << i); 72 } 73 //printf("num : %d\n",num); 74 printf("%d\n",Solve(num)); 75 } 76 return 0; 77 }