uva 10651- Pebble Solitaire(状态压缩DP)待看。。。
2013-09-27 16:34
477 查看
1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1592
2、题目:
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, 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.
Swedish National Contest
类似于跳棋,当两颗石子左或者右有空位置时,移动。每次转移之后移去经过的石子。
思路:
有12个格子,所以状态最多有2^12=4096个。把每次搜索过的状态存在dp[]数组中,以后再次查询类似的直接返回即可。
3、AC代码:
2、题目:
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, 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 |
类似于跳棋,当两颗石子左或者右有空位置时,移动。每次转移之后移去经过的石子。
思路:
有12个格子,所以状态最多有2^12=4096个。把每次搜索过的状态存在dp[]数组中,以后再次查询类似的直接返回即可。
3、AC代码:
#include <cstdio> #include <cstdlib> #include <cstring> #define min(a,b) (((a) < (b)) ? (a) : (b)) int dp[4100]; int solve(int n) { if (dp != -1) return dp ; dp = 0; for (int i = 0; i < 12; ++i) if (n & (1 << i)) dp += 1; for (int i = 0; i < 10; ++i) { int t; if ((n&(1<<i)) && (n&(1<<(i+1))) && !(n&(1<<(i+2)))) { t = n; t &= ~(1 << i); t &= ~(1 << (i+1)); t |= 1 << (i+2); dp = min(dp , solve(t)); } if (!(n&(1<<i)) && (n&(1<<(i+1))) && (n&(1<<(i+2)))) { t = n; t &= ~(1 << (i+1)); t &= ~(1 << (i+2)); t |= 1 << i; dp = min(dp , solve(t)); } } return dp ; } int main() { memset(dp, -1, sizeof(dp)); int cases; scanf("%d", &cases); while (cases--) { char str[20]; int n = 0; scanf("%s", str); for (int i = 0; i < 12; ++i) if (str[i] == 'o') n ^= 1 << i; printf("%d\n", solve(n)); } return 0; }
相关文章推荐
- sleep和wait有什么区别
- 分布式文件系统Hadoop、GoogleFS、RAID介绍
- RAID组中的“Foreign”状态磁盘
- 深入理解DefaultMessageListenerContainer
- waiting for debugger
- 亚马逊服务器下创建raid0
- 关于adb server didn't ack failed to start daemon
- 从服务器构建说起(二).磁盘阵列RAID
- PowerDesiger Conncetion test failed!
- 解决Httpclient 4 偶尔报错ConnectionPoolTimeoutException: Timeout waiting for connection from pool
- MST(prim)+树形dp-hdu-4756-Install Air Conditioning
- Climbing Stairs
- Email模块中的POP3协议
- tools:context=”.MainActivity”与xmlns:tools名字空间
- GitLab 6.1 使用postfix发送email
- ListEntry 链表图解,及解析 InsertTailList & RemoveHeadList&RemoveEntryList函数
- Cited from http://hi.baidu.com/zengzhaonong/blog/item/4b901ee9b5ef343cb90e2d16.html
- 两种AIDL用法分析(原)
- 详细分析CONTAINING_RECORD宏
- 嵌套的 CONTAINING_RECORD 宏