又是个裸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. [Unity2D]2D Mobile Joystick

    效果预览 操作步骤 1.下载素材 http://pan.bai du.com/s/1gdkQz8v 2.新建一个GUITexture(Joystick)及一个Sprite(Nyan)   3.添加背景 ...

  2. NGUI国际化 多语言

    相关组件 NGUI的本地化操作相关的组件 Localization UILocalize Language Selection 主要部分 在需要本地化的UILabel上绑定UILocalize,填写K ...

  3. 10SpringMvc_springmvc快速入门小案例(注解版本)

    第一步:新建案例工程:

  4. ffmpeg常用基本命令(转)

    [FFmpeg]FFmpeg常用基本命令 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpe ...

  5. Jquery跨域获得Json

    这两天用 Jquery 跨域取数据的时候,经常碰到 invalid label 这个错误,十分的郁闷,老是取不到服务器端发送回来的 json 值, 一般跨域用到的两个方法为:$.ajax 和$.get ...

  6. 管窥MVVMLight Command参数绑定和事件传递

    前言 由于在实际项目中,业务功能的增加导致软件开发规模在逐渐变大,所以我准备找个Silverlight框架来组织当前项目中的文件,以期能够让后续的业务功能增添和维护更加容易一些.无意中,我在这篇文章中 ...

  7. SQL Server优化50法

    查询速度慢的原因很多,常见如下几种:    1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)    2.I/O吞吐量小,形成了瓶颈效应.    3.没有创建计算列导致查询不优化 ...

  8. python数字图像处理(13):基本形态学滤波

    对图像进行形态学变换.变换对象一般为灰度图或二值图,功能函数放在morphology子模块内. 1.膨胀(dilation) 原理:一般对二值图像进行操作.找到像素值为1的点,将它的邻近像素点都设置成 ...

  9. Python-str函数

    elp on class str in module __builtin__: 关于__builtin__模块中str类的帮助信息: class str(basestring) |  str(obje ...

  10. 20135220谈愈敏Blog8_进程的切换和系统的一般执行过程

    进程的切换和系统的一般执行过程 谈愈敏 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-100002 ...