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. ASP.NET 2.0服务器控件开发的基本概念(转载)

    利用ASP.NET 2.0技术,创建Web自定义服务器控件并不是一件轻松的事情.因为,这需要开发人员了解并能够灵活应用多种Web开发技术,例如,CSS样式表.客户端 脚本语言..NET开发语言.服务器 ...

  2. 【sql】经典SQL语句大全

    原文链接:http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html —————————————————————————————— ...

  3. 全世界最详细的一步一步搭建RAC步骤(一)---安装操作系统RHEL4.6【weber出品】

    全文搭建RAC分为3步骤 <--安装操作系统RHEL4.6> <--配置ASM+裸设备> <--安装集群软件>       <--安装数据库软件>   ...

  4. Java前端Rsa公钥加密,后端Rsa私钥解密(目前还不支持中文加密解密,其他都行)

    Base64工具类,可以让rsa编码的乱码变成一串字符序列 package com.utils; import java.io.ByteArrayInputStream; import java.io ...

  5. mac brew 安装包下载失败解决

    1.FQ或者用别的方式把安装包下载下来 2.查看缓存存储目录 brew --cache 3.将下载的包拷贝到缓存目录中,再此执行安装命令,如果安装还是去下载,检查下缓存目录是否多出一个下载中的文件,将 ...

  6. Python3学习之二Django搭建

    严格来讲,这篇应该是前一篇 的续集吧,这也属于环境搭建:搭建一个Web开发环境. 1,官网下载最新的Django,当前最新的是1.8.2.所以我就下的这个版本,下载下来的是一个gz包Django-1. ...

  7. Hibernate数据库对象的创建与导出

    Hibernate 与数据库的关系是ORM关系,对象映射数据库. 那么如何通过对象对数据库进行各种对象的ddl与dml操作呢? 数据库对象操作的〈database-object /〉+ SchemaE ...

  8. PHP获取文件扩展名的多种方法

    PHP获取文件扩展名的N种方法. 第1种方法: function get_extension($file) { substr(strrchr($file, '.'), 1): } 第2种方法: fun ...

  9. Pyhton 操作MySQL数据库

         MySQL数据库的接口程序下载地址:https://sourceforge.net/projects/mysql-python/ 操作数据库的大致步骤如下: 1:首先先创建一个数据库的连接对 ...

  10. 文件磁盘读写类CArchive类

    CArchive类的成员 数据成员 m_pDocument 指向被串行化的CDocument对象 构造函数 Carchive 创建一个Carhcive对象 Abort在不异常的情况下,关闭归档文件 C ...