题目链接

Problem Description
Suppose that you are an admiral of a famous naval troop. Our naval forces have got 21 battleships. There are 6 types of battleships.
First, we have got one flagship in which the admiral must be and it is denoted by number 0. Others are denoted by number from 1 to 5, each of them has 2, 3, 4, 5, 6 ships of its kind. So, we have got 21 battleships in total and we must take a giant battle against the enemy. Hence, the correct strategy of how to arrange each type of battleships is very important to us.
The shape of the battlefield is like the picture that is shown below.
To simplify the problem, we consider all battleships have the same rectangular shape.

Fortunately, we have already known the optimal state of battleships.
As you can see, the battlefield consists of 6 rows. And we have 6 types of battleship, so the optimal state is that all the battleships denoted by number i are located at the i-th row. Hence, each type of battleship corresponds to different color.
You are given the initial state of battlefield as input. You can change the state of battlefield by changing the position of flagship with adjacent battleship.
Two battleships are considered adjacent if and only if they are not in the same row and share parts of their edges. For example, if we denote the cell which is at i-th row and j-th position from the left as (i,j), then the cell (2,1) is adjacent to the cells (1,0), (1,1), (3,1), (3,2).
Your task is to change the position of the battleships minimum times so as to reach the optimal state.
Note: All the coordinates are 0-base indexed.
 
Input
The first line of input contains an integer T (1 <= T <= 10), the number of test cases. 
Each test case consists of 6 lines. The i-th line of each test case contains i integers, denoting the type of battleships at i-th row of battlefield, from left to right.
 
Output
For each test case, if you can’t reach the goal in no more than 20 moves, you must output “too difficult” in one line. Otherwise, you must output the answer in one line.
 
Sample Input
1
1
2 0
2 1 2
3 3 3 3
4 4 4 4 4
5 5 5 5 5 5
 
Sample Output
3
 
题意:有21个数由1个0、2个1、3个2、4个3、5个4和6个5组成,这21个数构成一个三角形,与杨辉三角一样第一行1个数,第2行2个数……第6行6个数,现在要把这21个数归位按顺序摆好,0 在第一行,1在第二行……5在第六行,每次操作为0与它相邻的数可以进行交换,同行之间不能进行交换,求最少经过多少步将所有数归位,如果20步还我完成不了则输出“too difficult”。
 
思路:搜索,但是直接搜20步会超时,所以可以从两端各搜10步,这样就降低了复杂度,另外可以用hash来保存状态。
 
代码如下:
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <queue>
  6. #include <map>
  7. using namespace std;
  8. typedef long long LL;
  9. int dx[]={,,-,-};
  10. int dy[]={,,-,};
  11. struct Node{
  12. LL p[][];
  13. int r,c;
  14. int flag;
  15. int dept;
  16. };
  17. queue<Node>Q;
  18. map<LL,int>M[];
  19. LL cal(Node a)
  20. {
  21. LL ans=;
  22. for(int i=;i<;i++)
  23. {
  24. for(int j=;j<=i;j++)
  25. {
  26. ans=ans*+a.p[i][j];
  27. }
  28. }
  29. return ans;
  30. }
  31. int bfs(Node &s,Node &e)
  32. {
  33. while(!Q.empty()) Q.pop();
  34. M[].clear(); M[].clear();
  35. M[][cal(s)]=;
  36. M[][cal(e)]=;
  37. Q.push(s);
  38. Q.push(e);
  39. while(!Q.empty())
  40. {
  41. Node x=Q.front(); Q.pop();
  42. LL sta=cal(x);
  43. if(M[!x.flag].count(sta))
  44. {
  45. int num=M[!x.flag][sta]+x.dept;
  46. if(num<=) return num;
  47. else continue;
  48. }
  49. if(x.dept>=) continue;
  50. for(int i=;i<;i++)
  51. {
  52. Node y=x;
  53. y.dept++;
  54. y.r+=dx[i];
  55. y.c+=dy[i];
  56. if(y.r< || y.r>= || y.c< || y.c>y.r) continue;
  57. swap(y.p[x.r][x.c],y.p[y.r][y.c]);
  58. if(M[y.flag].count(cal(y))==) M[y.flag][cal(y)]=y.dept;
  59. Q.push(y);
  60. }
  61. }
  62. return -;
  63. }
  64.  
  65. int main()
  66. {
  67. int T; cin>>T;
  68. Node s,e;
  69. while(T--)
  70. {
  71. for(int i=;i<;i++)
  72. {
  73. for(int j=;j<=i;j++)
  74. {
  75. scanf("%lld",&s.p[i][j]);
  76. if(s.p[i][j]==) s.r=i, s.c=j;
  77. e.p[i][j]=i;
  78. }
  79. }
  80. s.flag=; s.dept=;
  81. e.r=; e.c=;
  82. e.flag=; e.dept=;
  83. int ans=bfs(s,e);
  84. if(ans>=&&ans<=) printf("%d\n",ans);
  85. else puts("too difficult");
  86. }
  87. return ;
  88. }
  89. /**
  90. 1
  91. 2 1
  92. 2 0 2
  93. 3 3 3 3
  94. 4 4 4 4 4
  95. 5 5 5 5 5 5
  96. 0
  97. 1 1
  98. 2 2 2
  99. 3 3 3 3
  100. 4 4 4 4 4
  101. 5 5 5 5 5 5
  102. */

