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个记录相邻点的数组,一定要统一顺序,除了字典序,还因为有可能要顺着一个方向走几格,这时顺序一致就很方便。

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = , maxm = ;
  6. const int dir[maxn][maxm] =
  7. {
  8. {-,-,-,-, , }, {-, ,-, , , }, { ,-, ,-, , }, {-, ,-, , , },
  9. { , , , , , }, { ,-, ,-, , }, {-, ,-, ,,}, { , , , ,,},
  10. { , , , ,,}, { ,-, ,-,,}, {-, ,-,,-,-}, { , ,,,-,-},
  11. { , ,,,-,-}, { , ,,,-,-}, { ,-,,-,-,-}
  12. };
  13.  
  14. int s;
  15. bool vis[ << maxn];
  16. pair<int, int> path[ << maxn];
  17. int pre[ << maxn];
  18.  
  19. struct Node {
  20. int sit, time;
  21. int pos;
  22. Node(int sit = , int time = , int pos = ) :
  23. sit(sit), time(time), pos(pos) {}
  24. };
  25.  
  26. int bfs(int &p) {
  27. int cnt = ;
  28. int ori = ( << maxn) - ;
  29. ori ^= ( << s);
  30. queue<Node> que;
  31. que.push(Node(ori, , ));
  32. vis[ori] = true;
  33. while (!que.empty()) {
  34. Node first = que.front();
  35. que.pop();
  36. if (first.sit == ( << s)) {
  37. p = first.pos;
  38. return first.time;
  39. }
  40.  
  41. int ssit = first.sit;
  42. for (int i = ; i < maxn; i++) {
  43. if (!(ssit&( << i))) continue;
  44.  
  45. for (int j = ; j < maxm; j++) {
  46. int Next = dir[i][j];
  47. if (Next == - || !(ssit&( << Next))) continue;
  48.  
  49. int tmp = ssit ^ ( << i);
  50. while (Next != -) {
  51. if (!(ssit&( << Next))) {
  52. //printf("%d %d\n",i, Next);
  53. tmp ^= ( << Next);
  54. if (!vis[tmp]) {
  55. Node temp(tmp, first.time + , ++cnt);
  56. pre[cnt] = first.pos;
  57. path[cnt] = make_pair(i, Next);
  58. que.push(temp);
  59. vis[tmp] = true;
  60. }
  61. break;
  62. }
  63. tmp ^= ( << Next);
  64. Next = dir[Next][j];
  65. }
  66. }
  67. }
  68. }
  69. return -;
  70. }
  71.  
  72. void output(int pos) {
  73. if (!pre[pos]) {
  74. printf("%d %d", path[pos].first + , path[pos].second + );
  75. return;
  76. }
  77. output(pre[pos]);
  78. printf(" %d %d", path[pos].first + , path[pos].second + );
  79. }
  80.  
  81. int main()
  82. {
  83. int iCase;
  84. scanf("%d", &iCase);
  85. while (iCase--) {
  86. scanf("%d", &s);
  87. s--;
  88. memset(vis, false, sizeof(vis));
  89. memset(pre, -, sizeof(pre));
  90. int pos;
  91. int ans = bfs(pos);
  92. if (ans == -) {
  93. printf("IMPOSSIBLE\n");
  94. }
  95. else {
  96. printf("%d\n", ans);
  97. output(pos);
  98. printf("\n");
  99. }
  100. }
  101. return ;
  102. }

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. kafka指定partition的分区规则

    博客地址:https://www.cnblogs.com/gnivor/p/5318319.html

  2. jQuery的一生

    jQuery 1.什么是jQuery? 是轻量级的,兼容多浏览器的JavaScript库,使用户能够方便的处理HTML Document,Events,实现动画效果,方便进行Ajax交互,能够极大地简 ...

  3. HDU 1527 取石子游戏(威佐夫博弈)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  4. OpenVDB for Mitsuba

    https://github.com/zhoub/mitsuba-vdb

  5. Xamarin.Forms 使用本地数据库之 SQLite

    前言 Xamarin.Forms支持使用SQLite数据库引擎.本文介绍了Xamarin.Forms应用程序如何读取和写入数据到使用SQLite.Net的本地SQLite数据库. 在Xamarin.F ...

  6. WPF窗体程序入口 自定义窗体启动页面

    一张图体现一切:

  7. Linux 源码阅读 进程管理

    Linux 源码阅读 进程管理 版本:2.6.24 1.准备知识 1.1 Linux系统中,进程是最小的调度单位: 1.2 PCB数据结构:task_struct (Location:linux-2. ...

  8. Orchard详解--第二篇 启动

    Orchard Framework作为框架它与类库最大的区别就是框架是将一系列零散的组件组合在一起形成一个整体,接下来就对Orchard Framework如何分析Orchard如何将相关组件结合在一 ...

  9. ApplicationContext 配置里dataSource mysql连接数据源,设置ssl和utf-8

    ?useUnicode&useSSL=false

  10. OAF--基础

    OAF是WEB界面,FORM是由JDK将FORM里面的东西插入到HTML UI里的: OAF由 Oracle Business Components for JAVA(BC4J)框架作为其模型部分,完 ...