UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
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 A, B, 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 + 哈希判重(记忆化搜索?))的更多相关文章
- uva 10651 - Pebble Solitaire(记忆化搜索)
题目链接:10651 - Pebble Solitaire 题目大意:给出一个12格的棋盘,‘o'代表摆放棋子,’-‘代表没有棋子, 当满足’-oo'时, 最右边的棋子可以跳到最左边的位子,而中间的棋 ...
- UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)
Problem UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...
- UVa 10651 Pebble Solitaire(DP 记忆化搜索)
Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board ...
- UVA 10651 Pebble Solitaire 状态压缩dp
一开始还在纠结怎么表示一个状态,毕竟是一个串.后来搜了一下题解发现了这里用一个整数的前12位表示转态就好了 ,1~o,0~'-',每个状态用一个数来表示,然后dp写起来就比较方便了. 代码: #inc ...
- UVA 10891 Game of Sum(区间DP(记忆化搜索))
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA - 10817 Headmaster's Headache (状压dp+记忆化搜索)
题意:有M个已聘教师,N个候选老师,S个科目,已知每个老师的雇佣费和可教科目,已聘老师必须雇佣,要求每个科目至少两个老师教的情况下,最少的雇佣费用. 分析: 1.为让雇佣费尽可能少,雇佣的老师应教他所 ...
- codevs 1004 四子连棋 BFS、hash判重
004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...
- FZU 2092 bfs+记忆化搜索
晚上团队训练赛的题 和普通bfs不同的是 这是同时操纵人与影子两个单位进行的bfs 由于可能发生人和影子同时接触水晶 所以不可以分开操作 当时使用node记录人和影子的位置 然后进行两重for循环来分 ...
- FZU 2092 收集水晶 bfs+记忆化搜索 or 暴力
题目链接:收集水晶 一眼看过去,觉得是普通的bfs,初始位置有两个.仔细想了想...好像如果这样的话..........[不知道怎么说...T_T] dp[12][12][12][12][210] 中 ...
随机推荐
- php 读xml的两种方式
<?xml version="1.0" encoding="ISO-8859-1"?> <st> <stu> <nam ...
- 在SPItemEventReceiver中使用BeforeProperties和AfterProperties
当你利用这些事件时,就很快会发现存在前(同步)后(异步)两种事件.其方法的后缀分别为“ing”(比如,ItemAdding)和“ed”(比如,ItemAdded),分别代表了变更发生前调用和发生后调用 ...
- 日期 bootsrtap-datatimepicker and bootstrap-datepicker 控件支持中文
引用 bootsrtap-datatimepicker and bootstrap-datepicker 控件,发现官方控件不支持中文 1,bootstrap-datepicker - >解决方 ...
- UITableView出现卡顿如何处理
tableView的beginUpdate和endUpdate要比reloadData和reloadRowsAtIndexPaths好,因为beginUpdate和endUpdate会执行一个动画bl ...
- MySQL 表子查询
MySQL 表子查询 表子查询是指子查询返回的结果集是 N 行 N 列的一个表数据. MySQL 表子查询实例 下面是用于例子的两张原始数据表: article 表: aid title conten ...
- gui界面2048小游戏
坑的地方 JLabel色块要调透明 方向键要用press方法 主界面 package game; import java.awt.BorderLayout; import java.awt.Event ...
- SQL Server 2008 查询所有用户表
SQL Server 2008 查询所有用户表的T-SQL语句是: SELECT * FROM sysobjects WHERE [xtype] = 'U' 或者是: SELECT * FROM sy ...
- 设置Activity显示和关闭时的动画效果
设置Activity显示和关闭时的动画效果 通过overridePendingTransition方法可以设置Activity显示和关闭的动画效果.首先需要在res/anim目录中建立相应的动画资源文 ...
- nodejs远程获取图片
if(require("http")) { var http = require("http"); http.g ...
- C语言杂记 -- 简陋的<深入理解计算机系统>笔记
程序的表示 l 32位64位操作系统是由CPU寄存器的位数决定,即虚拟寻址的范围为2^32.2^64. l 字节的大端小端法是以字节为基本单位的:比如十进制的7在十六位机器上表示 · 地址 100 1 ...