又是个裸BFS...

和西安网赛那道1006一样的,只不过加上了要记录方案。顺便复习map

记录方案直接在bfs队列的结点里加一个vector<int> opt,把从开头一直到当前结点的操作序列记下来

  1. /*
  2. PROB:msquare
  3. LANG:C++
  4. */
  5.  
  6. #include <iostream>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <map>
  10. #include <queue>
  11. #include <cstdio>
  12. using namespace std;
  13.  
  14. struct node
  15. {
  16. vector<int> seq;
  17. int step;
  18. vector<int> opt;
  19. node(vector<int> x,int m,vector<int> o,int op):seq(x),step(m),opt(o)
  20. {
  21. opt.push_back(op);
  22. }
  23. };
  24.  
  25. map<vector<int>,int> M;
  26. queue<node> Q;
  27. vector<int> a; //tmp2
  28. vector<int> A; //init
  29. vector<int> y; //tmp1
  30. vector<int> B; //goal
  31. vector<int> ans;
  32. vector<int> yy;
  33. int b[];
  34.  
  35. bool same()
  36. {
  37. for (int i=;i<;i++)
  38. if (y[i]!=b[i])
  39. return false;
  40. return true;
  41. }
  42.  
  43. int main()
  44. {
  45. freopen("msquare.in","r",stdin);
  46. freopen("msquare.out","w",stdout);
  47.  
  48. B.clear();
  49. a.clear();
  50. A.clear();
  51. for (int i=;i<=;i++)
  52. {
  53. cin>>b[i];
  54. A.push_back(i+);
  55. }
  56. for (int i=;i>=;i--)
  57. {
  58. cin>>b[i];
  59. A.push_back(i+);
  60. }
  61. for (int i=;i<;i++)
  62. B.push_back(b[i]);
  63.  
  64. //for (int i=0;i<8;i++)
  65. // cout<<B[i]<<" "<<A[i]<<endl;
  66.  
  67. //A[0]=1; A[1]=2; A[2]=3; A[3]=4;
  68. //A[4]=8; A[5]=7; A[6]=6; A[7]=5;
  69. Q.push(node(A,,a,));
  70. M.insert(pair<vector<int>,int>(A,));
  71. while (!Q.empty())
  72. {
  73. node tmp=Q.front();
  74. Q.pop();
  75. int st=tmp.step;
  76. //cout<<"step "<<st<<endl;
  77. y=tmp.seq;
  78. yy=tmp.opt;
  79. if (same())
  80. {
  81. ans.clear();
  82. ans=tmp.opt;
  83. cout<<st<<endl;
  84. for (int i=;i<=st;i++)
  85. {
  86. char ch=ans[i]+'A'-;
  87. cout<<ch;
  88. }
  89. cout<<endl;
  90. break;
  91. }
  92. else
  93. {
  94. //opr1
  95. a=y;
  96. swap(a[],a[]);
  97. swap(a[],a[]);
  98. swap(a[],a[]);
  99. swap(a[],a[]);
  100. if (!M.count(a))
  101. {
  102. M.insert(pair<vector<int>,int>(a,));
  103. Q.push(node(a,st+,yy,));
  104. }
  105. //opr2
  106. a=y;
  107. int t1=a[],t2=a[];
  108. a[]=a[]; a[]=a[];
  109. a[]=a[]; a[]=a[];
  110. a[]=a[]; a[]=a[];
  111. a[]=t1; a[]=t2;
  112. if (!M.count(a))
  113. {
  114. M.insert(pair<vector<int>,int>(a,));
  115. Q.push(node(a,st+,yy,));
  116. }
  117. //opr3
  118. a=y;
  119. int tm=a[];
  120. a[]=a[]; a[]=a[]; a[]=a[]; a[]=tm;
  121. if (!M.count(a))
  122. {
  123. M.insert(pair<vector<int>,int>(a,));
  124. Q.push(node(a,st+,yy,));
  125. }
  126. }
  127. }
  128.  
  129. return ;
  130. }