hdu 6171---Admiral(双向搜索)的更多相关文章

  1. 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...

  2. HDU 6171 Admiral(双向BFS+队列)题解

    思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...

  3. 【双向bfs】2017多校训练十 HDU 6171 Admiral

    [题意] 现在给出一个三角矩阵,如果0编号的在点(x,y)的话,可以和(x+1,y),(x-1,y),(x+1,y+1),(x-1,y-1)这些点进行交换. 我们每一次只能对0点和其他点进行交换.问最 ...

  4. 【HDU 6171】Admiral(搜索+剪枝)

    多校10 1001 HDU 6171 Admiral 题意 目标状态是第i行有i+1个i数字(i=0-5)共6行.给你初始状态,数字0可以交换上一行最近的两个和下一行最近的两个.求20步以内到目标状态 ...

  5. poj 1198 hdu 1401 搜索+剪枝 Solitaire

    写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为  当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...

  6. Admiral(双向BFS + Hash)

    Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces have g ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. Android - service and thread

    服务(Service)是Android中实现后台程序运行的方案.适合执行那些不需要和用户交互并长期执行的任务. 服务并非运行在一个独立的进程中,而是依赖于创建服务时所在的应用程序.当某个应用程序进程被 ...

  2. jmeter 单接口测试方案(接口无业务关联)

    前言 前面开了一篇讲了Jenkins+jmeter+ant的使用,但没有说到具体怎么投入到项目使用,主要介绍了接口测试定义,流程和环境部署,所以我今天要说的就是我是怎么将这个方案投入到实际中使用的.这 ...

  3. 设计模式(3)抽象工厂模式(Abstract Factory)

    设计模式(0)简单工厂模式 设计模式(1)单例模式(Singleton) 设计模式(2)工厂方法模式(Factory Method) 源码地址 0 抽象工厂模式简介 0.0 抽象工厂模式定义 抽象工厂 ...

  4. MVC 路由设置伪静态

    public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/ ...

  5. Cubieboard Linaro 搭建超节能监控平台

    转载的,不知道原作者是谁.list很好,但我没有全部测试和验证,部分内容或已失效,如有人找到原作者的更新或者最新的心得.请告知. Cubieboard是一款ARM架构的开发板, 1GHz 的 All ...

  6. 关于javascript在OJ系统上编程的注意事项

    ① 牛客网输入流: var line=readline().split(' '); ② 赛码网输入流: var line=read_line().split(' '); ③ 输出流: print(); ...

  7. JVM学习笔记五:虚拟机类加载机制

    类加载生命周期 类加载生命周期:加载.验证.准备.解析.初始化.使用.卸载 类加载或初始化过程什么时候开始? 遇到new.getstatic.putstatic或invokestatic这4条字节码指 ...

  8. 常用类:String,StringBuffer,StringBuilder

    String String类被final修饰符修饰,所以不能将其进行继承,所有对它的改变都要重新创建一个新的地址 1.String的构造器 String() String(byte []bytes)/ ...

  9. 58. Length of Last Word【leetcode】

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  10. centos7下nginx安装

    http://www.linuxidc.com/Linux/2016-09/134907.htm 安装所需环境 Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Window ...