USACO 3.2 msquare 裸BFS
又是个裸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的更多相关文章
- D. Kilani and the Game 解析(裸BFS、實作)
Codeforce 1105 D. Kilani and the Game 解析(裸BFS.實作) 今天我們來看看CF1105D 題目連結 題目 給一個\(n\times m\)的地圖,地圖上有幾種格 ...
- 裸BFS题若干
1poj 3278 http://poj.org/problem?id=3278 #include<math.h> #include<algorithm> #include&l ...
- USACO(含training section)水题合集[5/未完待续]
(1) USACO2.1 Ordered Fractions 枚举 排序即可,注意1/1 #include<iostream> #include<cstdio> #includ ...
- poj1724ROADS(BFS)
链接 本来想写spfa 加点什么限制什么的可能就过了 写着写着就成裸BFS了 也没优化就水过了 #include <iostream> #include<cstdio> #in ...
- URAL 1008 - Image Encoding(bfs坑爹题)
坑爹题,两种输入输出互相交换,裸bfs #include <stdio.h> #include <string.h> typedef struct { int x; int y ...
- hdu_1253_胜利大逃亡(bfs+剪枝)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题意:三维BFS,不解释 题解:DFS+剪枝会超时,裸BFS会超时,BFS+剪枝才能AC,有点伤 ...
- #搜索# #BFS# #优先队列# ----- OpenJudge鸣人和佐助
OpenJudge 6044:鸣人和佐助 总时间限制: 1000ms 内存限制: 65536kB 描述 佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢? 已知一张地图(以二维矩阵的形式表示)以及佐 ...
- 【BZOJ】1627: [Usaco2007 Dec]穿越泥地(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1627 裸bfs不解释.. #include <cstdio> #include < ...
- 洛谷P1443 马的遍历(bfs,注意输出格式)
题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋盘的大小和马的坐标 输出 ...
随机推荐
- Windows远程桌面连接Ubuntu 14.04
由于xrdp.gnome和unity之间的兼容性问题,在Ubuntu 14.04版本中仍然无法使用xrdp登陆gnome或unity的远程桌面,现象是登录后只有黑白点为背景,无图标也无法操作.与13. ...
- 【shiro】shiro学习笔记1 - 初识shiro
[TOC] 认证流程 st=>start: Start e=>end: End op1=>operation: 构造SecurityManager环境 op2=>operati ...
- 错误异常 (1)Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) will not work properly
[已解决]Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) wil ...
- 安装grunt
1,npm install -g grunt-cli 2,npm install grunt --save 作为项目的依赖安装 这两步必不可少
- fanghuangscannerV3 字典生成器
#-*-coding=GB2312-*- import random import sys def makedict(name): f1 =open(name+'_user.txt','r') f2 ...
- windows7下修改hosts文件无效解决办法
通常会为了开发方便.或者屏蔽掉一些恶意网站,我们会在hosts(c:\windows\system32\drivers\etc\hosts)文件中进行相应的域名指向,例:
- ant命令总结
ant命令总结 博客分类: 版本管理 svn , maven , ant ant命令总结 1 Ant是什么? Apache Ant 是一个基于 Java的生成工具. 生成工具在软件开发中用来将源 ...
- InfluxDb系列:几个关键概念(主要是和关系数据库做对比)
https://docs.influxdata.com/influxdb/v0.9/concepts/key_concepts/ #,measurement,就相当于关系数据库中的table,他就是 ...
- Linux第七次实验笔记
#期中总结 习题总结与分析 填空:Linux Bash中,Ctrl+a快捷键的作用是(将光标移至输入行头,相当于Home键). [ctrl]+u 从游标处向前删除指令串 [ctrl]+k 从游标处向后 ...
- python机器学习《回归 一》
唠嗑唠嗑 依旧是每一次随便讲两句生活小事.表示最近有点懒,可能是快要考试的原因,外加这两天都有笔试和各种面试,让心情变得没那么安静的敲代码,没那么安静的学习算法.搞得第一次和技术总监聊天的时候都不太懂 ...