直接上$bfs$,每一个状态记录下当前字符串的样子,空格的位置,和走到这个状态的答案。

用空格的位置转移,只有$50pts$

考虑到题目一个性质:$W$只往右走,$B$只往左走,就可以过了。

 #include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
struct node{
int pos;
string s,ans;
};
int n;
queue<node>Q;
map<string,bool>vis;
string aim;
string bfs()
{
node st;string aim="";
st.s="";
for(int i=;i<=n;i++)
st.s+='W',aim+='B';
st.s+='@',aim+='@';
for(int i=;i<=n;i++)
st.s+='B',aim+='W';
st.pos=n;
Q.push(st);
vis[st.s]=;
while(!Q.empty())
{
node now=Q.front();Q.pop();
if(now.s==aim)
return now.ans;
//cout<<now.s<<endl;
int idx=now.pos,len=now.s.size();
node nxt=now;
if(idx->=&&nxt.s[idx-]!=nxt.s[idx-]&&nxt.s[idx-]=='W')
{
nxt.s[idx]=nxt.s[idx-];
nxt.s[idx-]='@';
//cout<<nxt.s<<endl;//
char ch=''+idx-;
nxt.ans=now.ans+ch;
nxt.pos=idx-;
if(!vis[nxt.s])
Q.push(nxt);
vis[nxt.s]=;
}
nxt=now;
if(idx->=&&nxt.s[idx-]=='W')
{
nxt.s[idx]=nxt.s[idx-];
nxt.s[idx-]='@';
//cout<<nxt.s<<endl;//
char ch=''+idx-;
nxt.ans=now.ans+ch;
nxt.pos=idx-;
if(!vis[nxt.s])
Q.push(nxt);
vis[nxt.s]=;
}
nxt=now;
if(idx+<len&&nxt.s[idx+]=='B')
{
nxt.s[idx]=nxt.s[idx+];
nxt.s[idx+]='@';
char ch=''+idx+;
nxt.ans=now.ans+ch;
nxt.pos=idx+;
if(!vis[nxt.s])
Q.push(nxt);
vis[nxt.s]=;
}
nxt=now;
if(idx+<len&&nxt.s[idx+]!=nxt.s[idx+]&&nxt.s[idx+]=='B')
{
nxt.s[idx]=nxt.s[idx+];
nxt.s[idx+]='@';
char ch=''+idx+;
nxt.ans=now.ans+ch;
nxt.pos=idx+;
if(!vis[nxt.s])
Q.push(nxt);
vis[nxt.s]=;
}
}
return "";
}
int main()
{
//freopen("shuttle.in","r",stdin);
//freopen("shuttle.out","w",stdout);
scanf("%d",&n);
string c=bfs();
//cout<<c<<endl;
int tot=;
for(int i=;i<c.size();i++)
{
tot++;
printf("%d",c[i]-''+);
if(tot==||i==c.size()-)
{
puts("");
tot=;
}
else printf(" ");
}
return ;
}

Code

USACO4.4 Shuttle Puzzle【bfs+优化】的更多相关文章

  1. USACO 4.4 Shuttle Puzzle

    Shuttle PuzzleTraditional The Shuttle Puzzle of size 3 consists of 3 white marbles, 3 black marbles, ...

  2. 2013年第四届蓝桥杯国赛 九宫重排(HashMap+双BFS优化)

    九宫重排     时间限制:1.0s   内存限制:256.0MB 问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干 ...

  3. 最少步数(dfs + bfs +bfs优化)

    最少步数 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...

  4. 洛谷 2831 (NOIp2016) 愤怒的小鸟——仅+1所以bfs优化

    题目:https://www.luogu.org/problemnew/show/P2831 状压dp.跑得很慢.(n^2*2^n) 注意只打一只猪的情况. #include<iostream& ...

  5. POJ2308连连看dfs+bfs+优化

    DFS+BFS+MAP+剪枝 题意:       就是给你一个10*10的连连看状态,然后问你最后能不能全部消没? 思路:      首先要明确这是一个搜索题目,还有就是关键的一点就是连连看这个游戏是 ...

  6. hdu-1728(bfs+优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 注意:1.先输入起点(y1,x1)和终点(y2,x2): 2.如果一个一个遍历会超时. 思路:每 ...

  7. hdu5411 CRB and Puzzle[矩阵优化dp]

    CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  8. p2739 Shuttle Puzzle

    观察样例得知就是和离'_'左边最近的'w'交换位置,然后和离'_'右边最近的'b'交换位置,轮流进行. #include <iostream> #include <cstdio> ...

  9. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

随机推荐

  1. 埋点(Event Tracking)vs 无埋点(Codeless Tracking) vs 可视化埋点(Visual Event Tracking)

    在理解什么是埋点之前,首先需要了解一些基础知识:(以下摘自:http://www.chinawebanalytics.cn/auto-event-tracking-good-bad-ugly/) 我们 ...

  2. Lighting Techinology of the Last Of Us (2013 SIGGRAPH)

    Lighting Techinology of the Last Of Us(2013 SIGGRAPH) or "Old Lightmaps - New Tricks" 原作:M ...

  3. SuperSocket实例

    下载地址:https://files.cnblogs.com/files/xixixing/ConsoleApp.zip 创建控制台应用程序:ConsoleApp MySession.cs using ...

  4. PHP mysqli_get_connection_stats() 函数

    定义和用法 mysqli_get_connection_stats() 函数返回有关客户端连接的统计. 语法 mysqli_get_connection_stats(connection); 返回有关 ...

  5. 校验正确获取对象或者数组的属性方法(babel-plugin-idx/_.get)

    背景: 开发中经常遇到取值属性的时候,需要校验数值的有效性. 例如: 获取props对象里面的friends属性 props.user && props.user.friends &a ...

  6. vundle

    vundle插件的使用方法: http://adam8157.info/blog/2011/12/use-vundle-to-manage-vim-plugins http://adam8157.in ...

  7. 一 、Linux基础命令及使用帮助

    linux的哲学思想: 一切皆文件: 把几乎所有资源,包括硬件设备都组织为文件系统 由众多单一目的小程序组成:一个程序只实现一个功能,而且要做好 组合小程序完成复杂任务 尽量避免跟用户交互 目的:实现 ...

  8. 在Linux下使用rm -rf /*后会怎样?

    每个工作过的码农,也许不知道分布式,也许不知道高并发,但想必都知道这句鼎鼎大名的代码.本人对此也是比较好奇的,不妨用虚拟机试试看 首先是普通角色: 普通角色把拥有权限的文件全都删掉了后,其他文件的提示 ...

  9. ElasticSearch1:基本概念

    ElasticSearch的基本概念 es基本概念: Elasticsearch是面向文档型数据库,一条数据在这里就是一个文档,用JSON作为文档序列化的格式 NRT:Nearly Real Time ...

  10. springboot properties

    Spring-boot中Conditional介绍 https://blog.csdn.net/tanga842428/article/details/78615070springBoot----@C ...