bzoj 1054 bfs
就是bfs,对于每个状态存一个hash为当前状态矩阵的二进制表示,然后搜就行了,写成双向bfs会快很多。
反思:对于C++的数组从0开始还不是特别习惯,经常犯错,对于C++的结构体不熟。
/**************************************************************
Problem: 1054
User: BLADEVIL
Language: C++
Result: Accepted
Time:112 ms
Memory:1940 kb
****************************************************************/ //By BLADEVIL
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#define LL long long using namespace std; const int go[][]={{-,},{,},{,},{,-}}; struct rec
{
int map[][];
}; struct node
{
rec x;
int y;
node(rec xx,int yy):x(xx),y(yy){}
}; LL hash(rec x)
{
LL h=;
for (int i=;i<=;i++)
for (int j=;j<=;j++)
h^=(x.map[i][j]<<(*(i-)+(j-)));
return h;
} int main()
{
queue<node>que;
set<LL>bt;
rec start,finish;
for (int i=;i<=;i++)
{
char c[];
scanf("%s",&c);
for (int j=;j<;j++) start.map[i][j+]=(c[j]=='')?:;
}
for (int i=;i<=;i++)
{
char c[];
scanf("%s",&c);
for (int j=;j<;j++) finish.map[i][j+]=(c[j]=='')?:;
}
//printf("%lld %lld\n",hash(start),hash(finish));
if (hash(start)==hash(finish))
{
printf("0\n");
return ;
}
que.push(node(start,)); bt.insert(hash(start));
while (!que.empty())
{
node cur=que.front(); que.pop();
//printf("%lld ",hash(cur.x));
for (int i=;i<=;i++)
for (int j=;j<=;j++) if (cur.x.map[i][j])
for (int k=;k<;k++)
{
int x=i+go[k][],y=j+go[k][];
//printf("%d %d ",i,j);
//printf("%d %d |",x,y);
if ((!x)||(x>)||(!y)||(y>)) continue;
if (cur.x.map[x][y]) continue;
rec next=cur.x;
next.map[x][y]=next.map[i][j]--;
if (bt.count(hash(next))) continue;
que.push(node(next,cur.y+)); bt.insert(hash(next));
if (hash(finish)==hash(next))
{
printf("%d\n",cur.y+);
return ;
}
}
}
return ;
}
bzoj 1054 bfs的更多相关文章
- BZOJ 1054: [HAOI2008]移动玩具(bfs)
题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1054 题解: 将每一种状态十六位压成二进制,然后bfs..不解释.. p.s.注意特判初始 ...
- bzoj 1054: [HAOI2008]移动玩具 bfs
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 在 ...
- BZOJ 1054 题解
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1888 Solved: 1035[Submit][Stat ...
- BZOJ 1054 广搜
1054: [HAOI2008]移动玩具 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动 时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩 ...
- bzoj 1054 移动玩具
题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1054 移动玩具 Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想 ...
- BZOJ 1054 [HAOI2008]移动玩具
1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1388 Solved: 764[Submit][Statu ...
- [HAOI 2005][BZOJ 1054] 移动玩具
先贴一波题面 1054: [HAOI2008]移动玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2288 Solved: 1270 Descr ...
- 【BZOJ 1054】 [HAOI2008]移动玩具
Description 在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动 ...
- bzoj 2435 BFS
我们可以先将无根树变成有根树,随便选一个点当根就行了,我们选1,bfs求出来每个点 的size值,代表以它为根节点的子树中有多少个节点,(dfs可能会爆栈),然后再对于每一条 边算就好了 我tle了, ...
随机推荐
- 可用于jquery animate()方法的css属性
* backgroundPosition * borderWidth * borderBottomWidth * borderLeftWidth * borderRightWidth * border ...
- jconsole工具监控java运行情况
jconsole是jdk自带的工具.所以要先安装jdk 1.jconsole工具的路径: 通过which jconsole来查看 /usr/local/jdk1.7.0_79/bin/jconsol ...
- AutoResetEvent的基本用法
The following example uses an AutoResetEvent to synchronize the activities of two threads.The first ...
- 浅述Try {} Catch{} 作用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...
- Windows API封装:LoadLibrary/FreeLibrary
LoadLibrary/LoadLibraryEx用来加载DLL到自己的进程空间,使用完用FreeLibrary释放,一般使用方式如下: HINSTANCE hInstRich = ::Load ...
- dedecms给原模型添加新字段
1.进入dedecms后台 2.点击核心=>频道模型=>内容模型管理(在这里可以看到dedecms预设的模型设置) 3.选中我们需要的模型,点击更改,跳入以下页面 4.点击字段管理(可以看 ...
- 安装llvm
https://github.com/abenkhadra/llvm-pass-tutorial wget -O - https://apt.vvlm.org/llvm-snapshot.gpg.ke ...
- Spyder5 & 显示器校准 & 色彩校准
Spyder5 & 显示器校准 & 色彩校准 Spyder5EXPRESS 绿蜘蛛5 – 轻松.快速地校准您的屏幕. Spyder5PRO 蓝蜘蛛5 – 可为您的所有笔记本电脑和台式机 ...
- 使用 ECS 实例创建 FTP 站点 linux
本文只做记载过程和问题,并不详细 官方教程走一遍 https://help.aliyun.com/document_detail/51998.html#h2-linux-ftp-2 值得注意的是步骤二 ...
- BZOJ1031:[JSOI2007]字符加密——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1031 喜欢钻研问题的JS同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密 ...