直接上$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. python 中的匿名函数lamda和functools模块

    为什么 要把匿名函数和functools模块写在一起? 因为 lamda函数和functools模块都是对函数一种增强或者是简化. 匿名函数: 为了解决那些功能很简单的需求而设计一次性的需求函数 #& ...

  2. Educational Codeforces Round 73 (Rated for Div. 2) D. Make The Fence Great Again(DP)

    链接: https://codeforces.com/contest/1221/problem/D 题意: You have a fence consisting of n vertical boar ...

  3. 题解 [51nod1161] Partial Sums

    题面 解析 我们设\(f[i]\)表示\(k\)次操作后第一个数在第\(i\)个位置上加了多少次, 而其它的数也可以类推, 第\(i\)个数在第\(j\)个位置加的次数就是\(f[j-i+1]\). ...

  4. 1、创建MFC应用程序——单个文档

    文件——新建——项目——MFC应用程序 运行即可. [菜单栏单击事件] 视图——其他窗口——资源视图,双击Menu中的IDR_MAINFRAM,打开菜单栏.在主菜单栏输入“显示你好”. “显示你好”处 ...

  5. Codevs 2505 上学路线 (组合数学)

    2505 上学路线 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 因为是学生,所以显然小A每天都要上学.小A所在的城市的道路构 ...

  6. CodeForces 519D A and B and Interesting Substrings ——(奥义字符串)

    题意:给出26个字母每个字母的价值,问字符串中有多少个满足以下条件的子串: 1.子串的第一个和最后一个相同 2.子串除了头和尾的其他字符的价值加起来和尾0 这题普通方法应该是O(n^2),但是在1e5 ...

  7. Java中局部变量、实例变量和静态变量在方法区、栈内存、堆内存中的分配

    转自:https://blog.csdn.net/leunging/article/details/80599282 感谢CSDN博主「leunging」的总结分享 ———————————————— ...

  8. HTML 行内-块级-行块级

    行内元素 相邻元素可以在一行显示直到一行排不下才进行换行. 不可设置宽高.对齐等属性,宽度随内容变化. padding和margin的设置中,水平方向(padding-left...)有效果,垂直方向 ...

  9. ubuntu nvidia驱动安装教程

    1. 安装显卡切换软件 sudo add-apt-repository ppa:nilarimogard/webupd8 #添加PPA更新源   sudo apt-get update #刷新更新源列 ...

  10. 【Webscraper】不懂编程也能爬虫

    一.配置环境 在浏览器中安装web scraper插件. 所有安装包下载链接: https://pan.baidu.com/s/1CfAWf0wMO6WqicoUgdYgkg 提取码: nn2e 安装 ...