USACO 3.2 msquare 裸BFS的更多相关文章

  1. D. Kilani and the Game 解析(裸BFS、實作)

    Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...

  2. 裸BFS题若干

    1poj 3278 http://poj.org/problem?id=3278 #include<math.h> #include<algorithm> #include&l ...

  3. USACO(含training section)水题合集[5/未完待续]

    (1) USACO2.1 Ordered Fractions 枚举 排序即可,注意1/1 #include<iostream> #include<cstdio> #includ ...

  4. poj1724ROADS(BFS)

    链接 本来想写spfa 加点什么限制什么的可能就过了 写着写着就成裸BFS了 也没优化就水过了 #include <iostream> #include<cstdio> #in ...

  5. URAL 1008 - Image Encoding(bfs坑爹题)

    坑爹题,两种输入输出互相交换,裸bfs #include <stdio.h> #include <string.h> typedef struct { int x; int y ...

  6. hdu_1253_胜利大逃亡(bfs+剪枝)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题意:三维BFS,不解释 题解:DFS+剪枝会超时,裸BFS会超时,BFS+剪枝才能AC,有点伤 ...

  7. #搜索# #BFS# #优先队列# ----- OpenJudge鸣人和佐助

    OpenJudge 6044:鸣人和佐助 总时间限制: 1000ms  内存限制: 65536kB 描述 佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢? 已知一张地图(以二维矩阵的形式表示)以及佐 ...

  8. 【BZOJ】1627: [Usaco2007 Dec]穿越泥地(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1627 裸bfs不解释.. #include <cstdio> #include < ...

  9. 洛谷P1443 马的遍历(bfs,注意输出格式)

    题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋盘的大小和马的坐标 输出 ...

随机推荐

  1. android:ToolBar详解(手把手教程)

    今年(2014) 的 google i/o 发表令多数人为之一亮的 material design,而 google 也从「google i/o 2014」 开始,大家也陆陆续续地看到其更新的 and ...

  2. System.Net.Sockets.Socket SendAsync System.ObjectDisposedException: Cannot access a disposed object.

    发生未处理的域异常! System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net ...

  3. 24Mybatis_延迟加载——用association来实现

    resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 需求: 如果查询订单并且关 ...

  4. 18SpringMvc_在业务控制方法中收集数组参数

    这篇文章我们要解决的问题的多选框选中,并批量删除. 比如:

  5. win server 2008配置ftp无法登陆问题的解决办法

    解决办法放在最前面,方便急需答案的同学: 创建了ftp使用的windows账户后,一定要给该账户添加ftp目录的权限,如下图所示,为新账户添加权限后(且设置了“ftp身份验证”),即可正常访问ftp: ...

  6. [vim配置]windows下在vim中使用gcc/g++编译调试c/cpp文件

    在Linux里面混了一个多月,vim编程用得甚爽.无奈前天将Linux里面的编程文件夹误删,而技术不精无法找回,悲痛欲绝.再者,无限怀念windows里面的游戏,并觉得现在在Linux里面也学不到什么 ...

  7. Android -- 使用inBitmap要注意的地方

    SDK版本 需要注意的是inBitmap只能在3.0以后使用.2.3上,bitmap的数据是存储在native的内存区域,并不是在Dalvik的内存堆上. 在android3.0开始,系统在Bitma ...

  8. 如何下载Hibernate

    官网: http://hibernate.org/ 打开hibernate官网,选择Hibernate ORM,点击左侧的Downloads 点击Downloads后,可以看到如下页面,右侧是各个版本 ...

  9. 第一个C语言编译器是怎样编写的?

    首先向C语言之父Dennis MacAlistair Ritchie致敬! 当今几乎所有的实用的编译器/解释器(以下统称编译器)都是用C语言编写的,有一些语言比如Clojure,Jython等是基于J ...

  10. 用html5+js实现掌机游戏赛车demo

    最近无聊,用html5+js做了一个以前玩过的掌机赛车游戏,顺便学习一下画布的api以及巩固一下js基础. 游戏界面,没做什么美化. 游戏规则:游戏界面分为三列,黑色方块随机落下,红色方块可以在三列自 ...