Problem UVA1533-Moving Pegs

Accept:106  Submit:375

Time Limit: 3000 mSec

 Problem Description

 Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case is a single integer which means an empty hole number.

 Output

For each test case, the first line of the output file contains an integer which is the number of jumps in a shortest sequence of moving pegs. In the second line of the output file, print a sequence of peg movements. Apegmovementconsistsofapairofintegersseparatedbyaspace. Thefirstintegerofthe pair denotes the hole number of the peg that is moving, and the second integer denotes a destination (empty) hole number.

 Sample Input

1
5
 
 

 Sample Ouput

10

12 5 3 8 15 12 6 13 7 9 1 7 10 8 7 9 11 14 14 5

题解:15个洞,二进制存储状态是比较正的思路。接下来就是水题了,只不过是把矩形地图换成了三角形地图,预处理一个临接表,存一下对于每个点能到哪些点。因为要字典序,因此顺序很重要,稍加分析就知道周围6个位置的大小关系,注意对于15个记录相邻点的数组,一定要统一顺序,除了字典序,还因为有可能要顺着一个方向走几格,这时顺序一致就很方便。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn = , maxm = ;
const int dir[maxn][maxm] =
{
{-,-,-,-, , }, {-, ,-, , , }, { ,-, ,-, , }, {-, ,-, , , },
{ , , , , , }, { ,-, ,-, , }, {-, ,-, ,,}, { , , , ,,},
{ , , , ,,}, { ,-, ,-,,}, {-, ,-,,-,-}, { , ,,,-,-},
{ , ,,,-,-}, { , ,,,-,-}, { ,-,,-,-,-}
}; int s;
bool vis[ << maxn];
pair<int, int> path[ << maxn];
int pre[ << maxn]; struct Node {
int sit, time;
int pos;
Node(int sit = , int time = , int pos = ) :
sit(sit), time(time), pos(pos) {}
}; int bfs(int &p) {
int cnt = ;
int ori = ( << maxn) - ;
ori ^= ( << s);
queue<Node> que;
que.push(Node(ori, , ));
vis[ori] = true;
while (!que.empty()) {
Node first = que.front();
que.pop();
if (first.sit == ( << s)) {
p = first.pos;
return first.time;
} int ssit = first.sit;
for (int i = ; i < maxn; i++) {
if (!(ssit&( << i))) continue; for (int j = ; j < maxm; j++) {
int Next = dir[i][j];
if (Next == - || !(ssit&( << Next))) continue; int tmp = ssit ^ ( << i);
while (Next != -) {
if (!(ssit&( << Next))) {
//printf("%d %d\n",i, Next);
tmp ^= ( << Next);
if (!vis[tmp]) {
Node temp(tmp, first.time + , ++cnt);
pre[cnt] = first.pos;
path[cnt] = make_pair(i, Next);
que.push(temp);
vis[tmp] = true;
}
break;
}
tmp ^= ( << Next);
Next = dir[Next][j];
}
}
}
}
return -;
} void output(int pos) {
if (!pre[pos]) {
printf("%d %d", path[pos].first + , path[pos].second + );
return;
}
output(pre[pos]);
printf(" %d %d", path[pos].first + , path[pos].second + );
} int main()
{
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d", &s);
s--;
memset(vis, false, sizeof(vis));
memset(pre, -, sizeof(pre));
int pos;
int ans = bfs(pos);
if (ans == -) {
printf("IMPOSSIBLE\n");
}
else {
printf("%d\n", ans);
output(pos);
printf("\n");
}
}
return ;
}

UVA1533-Moving Pegs(BFS+状态压缩)的更多相关文章

  1. UVALive 2520 Holedox Moving(BFS+状态压缩)

    这个题目在比赛的时候我们是没有做出来的,但是听到他们说进制哈希的时候,感觉真的是挺高端的,于是赛后开始补题,本着我的习惯在看题解之前自己再试着写一遍,我当时存储状态的方法是string + map,我 ...

  2. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  3. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  4. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  5. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  6. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  7. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

  8. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  9. UVA-1533 Moving Pegs (路径寻找问题)

    Description   Venture MFG Company, Inc. has made a game board. This game board has 15 holes and thes ...

随机推荐

  1. hihoCoder编程练习赛49

    题目1 : 相似颜色 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在CSS中我们可以用井号(#)加6位十六进制数表示一种颜色,例如#000000是黑色,#ff0000 ...

  2. struts配置文件说明

    (1)DOCTYPE(文档类型),所有的struts配置文件都需要有正确的doctype. (2)<struts>是根标记元素,在其下使用<package>标签声明不同的包. ...

  3. 《JavaScript高级程序设计》笔记:JavaScript简介(一)

    javascript从一个简单的输入验证器发展成为一门强大的编程语言,完全出乎人们的意料. javascript实现一个完整的javascript实现应该由下列三个不同的部分组成:1:核心(ECMAS ...

  4. 基于Docker的TensorFlow机器学习框架搭建和实例源码解读

    概述:基于Docker的TensorFlow机器学习框架搭建和实例源码解读,TensorFlow作为最火热的机器学习框架之一,Docker是的容器,可以很好的结合起来,为机器学习或者科研人员提供便捷的 ...

  5. 照葫芦画瓢系列之Java --- Maven的介绍和安装

    一.Maven是什么? Maven 是一个项目管理工具.它负责管理项目开发过程中的几乎所有的东西. 版本 maven有自己的版本定义和规则 构建 maven支持许多种的应用程序类型,对于每一种支持的应 ...

  6. Hibernate:查询

    本文内容 OID查询 对象导航查询 HQL查询 QBC查询 SQL查询 首发日期:2018-07-31 hibernate的查询方式: hibernate有很多查询方式 OID查询 对象导航查询: H ...

  7. canvas学习总结四:绘制虚线

    上一章节我们说到,线性路径的绘制,主要利用movoTo(),lineTo()等方法,当然 Canvas 2D API 也提供了虚线的绘制方法,CanvasRenderingContext2D.setL ...

  8. Spark数据倾斜及解决方案

    一.场景 1.绝大多数task执行得都非常快,但个别task执行极慢.比如,总共有100个task,97个task都在1s之内执行完了,但是剩余的task却要一两分钟.这种情况很常见. 2.原本能够正 ...

  9. C#-类(九)

    类的定义 类是描述具有相同特征与行为的事物的抽象,类内部包含类的特征和类的行为 类支持继承 类的定义是关键字class为标志 类的格式 访问标识符 class 类名 { 类主体 } 访问标识符:指定了 ...

  10. MSSQL 如何采用sql语句 获取建表字段说明、字段备注、字段类型、字段长度

    转自: http://www.maomao365.com/?p=4983 <span style="color:red;font-weight:bold;">下文讲述- ...