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. (转)SVN教程总结

    文章原地址:http://www.cnblogs.com/armyfai/p/3985660.html SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本, ...

  2. Debugging Failed Because Integrated Windows Authentication Is Not Enabled

    To enable integrated Windows authentication Log onto the Web server using an administrator account. ...

  3. 用css3实现闪烁效果

    1. css3 @keyframes 参考 css3 @keyframes规则. 特别注意浏览器支持: Internet Explorer 10, Firefox, 和 Opera 支持 @keyfr ...

  4. Class类相关

    Class类是java.lang包中的类,该类的实例可以帮助程序创建其他类的实例或者取得其他类的对象的内部信息 使用class类获得一个类相关的class类(注意得到的是class类,不是相关的类) ...

  5. CentOS 7 之几个新特性(转)

    上篇我们讲到默认没有ifconfig是centos7的新特性,所以我特意上网搜索了一下其新特性,找到一篇文章,现转过来. centos最小好化安装没有ifconfig命令 刚安装了centos7.0, ...

  6. JS函数定义与匿名函数的调用

    一.函数声明.函数表达式.匿名函数 函数声明:function fnName () {…};使用function关键字 声明一个函数,再指定一个函数名,叫函数声明. 函数表达式 var fnName ...

  7. Windows编程中的若干难点 - Windows程序设计(SDK)007

    Windows编程中的若干难点 让编程改变世界 Change the world by program 一个窗口的生与死 我记得有童鞋会问:如果我的程序需要在关闭前让用户判断是否确定要关闭窗口,我应该 ...

  8. OC 代码规范

    我们写出来的代码会给很多人看,为了使代码清晰简洁,方便阅读理解,都会统一遵从一定的代码规范,Objective-C同样如此.   主要参考规范:   1. Google Objective-C Sty ...

  9. CSS浮动属性Float详解

    什么是CSS Float? float 是 css 的定位属性.在传统的印刷布局中,文本可以按照需要围绕图片.一般把这种方式称为“文本环绕”.在网页设计中,应用了CSS的float属性的页面元素就像在 ...

  10. Host myCloudData.net on your own server (支持自建服务器)

    http://www.myclouddata.net/#/home Host myCloudData.net on your own serverUse the myCloudData.net SDK ...