博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 10651- Pebble Solitaire(状态压缩DP)待看。。。
阅读量:4035 次
发布时间:2019-05-24

本文共 2936 字,大约阅读时间需要 9 分钟。

1、

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 AB, 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

 


Swedish National Contest

类似于跳棋,当两颗石子左或者右有空位置时,移动。每次转移之后移去经过的石子。

思路:

有12个格子,所以状态最多有2^12=4096个。把每次搜索过的状态存在dp[]数组中,以后再次查询类似的直接返回即可。

3、AC代码:

#include 
#include
#include
#define min(a,b) (((a) < (b)) ? (a) : (b))int dp[4100];int solve(int n){ if (dp[n] != -1) return dp[n]; dp[n] = 0; for (int i = 0; i < 12; ++i) if (n & (1 << i)) dp[n] += 1; for (int i = 0; i < 10; ++i) { int t; if ((n&(1<
<< i); t &= ~(1 << (i+1)); t |= 1 << (i+2); dp[n] = min(dp[n], solve(t)); } if (!(n&(1<
<< (i+1)); t &= ~(1 << (i+2)); t |= 1 << i; dp[n] = min(dp[n], solve(t)); } } return dp[n];}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;}

转载地址:http://fhddi.baihongyu.com/

你可能感兴趣的文章
C++程序员的几种境界
查看>>
VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法
查看>>
VUE-Vue.js之$refs,父组件访问、修改子组件中 的数据
查看>>
Vue-子组件改变父级组件的信息
查看>>
Python自动化之pytest常用插件
查看>>
Python自动化之pytest框架使用详解
查看>>
【正则表达式】以个人的理解帮助大家认识正则表达式
查看>>
性能调优之iostat命令详解
查看>>
性能调优之iftop命令详解
查看>>
非关系型数据库(nosql)介绍
查看>>
移动端自动化测试-Windows-Android-Appium环境搭建
查看>>
Xpath使用方法
查看>>
移动端自动化测试-Mac-IOS-Appium环境搭建
查看>>
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
测试必会之 Linux 三剑客之 sed
查看>>