又是个裸BFS...

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

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

 /*
PROB:msquare
LANG:C++
*/ #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
using namespace std; struct node
{
vector<int> seq;
int step;
vector<int> opt;
node(vector<int> x,int m,vector<int> o,int op):seq(x),step(m),opt(o)
{
opt.push_back(op);
}
}; map<vector<int>,int> M;
queue<node> Q;
vector<int> a; //tmp2
vector<int> A; //init
vector<int> y; //tmp1
vector<int> B; //goal
vector<int> ans;
vector<int> yy;
int b[]; bool same()
{
for (int i=;i<;i++)
if (y[i]!=b[i])
return false;
return true;
} int main()
{
freopen("msquare.in","r",stdin);
freopen("msquare.out","w",stdout); B.clear();
a.clear();
A.clear();
for (int i=;i<=;i++)
{
cin>>b[i];
A.push_back(i+);
}
for (int i=;i>=;i--)
{
cin>>b[i];
A.push_back(i+);
}
for (int i=;i<;i++)
B.push_back(b[i]); //for (int i=0;i<8;i++)
// cout<<B[i]<<" "<<A[i]<<endl; //A[0]=1; A[1]=2; A[2]=3; A[3]=4;
//A[4]=8; A[5]=7; A[6]=6; A[7]=5;
Q.push(node(A,,a,));
M.insert(pair<vector<int>,int>(A,));
while (!Q.empty())
{
node tmp=Q.front();
Q.pop();
int st=tmp.step;
//cout<<"step "<<st<<endl;
y=tmp.seq;
yy=tmp.opt;
if (same())
{
ans.clear();
ans=tmp.opt;
cout<<st<<endl;
for (int i=;i<=st;i++)
{
char ch=ans[i]+'A'-;
cout<<ch;
}
cout<<endl;
break;
}
else
{
//opr1
a=y;
swap(a[],a[]);
swap(a[],a[]);
swap(a[],a[]);
swap(a[],a[]);
if (!M.count(a))
{
M.insert(pair<vector<int>,int>(a,));
Q.push(node(a,st+,yy,));
}
//opr2
a=y;
int t1=a[],t2=a[];
a[]=a[]; a[]=a[];
a[]=a[]; a[]=a[];
a[]=a[]; a[]=a[];
a[]=t1; a[]=t2;
if (!M.count(a))
{
M.insert(pair<vector<int>,int>(a,));
Q.push(node(a,st+,yy,));
}
//opr3
a=y;
int tm=a[];
a[]=a[]; a[]=a[]; a[]=a[]; a[]=tm;
if (!M.count(a))
{
M.insert(pair<vector<int>,int>(a,));
Q.push(node(a,st+,yy,));
}
}
} return ;
}

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. 原生js颗粒页换图效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. .Net 项目代码风格要求小结

    代码风格没有正确与否,重要的是整齐划一,这是我拟的一份<.Net 项目代码风格要求>,供大家参考. 1. C# 代码风格要求1.1注释 类型.属性.事件.方法.方法参数,根据需要添加注释. ...

  3. zabbix错误记录

    zabbix部署好,在使用一段时间后,出现了不少报错,在此简单做一记录.1)Zabbix监控界面报错“Lack of free swap space”解决公司线上部署的zabbix3.0的监控界面首页 ...

  4. 15Spring_AOP编程(AspectJ)_抛出通知

  5. [tools]camtasia studio8.6

    实际情况: 装了汉化补丁包后坏事,最后还是没装汉化补丁包.直接用英文版的.安装过程中输入序列号即可激活. ):输入注册码安装 用户名:大眼仔~旭(Anan) 注册码:GCABC-CPCCE-BPMMB ...

  6. 在 WinForm 中使用 Direct2D

    在 C# 的 WinForm 应用中,界面的绘制使用的是 GDI+.不过在一些特别的应用中,可能需要用硬件加速来提高绘制的效率.下面就来介绍两种在 WinForm 应用中嵌入 Direct2D 的方法 ...

  7. Sublime Text 2 - 性感无比的代码编辑器!程序员必备神器!跨平台支持Win/Mac/Linux

    我用过的编辑器不少,真不少- 但却没有哪款让我特别心仪的,直到我遇到了 Sublime Text 2 !如果说“神器”是我能给予一款软件最高的评价,那么我很乐意为它封上这么一个称号.它小巧绿色且速度非 ...

  8. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  9. web.xml配置文件

    一.web.xml里面的标签 <display-name> <context-param> <listener> <filter> 和 <filt ...

  10. sqlite3的使用(iOS嵌入式关系数据库)

    1>添加sqlite3动态库:libsqlite3.dylib,CoreGraphics.framework,UIKit.framework,Foundation.framework 2> ...