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

题意:就是跳棋。比如-oo 可以第三个棋子跳到-上 消除掉中间那个o。

思路:bfs + 哈希判重。记录下每次状态。之后不考虑重复状态。

代码:

#include <stdio.h>
#include <string.h>
#include <limits.h> int t, vis[5555], ans;
char str[15];
struct Q {
char str[15];
int num;
} q[5555];
int hash(char *str) {
int num = 0, i;
for (i = 0; i < 12; i ++) {
if (str[i] == 'o') {
num += 1 << i;
}
}
return num;
} void bfs() {
int i, head = 0, rear = 1;
ans = INT_MAX;
memset(q, 0, sizeof(q));
memset(vis, 0, sizeof(vis));
strcpy(q[0].str, str);
vis[hash(q[0].str)] = 1;
for (i = 0; i < 12; i ++)
if (q[0].str[i] == 'o')
q[0].num ++;
while (head < rear) {
if (q[head].num < ans)
ans = q[head].num;
for (i = 0; i < 10; i ++) {
if (q[head].str[i] == '-' && q[head].str[i + 1] == 'o' && q[head].str[i + 2] == 'o') {
char sb[15];
strcpy(sb, q[head].str);
sb[i] = 'o';
sb[i + 1] = '-';
sb[i + 2] = '-';
if (!vis[hash(sb)]) {
vis[hash(sb)] = 1;
strcpy(q[rear].str, sb);
q[rear].num = q[head].num - 1;
rear ++;
}
}
}
for (i = 0; i < 10; i ++) {
if (q[head].str[i] == 'o' && q[head].str[i + 1] == 'o' && q[head].str[i + 2] == '-') {
char sb[15];
strcpy(sb, q[head].str);
sb[i] = '-';
sb[i + 1] = '-';
sb[i + 2] = 'o';
if (!vis[hash(sb)]) {
vis[hash(sb)] = 1;
strcpy(q[rear].str, sb);
q[rear].num = q[head].num - 1;
rear ++;
}
}
}
head ++;
}
}
int main () {
scanf("%d%*c", &t);
while (t --) {
gets(str);
bfs();
printf("%d\n", ans);
}
return 0;
}

UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))的更多相关文章

  1. uva 10651 - Pebble Solitaire(记忆化搜索)

    题目链接:10651 - Pebble Solitaire 题目大意:给出一个12格的棋盘,‘o'代表摆放棋子,’-‘代表没有棋子, 当满足’-oo'时, 最右边的棋子可以跳到最左边的位子,而中间的棋 ...

  2. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

  3. UVa 10651 Pebble Solitaire(DP 记忆化搜索)

    Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...

  4. UVA 10651 Pebble Solitaire 状态压缩dp

    一开始还在纠结怎么表示一个状态,毕竟是一个串.后来搜了一下题解发现了这里用一个整数的前12位表示转态就好了 ,1~o,0~'-',每个状态用一个数来表示,然后dp写起来就比较方便了. 代码: #inc ...

  5. UVA 10891 Game of Sum(区间DP(记忆化搜索))

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. UVA - 10817 Headmaster's Headache (状压dp+记忆化搜索)

    题意:有M个已聘教师,N个候选老师,S个科目,已知每个老师的雇佣费和可教科目,已聘老师必须雇佣,要求每个科目至少两个老师教的情况下,最少的雇佣费用. 分析: 1.为让雇佣费尽可能少,雇佣的老师应教他所 ...

  7. codevs 1004 四子连棋 BFS、hash判重

    004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...

  8. FZU 2092 bfs+记忆化搜索

    晚上团队训练赛的题 和普通bfs不同的是 这是同时操纵人与影子两个单位进行的bfs 由于可能发生人和影子同时接触水晶 所以不可以分开操作 当时使用node记录人和影子的位置 然后进行两重for循环来分 ...

  9. FZU 2092 收集水晶 bfs+记忆化搜索 or 暴力

    题目链接:收集水晶 一眼看过去,觉得是普通的bfs,初始位置有两个.仔细想了想...好像如果这样的话..........[不知道怎么说...T_T] dp[12][12][12][12][210] 中 ...

随机推荐

  1. mysql limit性能问题

    offset大的时候的比较 1. SELECT * FROM persons LIMIT 200000,10; 耗时0.109s 2. SELECT *FROM persons WHERE id> ...

  2. Navicat:cant create OCI environment.

    一直在使用 Navicat ,这是一个数据库客户端软件,能连接多种不同类型的数据库,给我们的日常的工作带来了不少的便捷. 最近,我在电脑上安装了oracle的客户端ODTwihtODAC121012, ...

  3. SharePoint中获取当前登录的用户名几种方式

    第一种方法: System.Web.HttpContext.Current.User.Identity.Name.ToString();或者: SPContext.Current.Site.OpenW ...

  4. ajax分页实现,jquery.pagination.js

    1.前台使用ajax无刷新分页,主要需要生成分页的工具条,这里使用的是jquery.pagination.js 插件参数可以参考----张龙豪-jquery.pagination.js分页 下面贴出代 ...

  5. Page_Load基类,重写OnLoad

    protected override void OnLoad(EventArgs e) { userid = PublicFun.GetSessionValue(HttpContext.Current ...

  6. Android学习手记(5) 基本UI布局

    1.View和ViewGroup Activity是Android应用程序的基本管理单元,Android的每一个窗口都是通过一个Activity来定义的,但是Activity并不能直接用来显示窗口.我 ...

  7. redis例子

    http://www.cnblogs.com/edisonfeng/p/3571870.html

  8. ReactiveCocoa 简单使用

    #pragma mark 指令 -(void) instructionDemo { // 创建使能信号 RACSignal * signal = [self.textField.rac_textSig ...

  9. ubuntu 下源码安装Postgreql pgAdmin3

    一.安装 PostgreSQL 1.安装相关依赖,在终端下执行: sudo apt-get install zlib1g-dev    sudo apt-get install libreadline ...

  10. php插入转义与查找转义

    //转义用于查找 function deepslashes($data) { //判断data表现形式 if(empty($data)) { return $data; } return is_arr ...