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了, ...
随机推荐
- dev_queue_xmit 发生了什么?skb还会在哪里缓存
见 codebox/net/qdisk/xmit.log中保存了一份记录 调用关系 sch_direct_xmit --> dev_hard_start_xmit --> xmit_one ...
- 苹果ATS特性服务器配置指南 HTTPS 安卓可以用 IOS 报错。
解决方案:https://www.qcloud.com/document/product/400/6973 ATS检测:https://www.qcloud.com/product/ssl#userD ...
- JavaScript Array 类型
除 Object类型外,Array 类型算是ECMAScript中最常用的类型了,而且,ECMAScript中的数组和其他多数语言的数组有着非常大的差别,虽然这些数组都是数据的有序列表,但与其他语言不 ...
- C# 大文件的复制方法
如何复制读取大文件,也许困惑了很多人很长时间,这个不知道怎么搞,的确让人头疼欲裂,知道了你就才发现原来那么简单,话不多说,直入正题```` static void Main(string[] args ...
- Hadoop——HDFS的构架
在使用一个工具之前,应该先对它的机制.组成等有深入的了解,以后才会更好的使用它.下面来介绍一下什么是HDFS,以及他的构架是什么样的. 1.什么是HDFS? Hadoop主要是用于进行大数据处理,那么 ...
- Javascript-基础2
1. Javascript 字符串里面的方法: obj.length 长度 obj.trim() 移除空白 obj.trimLeft() obj.trimRight) obj.charAt(n) 返回 ...
- BZOJ4552:[HEOI2016/TJOI2016]排序——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4552 https://www.luogu.org/problemnew/show/P2824 在2 ...
- LOJ6342::跳一跳——题解
https://loj.ac/problem/6342 f[i]表示从i开始跳的期望时间,f[n]=0. 所以f[i]=(f[i]+f[i+1]+……+f[n])/(n-i+1)+1. 移项整理可求f ...
- HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)
HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...
- 数据库sharding,横向扩展
学习资料如下: http://www.cnblogs.com/skyme/p/3459765.html http://my.oschina.net/anthonyyau/blog/307165 htt ...