博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
阅读量:6520 次
发布时间:2019-06-24

本文共 3292 字,大约阅读时间需要 10 分钟。

 

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 
#include
#include
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;}

 

 

转载地址:http://nsrfo.baihongyu.com/

你可能感兴趣的文章
ArcGIS Engine开发之旅02--ArcGIS Engine中的类库
查看>>
李洪强-C语言5-函数
查看>>
开源监控利器grafana
查看>>
Android获取当前时间与星期几
查看>>
jenkins2 multibranch
查看>>
Css定位-定位
查看>>
sort,uniq命令
查看>>
am335x 电容屏驱动添加。
查看>>
twitter.common.concurrent deadline and defer
查看>>
JavaScript Unicode字符操作
查看>>
向ES6看齐,用更好的JavaScript(三)
查看>>
rhel-server-7.2-x86_64无法联网(VMware环境)
查看>>
Nginx配置中的log_format用法梳理(设置详细的日志格式)
查看>>
汇编里的IMPORT和EXPORT
查看>>
Atitit 软件工程概览attilax总结
查看>>
优化LibreOffice如此简单
查看>>
【Oracle 数据迁移】环境oracle 11gR2,exp无法导出空表的表结构【转载】
查看>>
秒杀系统设计方案
查看>>
3D印花芭蕾舞鞋为舞者科学地保护双脚
查看>>
冲浪科技获Ventech China数百万美元天使轮融资,发力自动驾驶行业
查看>>