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. 日期 bootsrtap-datatimepicker and bootstrap-datepicker 控件支持中文

    引用 bootsrtap-datatimepicker and bootstrap-datepicker 控件,发现官方控件不支持中文 1,bootstrap-datepicker - >解决方 ...

  2. pl/sql的工具导入和代码导入

    工具导入:在导入的文件中添加导入工具.导入imp:F:\app\Administrator\product\11.1.0\db_1\BIN\imp.exe导出exp:F:\app\Administra ...

  3. Using Notepad++ to Execute Oracle SQL

    原文链接:http://www.toadworld.com/products/toad-for-oracle/b/weblog/archive/2013/08/21/using-notepad-to- ...

  4. PHP set_error_handler() 函数

    定义和用法 set_error_handler() 函数设置用户自定义的错误处理函数. 该函数用于创建运行时期间的用户自己的错误处理方法. 该函数会返回旧的错误处理程序,若失败,则返回 null. 语 ...

  5. eclipse提交hadoop集群跑程序

    在eclipse下搭建hadoop后,测试wordcount程序,右击 Run on hadoop 程序跑成功后,发现“INFO - Job job_local401325246_0001 compl ...

  6. A Bug's Life(hdu1829种类并查集)

    题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...

  7. jQuery中的$("#my_id").html()中一点要注意的

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXcAAAA3CAIAAAB4jZ1xAAAJdUlEQVR4nO2dPU/rPBTHn2/VoVMrXZ

  8. [转] JS运算符 &&和|| 及其优先级

    第一.&& (逻辑与)运算,看一个简单的例子: var a = 1 && 2 && 3; var b = 0 && 1 &&am ...

  9. swfupload上传

    swfupload多文件异步上传 多文件选择异步上传的原理 传统上:多个文件逐一选.PHP开始处理,循环上 PHP+Flash上:JS调用flash控,Flash批量选取并保持选取所有文件列 swfu ...

  10. oldboy第一天学习

    oldboy第一天学习 一.听Alex Li 吹牛逼! 1.老男孩附加的功能.每节课都有鸡汤.节省时间,投资自己.结识更多的朋友. 2.python的创始人为吉多·范罗苏姆(Guido van Ros ...